Skip to content

Commit

Permalink
Add {{ guest-entries:success }} tag (#53)
Browse files Browse the repository at this point in the history
* Revert "Put success into the session (#47)"

This reverts commit d596a97.

* Add `{{ guest-entries:success }}` tag

* Apply Code Style changes

---------

Co-authored-by: duncanmcclean <[email protected]>
  • Loading branch information
duncanmcclean and duncanmcclean authored Apr 21, 2023
1 parent 36f3f9f commit bee2ff1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
4 changes: 2 additions & 2 deletions docs/tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
```
14 changes: 5 additions & 9 deletions src/Http/Controllers/GuestEntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/Tags/GuestEntriesTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,9 @@ public function hasErrors()
{
return session()->has('errors');
}

public function success(): bool
{
return session()->get('guest-entries.success', false);
}
}
9 changes: 3 additions & 6 deletions tests/Http/Controllers/GuestEntryControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
'title' => 'This is great',
'slug' => 'this-is-great',
])
->assertRedirect()
->assertSessionHas('success');
->assertRedirect();

$entry = Entry::all()->last();

Expand Down Expand Up @@ -1006,8 +1005,7 @@
'_id' => 'allo-mate-idee',
'record_label' => 'Unknown',
])
->assertRedirect()
->assertSessionHas('success');
->assertRedirect();

$entry = Entry::find('allo-mate-idee');

Expand Down Expand Up @@ -2004,8 +2002,7 @@
'_collection' => 'albums',
'_id' => 'allo-mate-idee',
])
->assertRedirect()
->assertSessionHas('success');
->assertRedirect();

$entry = Entry::find('allo-mate-idee');

Expand Down

0 comments on commit bee2ff1

Please sign in to comment.