notification crud added

This commit is contained in:
erishBRBS 2025-05-21 21:17:39 +08:00
parent b09ac700aa
commit 970119760b
6 changed files with 74 additions and 16 deletions

View File

@ -3,18 +3,64 @@
namespace App\Livewire\Buttons; namespace App\Livewire\Buttons;
use Livewire\Component; use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
class CreateNotification extends Component class CreateNotification extends Component
{ {
public $subject;
public $description;
public $set_schedule = 'no';
public $schedule;
public $expiration;
public $status = 0;
public function submit() 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() public function cancel()
{ {
return redirect()->to('/main/notification'); return redirect()->to('/main/notification');
} }
public function render() public function render()
{ {

View File

@ -65,8 +65,6 @@ class CreateUser extends Component
'password' => $this->default_password, 'password' => $this->default_password,
]); ]);
// dd($response->json());
//handle backend response //handle backend response
if ($response->status() === 422) { if ($response->status() === 422) {

View File

@ -170,7 +170,7 @@ public function deleteConfirmed()
{ {
$currentRows = $this->getPaginatedRows(); $currentRows = $this->getPaginatedRows();
if ($this->selectAll) { if ($this->selectAll) {
$this->selected = collect($currentRows)->pluck('id')->toArray(); $this->selected = collect($currentRows)->pluck($this->rowKey)->toArray();
} else { } else {
$this->selected = []; $this->selected = [];
} }

View File

@ -38,10 +38,10 @@ class Notification extends Component
return [ return [
'id' => $notifs['id'], 'id' => $notifs['id'],
'subject' => $notifs['subject'], 'subject' => $notifs['subject'],
'content' => $notifs['description'], 'description' => $notifs['description'],
'is_scheduled' => $notifs['trigger_schedule'], 'schedule' => $notifs['trigger_schedule']?? '-',
'schedule' => $notifs['schedule']?? '-', 'expiration' => $notifs['expiration_date']?? '-',
'expiration' => $notifs['expiration_date'], 'is_scheduled' => !empty($notifs['trigger_schedule']) ? 'Yes' : 'No',
]; ];
}); });
} else { } else {

View File

@ -30,13 +30,13 @@
<!-- Subject --> <!-- Subject -->
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<label class="w-40">Subject:</label> <label class="w-40">Subject:</label>
<input type="text" wire:model="last_name" class="flex-1 border rounded px-3 py-2" placeholder="Subject"> <input type="text" wire:model="subject" class="flex-1 border rounded px-3 py-2" placeholder="Subject">
</div> </div>
<!-- Content --> <!-- Content -->
<div class="flex items-start gap-2"> <div class="flex items-start gap-2">
<label class="w-40 pt-2">Content:</label> <label class="w-40 pt-2">Content:</label>
<textarea wire:model="email" class="flex-1 border rounded px-3 py-2" rows="4" placeholder="Enter your content here..."></textarea> <textarea wire:model="description" class="flex-1 border rounded px-3 py-2" rows="4" placeholder="Enter your content here..."></textarea>
</div> </div>
@ -44,13 +44,26 @@
<div class="flex items-center gap-4"> <div class="flex items-center gap-4">
<label class="w-40">Schedule:</label> <label class="w-40">Schedule:</label>
<label> <label>
<input type="radio" wire:model="schecule" name="schedule" value="active" class="mr-1"> Yes <input type="radio" wire:model="set_schedule" wire:click="$set('set_schedule', 'no')" name="schedule" value="no" class="mr-1"> No
</label> </label>
<label> <label>
<input type="radio" wire:model="schedule" name="schedule" value="inactive" class="mr-1"> No <input type="radio" wire:click="$set('set_schedule', 'yes')" name="schedule" value="yes" class="mr-1"> Yes
</label> </label>
</div> </div>
@if($set_schedule === 'yes')
<div class="flex items-center gap-2 mt-4 transition duration-300 ease-in-out">
<label class="w-40">Schedule Date:</label>
<input type="datetime-local" wire:model="schedule" class="flex-1 border rounded px-3 py-2 hover:border-orange-400">
</div>
<div class="flex items-center gap-2 mt-2 transition duration-300 ease-in-out">
<label class="w-40">Expiration Date:</label>
<input type="datetime-local" wire:model="expiration" class="flex-1 border rounded px-3 py-2 hover:border-orange-400">
</div>
@endif
<!-- Submit / Cancel --> <!-- Submit / Cancel -->
<div class="mt-6 flex justify-end gap-2"> <div class="mt-6 flex justify-end gap-2">
<button wire:click="cancel" class="px-4 py-2 bg-gray-300 text-black rounded hover:bg-gray-400">Cancel</button> <button wire:click="cancel" class="px-4 py-2 bg-gray-300 text-black rounded hover:bg-gray-400">Cancel</button>

View File

@ -7,14 +7,15 @@
:columns="[ :columns="[
['label' => 'ID', 'field' => 'id'], ['label' => 'ID', 'field' => 'id'],
['label' => 'Subject', 'field' => 'subject'], ['label' => 'Subject', 'field' => 'subject'],
['label' => 'Content', 'field' => 'content'], ['label' => 'Content', 'field' => 'description'],
['label' => 'Is Scheduled', 'field' => 'is_scheduled'], ['label' => 'Is Scheduled', 'field' => 'is_scheduled'],
['label' => 'Schedule', 'field' => 'schedule'], ['label' => 'Schedule', 'field' => 'schedule'],
['label' => 'Expiration', 'field' => 'expiration_date'], ['label' => 'Expiration', 'field' => 'expiration'],
]" ]"
:rows="$notifs" :rows="$notifs"
:hasActions="false" :hasActions="false"
:addRoute="route('notification-create')" :addRoute="route('notification-create')"
:rowKey="'id'"
/> />
</div> </div>