updated subscription elements
This commit is contained in:
76
app/Livewire/EmailSubscribe.php
Normal file
76
app/Livewire/EmailSubscribe.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Jobs\SendEmail;
|
||||
use Carbon\Carbon;
|
||||
use Livewire\Component;
|
||||
use App\Models\EmailSubscriptions;
|
||||
use App\Mail\UserWelcome;
|
||||
|
||||
class EmailSubscribe extends Component
|
||||
{
|
||||
public string $email = '';
|
||||
public bool $success = false;
|
||||
public string $message = '';
|
||||
public string $trap = '';
|
||||
|
||||
protected $rules = [
|
||||
'email' => 'required|email|max:255',
|
||||
];
|
||||
|
||||
public function subscribe(): void
|
||||
{
|
||||
// Honeypot - if this hidden field is filled, treat as success but do nothing
|
||||
if (! empty($this->trap)) {
|
||||
$this->reset(['email', 'trap']);
|
||||
$this->success = true;
|
||||
$this->message = 'Thanks, you have been subscribed to our newsletter.';
|
||||
return;
|
||||
}
|
||||
|
||||
// Simple rate limiting per session
|
||||
$attempts = session('subscribe_attempts', 0);
|
||||
if ($attempts >= 5) {
|
||||
$this->success = false;
|
||||
$this->message = 'Too many attempts. Please try again in a little while.';
|
||||
return;
|
||||
}
|
||||
session(['subscribe_attempts' => $attempts + 1]);
|
||||
|
||||
|
||||
$this->validate();
|
||||
|
||||
// Look up existing subscription by email
|
||||
$subscription = EmailSubscriptions::where('email', $this->email)->first();
|
||||
|
||||
// If already confirmed, do not create a new record or resend confirmation
|
||||
if ($subscription && $subscription->confirmed) {
|
||||
// Optionally you could set a different flag or message here
|
||||
$this->success = false;
|
||||
$this->message = 'That email is already subscribed to our newsletter.';
|
||||
} else {
|
||||
// If no subscription exists, create a new unconfirmed one
|
||||
if (!$subscription) {
|
||||
$subscription = EmailSubscriptions::create([
|
||||
'email' => $this->email,
|
||||
'confirmed' => Carbon::now()
|
||||
]);
|
||||
|
||||
$subscription->save();
|
||||
}
|
||||
|
||||
dispatch(new SendEmail($subscription->email, new UserWelcome($subscription->email)))->onQueue('mail');
|
||||
|
||||
$this->success = true;
|
||||
$this->message = 'Thanks, you have been subscribed to our newsletter.';
|
||||
}
|
||||
|
||||
$this->reset(['email', 'trap']);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.email-subscribe');
|
||||
}
|
||||
}
|
||||
BIN
public/about.webp
Normal file
BIN
public/about.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 49 KiB |
39
resources/views/livewire/email-subscribe.blade.php
Normal file
39
resources/views/livewire/email-subscribe.blade.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<div>
|
||||
<form wire:submit.prevent="subscribe" class="flex flex-row justify-center">
|
||||
<input
|
||||
type="text"
|
||||
name="name"
|
||||
wire:model.defer="trap"
|
||||
autocomplete="off"
|
||||
tabindex="-1"
|
||||
class="hidden"
|
||||
/>
|
||||
|
||||
<x-ui.input
|
||||
type="email"
|
||||
name="email"
|
||||
label="Email"
|
||||
no-label
|
||||
wire:model.defer="email"
|
||||
class="m-0"
|
||||
field-classes="rounded-r-none sm:w-96"
|
||||
/>
|
||||
|
||||
{{-- Submit button --}}
|
||||
<x-ui.button color="dark" type="submit" class="rounded-l-none">
|
||||
Subscribe
|
||||
</x-ui.button>
|
||||
</form>
|
||||
|
||||
@if($message)
|
||||
@if($success)
|
||||
<p class="mt-4 text-sm text-green-600 mx-auto border-green-800 bg-green-100 py-1 px-4 w-fit">
|
||||
<i class="fa fa-check mr-2"></i>{{ $message }}
|
||||
</p>
|
||||
@else
|
||||
<p class="mt-4 text-sm text-red-600 mx-auto border-red-800 bg-red-100 py-1 px-4 w-fit">
|
||||
<i class="fa fa-exclamation-triangle mr-2"></i>{{ $message }}
|
||||
</p>
|
||||
@endif
|
||||
@endif
|
||||
</div>
|
||||
Reference in New Issue
Block a user