lasthourcloud/app/Http/Livewire/Waitlist/Index.php

64 lines
2.1 KiB
PHP
Raw Normal View History

2023-08-15 12:11:38 +00:00
<?php
2023-09-01 07:34:25 +00:00
namespace App\Http\Livewire\Waitlist;
2023-08-15 12:11:38 +00:00
use App\Jobs\SendConfirmationForWaitlistJob;
2023-08-15 12:27:45 +00:00
use App\Models\User;
2023-09-01 07:34:25 +00:00
use App\Models\Waitlist;
2023-08-15 12:11:38 +00:00
use Livewire\Component;
2023-09-15 09:19:36 +00:00
use Illuminate\Support\Str;
2023-08-15 12:11:38 +00:00
2023-09-01 07:34:25 +00:00
class Index extends Component
2023-08-15 12:11:38 +00:00
{
public string $email;
public int $users = 0;
2023-09-01 07:34:25 +00:00
public int $waitingInLine = 0;
2023-08-15 12:11:38 +00:00
protected $rules = [
'email' => 'required|email',
];
2023-09-01 07:34:25 +00:00
public function render()
{
return view('livewire.waitlist.index')->layout('layouts.simple');
}
2023-08-15 12:11:38 +00:00
public function mount()
{
if (config('coolify.waitlist') == false) {
return redirect()->route('register');
}
2023-09-01 07:34:25 +00:00
$this->waitingInLine = Waitlist::whereVerified(true)->count();
$this->users = User::count();
if (isDev()) {
$this->email = 'waitlist@example.com';
2023-08-15 12:11:38 +00:00
}
}
public function submit()
{
$this->validate();
try {
2023-08-15 12:27:45 +00:00
$already_registered = User::whereEmail($this->email)->first();
if ($already_registered) {
throw new \Exception('You are already on the waitlist or registered. <br>Please check your email to verify your email address or contact support.');
2023-08-15 12:27:45 +00:00
}
2023-09-01 07:34:25 +00:00
$found = Waitlist::where('email', $this->email)->first();
2023-08-15 12:11:38 +00:00
if ($found) {
if (!$found->verified) {
$this->emit('error', 'You are already on the waitlist. <br>Please check your email to verify your email address.');
return;
}
$this->emit('error', 'You are already on the waitlist. <br>You will be notified when your turn comes. <br>Thank you.');
2023-08-15 12:11:38 +00:00
return;
}
2023-09-01 07:34:25 +00:00
$waitlist = Waitlist::create([
2023-09-13 18:48:13 +00:00
'email' => Str::lower($this->email),
2023-08-15 12:11:38 +00:00
'type' => 'registration',
]);
2023-08-15 14:09:40 +00:00
$this->emit('success', 'Check your email to verify your email address.');
2023-08-15 12:11:38 +00:00
dispatch(new SendConfirmationForWaitlistJob($this->email, $waitlist->uuid));
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-08-15 12:11:38 +00:00
}
}
}