From 970119760b4ed3a06db4cbaf038be06aa22c9916 Mon Sep 17 00:00:00 2001 From: erishBRBS Date: Wed, 21 May 2025 21:17:39 +0800 Subject: [PATCH] notification crud added --- app/Livewire/Buttons/CreateNotification.php | 52 +++++++++++++++++-- app/Livewire/Buttons/CreateUser.php | 2 - app/Livewire/Components/Table.php | 2 +- app/Livewire/Notification.php | 8 +-- .../buttons/create-notification.blade.php | 21 ++++++-- .../notification/notification.blade.php | 5 +- 6 files changed, 74 insertions(+), 16 deletions(-) diff --git a/app/Livewire/Buttons/CreateNotification.php b/app/Livewire/Buttons/CreateNotification.php index 80ded15..4d3e531 100644 --- a/app/Livewire/Buttons/CreateNotification.php +++ b/app/Livewire/Buttons/CreateNotification.php @@ -3,18 +3,64 @@ namespace App\Livewire\Buttons; use Livewire\Component; +use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Session; class CreateNotification extends Component { + + public $subject; + public $description; + public $set_schedule = 'no'; + public $schedule; + public $expiration; + public $status = 0; + + public function submit() { - return redirect()->to('/main/notification'); + + $token = Session::get('user')['access_token'] ?? null; + + + if (!$token) { + $this->addError('users', 'No access token found.'); + return; + } + + + $response = Http::withToken($token)->post(config('services.backend_api.url') . '/api/cms/notification', [ + 'subject' => $this->subject, + 'description' => $this->description, + 'schedule' => $this->set_schedule === 'yes' ? $this->schedule : null, + 'expiration' => $this->set_schedule === 'yes' ? $this->expiration : null, + 'status' => $this->status, + ]); + + // dd($response); + + + //handle backend response + if ($response->status() === 422) { + $errors = $response->json('data'); + foreach ($errors as $field => $messages) { + $this->addError($field, $messages[0]); + } + return; + } + + if ($response->successful()) { + session()->flash('success', 'Notification created successfully.'); + return redirect('/main/notification'); + } else { + $this->addError('notification', 'Failed to create notification.'); + } } - + public function cancel() { return redirect()->to('/main/notification'); - } + } public function render() { diff --git a/app/Livewire/Buttons/CreateUser.php b/app/Livewire/Buttons/CreateUser.php index c2e6971..1cc77ea 100644 --- a/app/Livewire/Buttons/CreateUser.php +++ b/app/Livewire/Buttons/CreateUser.php @@ -65,8 +65,6 @@ class CreateUser extends Component 'password' => $this->default_password, ]); - // dd($response->json()); - //handle backend response if ($response->status() === 422) { diff --git a/app/Livewire/Components/Table.php b/app/Livewire/Components/Table.php index b803c2e..778c77e 100644 --- a/app/Livewire/Components/Table.php +++ b/app/Livewire/Components/Table.php @@ -170,7 +170,7 @@ public function deleteConfirmed() { $currentRows = $this->getPaginatedRows(); if ($this->selectAll) { - $this->selected = collect($currentRows)->pluck('id')->toArray(); + $this->selected = collect($currentRows)->pluck($this->rowKey)->toArray(); } else { $this->selected = []; } diff --git a/app/Livewire/Notification.php b/app/Livewire/Notification.php index 0162b63..d2dc48c 100644 --- a/app/Livewire/Notification.php +++ b/app/Livewire/Notification.php @@ -38,10 +38,10 @@ class Notification extends Component return [ 'id' => $notifs['id'], 'subject' => $notifs['subject'], - 'content' => $notifs['description'], - 'is_scheduled' => $notifs['trigger_schedule'], - 'schedule' => $notifs['schedule']?? '-', - 'expiration' => $notifs['expiration_date'], + 'description' => $notifs['description'], + 'schedule' => $notifs['trigger_schedule']?? '-', + 'expiration' => $notifs['expiration_date']?? '-', + 'is_scheduled' => !empty($notifs['trigger_schedule']) ? 'Yes' : 'No', ]; }); } else { diff --git a/resources/views/livewire/buttons/create-notification.blade.php b/resources/views/livewire/buttons/create-notification.blade.php index bf42d9c..651fae1 100644 --- a/resources/views/livewire/buttons/create-notification.blade.php +++ b/resources/views/livewire/buttons/create-notification.blade.php @@ -30,13 +30,13 @@
- +
- +
@@ -44,13 +44,26 @@
+ + @if($set_schedule === 'yes') +
+ + +
+ +
+ + +
+ @endif +
diff --git a/resources/views/livewire/notification/notification.blade.php b/resources/views/livewire/notification/notification.blade.php index 8cdc6b7..5cbaf13 100644 --- a/resources/views/livewire/notification/notification.blade.php +++ b/resources/views/livewire/notification/notification.blade.php @@ -7,14 +7,15 @@ :columns="[ ['label' => 'ID', 'field' => 'id'], ['label' => 'Subject', 'field' => 'subject'], - ['label' => 'Content', 'field' => 'content'], + ['label' => 'Content', 'field' => 'description'], ['label' => 'Is Scheduled', 'field' => 'is_scheduled'], ['label' => 'Schedule', 'field' => 'schedule'], - ['label' => 'Expiration', 'field' => 'expiration_date'], + ['label' => 'Expiration', 'field' => 'expiration'], ]" :rows="$notifs" :hasActions="false" :addRoute="route('notification-create')" + :rowKey="'id'" />