From 6b09c09d3ea660b70f2bb7e941673f01e086ec71 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Mon, 15 Dec 2025 21:32:12 +0700 Subject: [PATCH] refactor: centralize request notification handling into a new trait and apply it to Livewire components. --- app/Livewire/Studio/Catalog/Bottle/Index.php | 18 ++------------- app/Livewire/Studio/Catalog/Perfume/Index.php | 18 ++------------- app/Livewire/Studio/Catalog/Product/Index.php | 18 ++------------- app/Livewire/Studio/Manage/Article/Index.php | 18 ++------------- app/Livewire/Studio/Master/Outlet/Index.php | 15 ++---------- app/Livewire/Studio/Master/User/Index.php | 15 ++---------- .../Utilities/WithRequestNotification.php | 23 +++++++++++++++++++ 7 files changed, 35 insertions(+), 90 deletions(-) create mode 100644 app/Traits/Utilities/WithRequestNotification.php diff --git a/app/Livewire/Studio/Catalog/Bottle/Index.php b/app/Livewire/Studio/Catalog/Bottle/Index.php index e8a6c45..a682929 100644 --- a/app/Livewire/Studio/Catalog/Bottle/Index.php +++ b/app/Livewire/Studio/Catalog/Bottle/Index.php @@ -8,6 +8,7 @@ use App\Traits\Components\WithToast; use App\Traits\Notification\WithSubscribeNotification; use App\Traits\Pricing\WithShowPrice; +use App\Traits\Utilities\WithRequestNotification; use Flux\Flux; use Illuminate\Contracts\View\View; use Livewire\Attributes\Title; @@ -16,22 +17,7 @@ #[Title('Botol')] class Index extends Component { - use WithAuthorization, WithConfirmation, WithShowPrice, WithSubscribeNotification, WithToast; - - public function mount(): void - { - $hasNotification = request()->get('notification'); - - if ($hasNotification) { - $this->js(<<<'JS' - const url = new URL(window.location); - url.search = ''; - window.history.replaceState({}, '', url); - JS); - - $this->toast($hasNotification); - } - } + use WithAuthorization, WithConfirmation, WithRequestNotification, WithShowPrice, WithSubscribeNotification, WithToast; public function delete(Bottle $bottle): void { diff --git a/app/Livewire/Studio/Catalog/Perfume/Index.php b/app/Livewire/Studio/Catalog/Perfume/Index.php index 030e0c0..7ce4ae3 100644 --- a/app/Livewire/Studio/Catalog/Perfume/Index.php +++ b/app/Livewire/Studio/Catalog/Perfume/Index.php @@ -8,6 +8,7 @@ use App\Traits\Components\WithToast; use App\Traits\Notification\WithSubscribeNotification; use App\Traits\Pricing\WithShowPrice; +use App\Traits\Utilities\WithRequestNotification; use Flux\Flux; use Livewire\Attributes\Title; use Livewire\Component; @@ -15,22 +16,7 @@ #[Title('Parfum')] class Index extends Component { - use WithAuthorization, WithConfirmation, WithShowPrice, WithSubscribeNotification, WithToast; - - public function mount(): void - { - $hasNotification = request()->get('notification'); - - if ($hasNotification) { - $this->js(<<<'JS' - const url = new URL(window.location); - url.search = ''; - window.history.replaceState({}, '', url); - JS); - - $this->toast($hasNotification); - } - } + use WithAuthorization, WithConfirmation, WithRequestNotification, WithShowPrice, WithSubscribeNotification, WithToast; public function delete(Perfume $perfume) { diff --git a/app/Livewire/Studio/Catalog/Product/Index.php b/app/Livewire/Studio/Catalog/Product/Index.php index 7104228..01761eb 100644 --- a/app/Livewire/Studio/Catalog/Product/Index.php +++ b/app/Livewire/Studio/Catalog/Product/Index.php @@ -8,6 +8,7 @@ use App\Traits\Components\WithToast; use App\Traits\Notification\WithSubscribeNotification; use App\Traits\Pricing\WithShowPrice; +use App\Traits\Utilities\WithRequestNotification; use Flux\Flux; use Illuminate\Contracts\View\View; use Livewire\Attributes\Title; @@ -16,22 +17,7 @@ #[Title('Produk')] class Index extends Component { - use WithAuthorization, WithConfirmation, WithShowPrice, WithSubscribeNotification, WithToast; - - public function mount(): void - { - $hasNotification = request()->get('notification'); - - if ($hasNotification) { - $this->js(<<<'JS' - const url = new URL(window.location); - url.search = ''; - window.history.replaceState({}, '', url); - JS); - - $this->toast($hasNotification); - } - } + use WithAuthorization, WithConfirmation, WithRequestNotification, WithShowPrice, WithSubscribeNotification, WithToast; public function delete(Product $product): void { diff --git a/app/Livewire/Studio/Manage/Article/Index.php b/app/Livewire/Studio/Manage/Article/Index.php index c5be92d..623b3a5 100644 --- a/app/Livewire/Studio/Manage/Article/Index.php +++ b/app/Livewire/Studio/Manage/Article/Index.php @@ -7,6 +7,7 @@ use App\Traits\Components\WithConfirmation; use App\Traits\Components\WithToast; use App\Traits\Notification\WithSubscribeNotification; +use App\Traits\Utilities\WithRequestNotification; use Flux\Flux; use Illuminate\Contracts\View\View; use Livewire\Attributes\Title; @@ -15,22 +16,7 @@ #[Title('Artikel')] class Index extends Component { - use WithAuthorization, WithConfirmation, WithSubscribeNotification, WithToast; - - public function mount(): void - { - $hasNotification = request()->get('notification'); - - if ($hasNotification) { - $this->js(<<<'JS' - const url = new URL(window.location); - url.search = ''; - window.history.replaceState({}, '', url); - JS); - - $this->toast($hasNotification); - } - } + use WithAuthorization, WithConfirmation, WithRequestNotification, WithSubscribeNotification, WithToast; public function delete(Article $article): void { diff --git a/app/Livewire/Studio/Master/Outlet/Index.php b/app/Livewire/Studio/Master/Outlet/Index.php index f36af50..5e5d517 100644 --- a/app/Livewire/Studio/Master/Outlet/Index.php +++ b/app/Livewire/Studio/Master/Outlet/Index.php @@ -9,6 +9,7 @@ use App\Traits\Components\WithConfirmation; use App\Traits\Components\WithToast; use App\Traits\Media\WithMediaHandler; +use App\Traits\Utilities\WithRequestNotification; use Flux\Flux; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Str; @@ -19,7 +20,7 @@ #[Title('Outlet')] class Index extends Component { - use WithAuthorization, WithCloseModal, WithConfirmation, WithMediaHandler, WithToast; + use WithAuthorization, WithCloseModal, WithConfirmation, WithMediaHandler, WithRequestNotification, WithToast; public Collection $outlets; @@ -29,18 +30,6 @@ class Index extends Component public function mount(): void { - $hasNotification = request()->get('notification'); - - if ($hasNotification) { - $this->js(<<<'JS' - const url = new URL(window.location); - url.search = ''; - window.history.replaceState({}, '', url); - JS); - - $this->toast($hasNotification); - } - $this->loadOutlets(); } diff --git a/app/Livewire/Studio/Master/User/Index.php b/app/Livewire/Studio/Master/User/Index.php index 01960a3..e78d4d3 100644 --- a/app/Livewire/Studio/Master/User/Index.php +++ b/app/Livewire/Studio/Master/User/Index.php @@ -8,6 +8,7 @@ use App\Traits\Components\WithConfirmation; use App\Traits\Components\WithToast; use App\Traits\Notification\WithSubscribeNotification; +use App\Traits\Utilities\WithRequestNotification; use Flux\Flux; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Facades\Hash; @@ -18,7 +19,7 @@ #[Title('Pegawai')] class Index extends Component { - use WithAuthorization, WithConfirmation, WithSubscribeNotification, WithToast; + use WithAuthorization, WithConfirmation, WithRequestNotification, WithSubscribeNotification, WithToast; public Collection $employees; @@ -28,18 +29,6 @@ class Index extends Component public function mount(): void { - $hasNotification = request()->get('notification'); - - if ($hasNotification) { - $this->js(<<<'JS' - const url = new URL(window.location); - url.search = ''; - window.history.replaceState({}, '', url); - JS); - - $this->toast($hasNotification); - } - $this->loadEmployees(); } diff --git a/app/Traits/Utilities/WithRequestNotification.php b/app/Traits/Utilities/WithRequestNotification.php new file mode 100644 index 0000000..bc01398 --- /dev/null +++ b/app/Traits/Utilities/WithRequestNotification.php @@ -0,0 +1,23 @@ +input('notification'); + + if ($notification) { + $this->js(<<<'JS' + const url = new URL(window.location); + url.search = ''; + window.history.replaceState({}, '', url); + JS); + + if (method_exists($this, 'toast')) { + $this->toast($notification); + } + } + } +}