unioil-cms-fe/app/Livewire/Notification.php

63 lines
1.8 KiB
PHP

<?php
namespace App\Livewire;
use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
class Notification extends Component
{
public $notifs = [];
public function mount()
{
$this->loadNotifications();
}
// Get filtered notifications based on the search input
public function loadNotifications()
{
try {
$token = Session::get('user')['access_token'] ?? null;
if (!$token) {
$this->addError('users', 'No access token found.');
return;
}
$response = Http::withToken($token)
->get(config('services.backend_api.url') . '/api/cms/notification');
// dd($response->json());
if ($response->successful()) {
// Properly use collect to handle the response data
$this->notifs = collect($response->json()['data']['notifications'])->map(function ($notifs) {
return [
'id' => $notifs['id'],
'subject' => $notifs['subject'],
'content' => $notifs['description'],
'is_scheduled' => $notifs['trigger_schedule'],
'schedule' => $notifs['schedule']?? '-',
'expiration' => $notifs['expiration_date'],
];
});
} else {
$this->addError('users', 'Failed to load notifications.');
}
} catch (\Exception $e) {
$this->addError('users', 'Error: ' . $e->getMessage());
}
}
public function render()
{
return view('livewire.notification.notification', [
'notification' => $this->notifs, // Pass filtered notifs here
]);
}
}