From b8d2b370579fefde95a41d970ab837af0c06ab52 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 12 Dec 2024 03:07:10 +0100 Subject: [PATCH] Add sorting frontend for participants list --- app/Form/Actions/FormUpdateMetaAction.php | 4 ++-- app/Form/Models/Form.php | 4 ++-- .../2024_12_11_235710_update_form_searching.php | 2 ++ resources/js/components/ui/Th.vue | 4 ++-- resources/js/views/form/Participants.vue | 13 +++++++++++++ .../EndToEnd/Form/ParticipantIndexActionTest.php | 16 ++++++++++++++++ tests/Feature/Form/FormStoreActionTest.php | 13 +++++++++++++ tests/Feature/Form/FormUpdateMetaActionTest.php | 10 +++++----- 8 files changed, 55 insertions(+), 11 deletions(-) diff --git a/app/Form/Actions/FormUpdateMetaAction.php b/app/Form/Actions/FormUpdateMetaAction.php index 94480502..f41180cf 100644 --- a/app/Form/Actions/FormUpdateMetaAction.php +++ b/app/Form/Actions/FormUpdateMetaAction.php @@ -22,8 +22,8 @@ class FormUpdateMetaAction return [ 'sorting' => 'array', - 'sorting.0' => 'required|string', - 'sorting.1' => 'required|string|in:asc,desc', + 'sorting.by' => 'required|string', + 'sorting.direction' => 'required|boolean', 'active_columns' => 'array', 'active_columns.*' => ['string', Rule::in([...$form->getFields()->pluck('key')->toArray(), 'created_at', 'prevention'])] ]; diff --git a/app/Form/Models/Form.php b/app/Form/Models/Form.php index dd6741f2..972a348f 100644 --- a/app/Form/Models/Form.php +++ b/app/Form/Models/Form.php @@ -160,7 +160,7 @@ class Form extends Model implements HasMedia if (is_null(data_get($model->meta, 'active_columns'))) { $model->setAttribute('meta', [ 'active_columns' => $model->getFields()->count() ? $model->getFields()->take(4)->pluck('key')->toArray() : null, - 'sorting' => $model->getFields()->count() ? [$model->getFields()->first()->key, 'asc'] : null, + 'sorting' => Sorting::by('id'), ]); return; } @@ -186,6 +186,6 @@ class Form extends Model implements HasMedia public function defaultSorting(): Sorting { - return Sorting::by(data_get($this->meta, 'active_columns.0', 'id')); + return Sorting::from($this->meta['sorting']); } } diff --git a/database/migrations/2024_12_11_235710_update_form_searching.php b/database/migrations/2024_12_11_235710_update_form_searching.php index f1206dbd..9ab74aab 100644 --- a/database/migrations/2024_12_11_235710_update_form_searching.php +++ b/database/migrations/2024_12_11_235710_update_form_searching.php @@ -2,6 +2,7 @@ use App\Form\Actions\UpdateParticipantSearchIndexAction; use App\Form\Models\Form; +use App\Lib\Sorting; use Illuminate\Database\Migrations\Migration; return new class extends Migration @@ -16,6 +17,7 @@ return new class extends Migration foreach ($form->participants as $participant) { $participant->searchable(); } + $form->update(['meta' => [...$form->meta, 'sorting' => Sorting::by('id')]]); } } diff --git a/resources/js/components/ui/Th.vue b/resources/js/components/ui/Th.vue index 866ff5e5..db88aa65 100644 --- a/resources/js/components/ui/Th.vue +++ b/resources/js/components/ui/Th.vue @@ -2,8 +2,8 @@
- ASC - DESC + ASC + DESC
diff --git a/resources/js/views/form/Participants.vue b/resources/js/views/form/Participants.vue index e418131e..24cb4faf 100644 --- a/resources/js/views/form/Participants.vue +++ b/resources/js/views/form/Participants.vue @@ -186,6 +186,7 @@ const getSort = computed(() => innerFilter.value.sort); async function setSort(column) { innerFilter.value.sort = getSort.value.by === column ? {by: column, direction: !getSort.value.direction} : {by: column, direction: false}; + sortingConfig.value = innerFilter.value.sort; } const activeColumnsConfig = computed({ @@ -200,6 +201,18 @@ const activeColumnsConfig = computed({ }, }); +const sortingConfig = computed({ + get: () => meta.value.form_meta.sorting, + set: async (v) => { + const response = await axios.patch(meta.value.links.update_form_meta, { + ...meta.value.form_meta, + sorting: v, + }); + + meta.value.form_meta = response.data; + }, +}); + async function handleDelete() { await remove(deleting.value); deleting.value = null; diff --git a/tests/EndToEnd/Form/ParticipantIndexActionTest.php b/tests/EndToEnd/Form/ParticipantIndexActionTest.php index 88a665c2..8854169e 100644 --- a/tests/EndToEnd/Form/ParticipantIndexActionTest.php +++ b/tests/EndToEnd/Form/ParticipantIndexActionTest.php @@ -80,6 +80,22 @@ it('testItShowsEmptyFilters', function () { $this->callFilter('form.participant.index', ['data' => []], ['form' => $form])->assertJsonPath('meta.filter.data.check', ParticipantFilterScope::$nan); }); +it('sorts by active colums sorting by default', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->fields([ + $this->checkboxField('check'), + $this->checkboxField('vorname'), + ])->create(); + $form->update(['meta' => ['active_columns' => [], 'sorting' => ['by' => 'vorname', 'direction' => true]]]); + + sleep(2); + $this->callFilter('form.participant.index', [], ['form' => $form]) + ->assertOk() + ->assertJsonPath('meta.filter.sort.by', 'vorname') + ->assertJsonPath('meta.filter.sort.direction', true); +}); + + it('testItDisplaysHasNamiField', function () { $this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->fields([$this->namiField('mitglieder')])->create(); diff --git a/tests/Feature/Form/FormStoreActionTest.php b/tests/Feature/Form/FormStoreActionTest.php index 644d23e9..1c9fe294 100644 --- a/tests/Feature/Form/FormStoreActionTest.php +++ b/tests/Feature/Form/FormStoreActionTest.php @@ -59,6 +59,19 @@ class FormStoreActionTest extends FormTestCase $this->assertFrontendCacheCleared(); } + public function testItStoresDefaultSorting(): void + { + Event::fake([Succeeded::class]); + $this->login()->loginNami()->withoutExceptionHandling(); + FormRequest::new()->fields([$this->textField()])->fake(); + + $this->postJson(route('form.store'))->assertOk(); + + $form = Form::latest()->first(); + $this->assertEquals('id', $form->meta['sorting']['by']); + $this->assertFalse(false, $form->meta['sorting']['direction']); + } + public function testRegistrationDatesCanBeNull(): void { $this->login()->loginNami()->withoutExceptionHandling(); diff --git a/tests/Feature/Form/FormUpdateMetaActionTest.php b/tests/Feature/Form/FormUpdateMetaActionTest.php index d1882d80..d687e062 100644 --- a/tests/Feature/Form/FormUpdateMetaActionTest.php +++ b/tests/Feature/Form/FormUpdateMetaActionTest.php @@ -21,13 +21,13 @@ class FormUpdateMetaActionTest extends FormTestCase $this->patchJson(route('form.update-meta', ['form' => $form]), [ 'active_columns' => ['textone'], - 'sorting' => ['textone', 'desc'], + 'sorting' => ['by' => 'textone', 'direction' => false], ])->assertOk() ->assertJsonPath('active_columns.0', 'textone') - ->assertJsonPath('sorting.1', 'desc'); + ->assertJsonPath('sorting.by', 'textone'); $form = Form::latest()->first(); - $this->assertEquals(['textone', 'desc'], $form->meta['sorting']); + $this->assertEquals(['by' => 'textone', 'direction' => false], $form->meta['sorting']); $this->assertEquals(['textone'], $form->meta['active_columns']); } @@ -38,11 +38,11 @@ class FormUpdateMetaActionTest extends FormTestCase $this->patchJson(route('form.update-meta', ['form' => $form]), [ 'active_columns' => ['created_at'], - 'sorting' => ['created_at', 'desc'], + 'sorting' => ['by' => 'textone', 'direction' => false], ])->assertOk(); $form = Form::latest()->first(); - $this->assertEquals(['created_at', 'desc'], $form->fresh()->meta['sorting']); + $this->assertEquals(['by' => 'textone', 'direction' => false], $form->fresh()->meta['sorting']); $this->assertEquals(['created_at'], $form->fresh()->meta['active_columns']); } }