From bbab732bd216653f50eff2a68f871ce42ae2a34e Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Wed, 3 Dec 2025 21:04:19 +0700 Subject: [PATCH] refactor(register): memisahkan tanggung jawab menjadi masing" fungsi dan memberikan return type --- app/Livewire/Auth/Register.php | 68 +++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/app/Livewire/Auth/Register.php b/app/Livewire/Auth/Register.php index 756c637..8b4d665 100644 --- a/app/Livewire/Auth/Register.php +++ b/app/Livewire/Auth/Register.php @@ -8,6 +8,7 @@ use App\Models\User; use App\Traits\WithToast; use App\Traits\WithUpdatedData; +use Illuminate\Contracts\View\View; use Illuminate\Support\Str; use Livewire\Attributes\Layout; use Livewire\Component; @@ -35,16 +36,16 @@ public function mount() ]; } - public function updateStep(int $value) + public function updateStep(int $value): void { - if ($this->currentStep === 1) { + if ($this->shouldValidateBeforeChangingStep()) { $this->form->validateStep1(); } $this->currentStep = $value; } - public function nextStep() + public function nextStep(): void { match ($this->currentStep) { 1 => $this->form->validateStep1(), @@ -57,14 +58,44 @@ public function nextStep() } } - public function prevStep() + public function prevStep(): void { if ($this->currentStep > 1) { $this->currentStep--; } } - public function updated(string $propertyName, $value) + public function updated(string $propertyName, $value): void + { + $this->validateUpdatedField($propertyName); + } + + public function register(): void + { + $this->form->auth(); + + $this->sendRegistrationSuccessToast(); + $this->notifyPrivilegedUsersAboutNewMember(); + $this->redirectAfterRegistration(); + } + + public function render(): View + { + return view('livewire.auth.register'); + } + + /** + * Determine whether validation is required before changing step. + */ + protected function shouldValidateBeforeChangingStep(): bool + { + return $this->currentStep === 1; + } + + /** + * Validate updated field based on current form step rules. + */ + protected function validateUpdatedField(string $propertyName): void { $field = Str::replace('form.', '', $propertyName); @@ -77,27 +108,36 @@ public function updated(string $propertyName, $value) } } - public function register() + /** + * Show success toast after registration. + */ + protected function sendRegistrationSuccessToast(): void { - $this->form->auth(); - $this->toast('Selamat, akun Anda berhasil didaftarkan. Silakan periksa email Anda untuk melakukan verifikasi.'); + } + /** + * Dispatch notification to privileged users when a new member registers. + */ + protected function notifyPrivilegedUsersAboutNewMember(): void + { $userIds = User::role(['Developer', 'Owner'])->pluck('id')->toArray(); + StorePushNotification::dispatch( PushNotification::whereIn('user_id', $userIds)->get(), [ 'title' => '๐ŸŽ‰Member Baru Bergabung!', - 'body' => 'Ada member baru saja. Yuk lihat profil lengkapnya di aplikasi! ๐ŸŒŸ', + 'body' => 'Ada member baru saja bergabung. Cek profil lengkapnya di aplikasi! ๐ŸŒŸ', 'url' => route('studio.loyalty.customer.index'), ], ); + } + /** + * Redirect user after successful registration. + */ + protected function redirectAfterRegistration(): void + { $this->redirectIntended('/member/overview', navigate: true); } - - public function render() - { - return view('livewire.auth.register'); - } }