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;
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()
{

View File

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

View File

@ -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 = [];
}

View File

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

View File

@ -30,13 +30,13 @@
<!-- Subject -->
<div class="flex items-center gap-2">
<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>
<!-- Content -->
<div class="flex items-start gap-2">
<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>
@ -44,13 +44,26 @@
<div class="flex items-center gap-4">
<label class="w-40">Schedule:</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>
<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>
</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 -->
<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>

View File

@ -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'"
/>
</div>