Add update of active columns in participants table

This commit is contained in:
philipp lang 2024-02-21 23:18:31 +01:00
parent acec968963
commit 9d11294fde
5 changed files with 101 additions and 5 deletions

View File

@ -0,0 +1,45 @@
<?php
namespace App\Form\Actions;
use App\Form\Models\Form;
use Illuminate\Validation\Rule;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class FormUpdateMetaAction
{
use AsAction;
/**
* @return array<string, mixed>
*/
public function rules(): array
{
/** @var Form */
$form = request()->route('form');
return [
'sorting' => 'array',
'sorting.0' => 'required|string',
'sorting.1' => 'required|string|in:asc,desc',
'active_columns' => 'array',
'active_columns.*' => ['string', Rule::in($form->getFields()->pluck('key')->toArray())]
];
}
/**
* @param array<string, mixed> $input
*/
public function handle(Form $form, array $input): void
{
$form->update(['meta' => $input]);
}
public function asController(Form $form, ActionRequest $request)
{
$this->handle($form, $request->validated());
return response()->json($form->fresh()->meta);
}
}

View File

@ -35,7 +35,10 @@ class ParticipantResource extends JsonResource
public static function meta(Form $form): array
{
return [
'active_columns' => $form->active_columns,
'form_meta' => $form->meta,
'links' => [
'update_form_meta' => route('form.update-meta', ['form' => $form]),
],
'columns' => $form->getFields()
->map(fn ($field) => Field::fromConfig($field))
->map(fn ($field) => [

View File

@ -1,5 +1,8 @@
<template>
<div class="mt-5">
<page-filter breakpoint="lg">
<f-multipleselect id="active_columns" v-model="activeColumnsConfig" :options="meta.columns" label="Aktive Spalten" size="sm" name="active_columns"></f-multipleselect>
</page-filter>
<table cellspacing="0" cellpadding="0" border="0" class="custom-table custom-table-sm">
<thead>
<th v-for="column in activeColumns" :key="column.id" v-text="column.name"></th>
@ -25,7 +28,6 @@
<script setup>
import {ref, computed} from 'vue';
import {useApiIndex} from '../../composables/useApiIndex.js';
import FormBuilder from '../formtemplate/FormBuilder.vue';
const props = defineProps({
url: {
@ -35,9 +37,21 @@ const props = defineProps({
},
});
var {meta, data, reload, reloadPage} = useApiIndex(props.url, 'participant');
await reload();
var {meta, data, reload, reloadPage, axios} = useApiIndex(props.url, 'participant');
const activeColumns = computed(() => meta.value.columns.filter((c) => meta.value.form_meta.active_columns.includes(c.id)));
const activeColumnsConfig = computed({
get: () => meta.value.form_meta.active_columns,
set: async (v) => {
const response = await axios.patch(meta.value.links.update_form_meta, {
...meta.value.form_meta,
active_columns: v,
});
meta.value.form_meta = response.data;
},
});
await reload();
</script>

View File

@ -0,0 +1,33 @@
<?php
namespace Tests\Feature\Form;
use App\Form\Models\Form;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class FormUpdateMetaActionTest extends FormTestCase
{
use DatabaseTransactions;
public function testItUpdatesMetaOfForm(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
$this->textField('textone'),
$this->dropdownField('texttwo'),
])])->create();
$this->patchJson(route('form.update-meta', ['form' => $form]), [
'active_columns' => ['textone'],
'sorting' => ['textone', 'desc'],
])->assertOk()
->assertJsonPath('active_columns.0', 'textone')
->assertJsonPath('sorting.1', 'desc');
$form = Form::latest()->first();
$this->assertEquals(['textone', 'desc'], $form->meta['sorting']);
$this->assertEquals(['textone'], $form->meta['active_columns']);
}
}

View File

@ -50,6 +50,7 @@ class ParticipantIndexActionTest extends FormTestCase
->assertJsonPath('meta.columns.6.display_attribute', 'birthday_display')
->assertJsonPath('meta.columns.0.display_attribute', 'vorname_display')
->assertJsonPath('meta.form_meta.active_columns', ['vorname', 'select', 'stufe', 'test1'])
->assertJsonPath('meta.links.update_form_meta', route('form.update-meta', ['form' => $form]))
->assertJsonPath('meta.form_meta.sorting', ['vorname', 'asc']);
}