refactor: centralize request notification handling into a new trait and apply it to Livewire components.

This commit is contained in:
Yoga Pangestu 2025-12-15 21:32:12 +07:00
parent d266acae85
commit 6b09c09d3e
7 changed files with 35 additions and 90 deletions

View File

@ -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
{

View File

@ -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)
{

View File

@ -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
{

View File

@ -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
{

View File

@ -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();
}

View File

@ -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();
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Traits\Utilities;
trait WithRequestNotification
{
public function mountWithRequestNotification(): void
{
$notification = request()->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);
}
}
}
}