From bee2ff1cd1ca898021936e58d868d12dfc2cb73d Mon Sep 17 00:00:00 2001 From: Duncan McClean <19637309+duncanmcclean@users.noreply.github.com> Date: Fri, 21 Apr 2023 17:18:58 +0100 Subject: [PATCH] Add `{{ guest-entries:success }}` tag (#53) * Revert "Put success into the session (#47)" This reverts commit d596a978a43525ef7c91f4ef48518019add7bc72. * Add `{{ guest-entries:success }}` tag * Apply Code Style changes --------- Co-authored-by: duncanmcclean --- docs/tags.md | 4 ++-- src/Http/Controllers/GuestEntryController.php | 14 +++++--------- src/Tags/GuestEntriesTag.php | 5 +++++ .../Http/Controllers/GuestEntryControllerTest.php | 9 +++------ 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/docs/tags.md b/docs/tags.md index 28e371b..d66d145 100644 --- a/docs/tags.md +++ b/docs/tags.md @@ -106,10 +106,10 @@ If you'd like to show any errors after a user has submitted the Guest Entries fo ### Success -If you'd like to show a success message after a user has submitted a Guest entry, you can test for the session success key. +If you'd like to show a success message after a user has submitted the Guest Entries form, you can use the `{{ guest-entries:success }}` tag, like shown below: ```antlers -{{ if {session:has key="success"} }} +{{ if {guest-entries:success} }} Well done buddy! {{ /if }} ``` diff --git a/src/Http/Controllers/GuestEntryController.php b/src/Http/Controllers/GuestEntryController.php index b58841e..62ffcfa 100644 --- a/src/Http/Controllers/GuestEntryController.php +++ b/src/Http/Controllers/GuestEntryController.php @@ -76,9 +76,7 @@ public function store(StoreRequest $request) event(new GuestEntryCreated($entry)); - return $this->withSuccess($request, [ - 'success' => true, - ]); + return $this->withSuccess($request); } public function update(UpdateRequest $request) @@ -151,9 +149,7 @@ public function update(UpdateRequest $request) event(new GuestEntryUpdated($entry)); - return $this->withSuccess($request, [ - 'success' => true, - ]); + return $this->withSuccess($request); } public function destroy(DestroyRequest $request) @@ -168,9 +164,7 @@ public function destroy(DestroyRequest $request) event(new GuestEntryDeleted($entry)); - return $this->withSuccess($request, [ - 'success' => true, - ]); + return $this->withSuccess($request); } protected function processField(Field $field, $key, $value, $request): mixed @@ -339,6 +333,8 @@ protected function withSuccess(Request $request, array $data = []) return response()->json($data); } + $request->session()->put('guest-entries.success', true); + return $request->_redirect ? redirect($request->_redirect)->with($data) : back()->with($data); diff --git a/src/Tags/GuestEntriesTag.php b/src/Tags/GuestEntriesTag.php index 9c2aa61..c7265e6 100644 --- a/src/Tags/GuestEntriesTag.php +++ b/src/Tags/GuestEntriesTag.php @@ -97,4 +97,9 @@ public function hasErrors() { return session()->has('errors'); } + + public function success(): bool + { + return session()->get('guest-entries.success', false); + } } diff --git a/tests/Http/Controllers/GuestEntryControllerTest.php b/tests/Http/Controllers/GuestEntryControllerTest.php index 990fe07..c39edb8 100644 --- a/tests/Http/Controllers/GuestEntryControllerTest.php +++ b/tests/Http/Controllers/GuestEntryControllerTest.php @@ -36,8 +36,7 @@ 'title' => 'This is great', 'slug' => 'this-is-great', ]) - ->assertRedirect() - ->assertSessionHas('success'); + ->assertRedirect(); $entry = Entry::all()->last(); @@ -1006,8 +1005,7 @@ '_id' => 'allo-mate-idee', 'record_label' => 'Unknown', ]) - ->assertRedirect() - ->assertSessionHas('success'); + ->assertRedirect(); $entry = Entry::find('allo-mate-idee'); @@ -2004,8 +2002,7 @@ '_collection' => 'albums', '_id' => 'allo-mate-idee', ]) - ->assertRedirect() - ->assertSessionHas('success'); + ->assertRedirect(); $entry = Entry::find('allo-mate-idee');