Skip to content

Commit

Permalink
Correctly show messages for the user if already added to mailing list
Browse files Browse the repository at this point in the history
  • Loading branch information
Raccoon254 committed Jun 16, 2024
1 parent bc0e615 commit 517c2a7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
9 changes: 8 additions & 1 deletion app/Livewire/Newsletter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ class Newsletter extends Component
public $email;

protected $rules = [
'email' => 'required|email|unique:subscribers,email',
'email' => 'required|email',
];

public function subscribe(): void
{
$this->validate();

//if the email already exists in the database, return an error message
if (Subscriber::where('email', $this->email)->exists()) {
session()->flash('message', 'You have already subscribed to the newsletter!');

return;
}

Subscriber::create(['email' => $this->email]);

session()->flash('message', 'You have successfully subscribed to the newsletter!');
Expand Down
31 changes: 19 additions & 12 deletions resources/views/livewire/newsletter.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<div class="center bg-blue-500 p-4 md:p-14">
<form class="w-full max-w-md flex flex-col gap-4 center bg-blue-500 items-center" wire:submit.prevent="subscribe">
@csrf
<x-text-input type="email" name="email" autocomplete="email" placeholder="Your email address" required />
@error('email')
<p class="text-xs text-red-500">{{ $message }}</p>
@enderror
<button class="py-2 text-blue-500 font-semibold w-full bg-white px-4 rounded-md ring" type="submit">
Subscribe
<i class="fas fa-paper-plane"></i>
</button>
</form>
<div class="">
@if (session()->has('message'))
<div class="text-white w-full alert alert-warning rounded-none bg-green-500">
{{ session('message') }}
</div>
@endif
<div class="center flex-col bg-blue-500 p-4 md:p-14">
<form class="w-full max-w-md flex flex-col gap-4 center bg-blue-500 items-center" wire:submit.prevent="subscribe">
@csrf
<x-text-input type="email" wire:model="email" autocomplete="email" placeholder="Your email address" required />
@error('email')
<p class="text-xs text-red-500">{{ $message }}</p>
@enderror
<button class="py-2 text-blue-500 font-semibold w-full bg-white px-4 rounded-md ring" type="submit">
Subscribe
<i class="fas fa-paper-plane"></i>
</button>
</form>
</div>
</div>

0 comments on commit 517c2a7

Please sign in to comment.