diff --git a/app/Livewire/Components/Table.php b/app/Livewire/Components/Table.php index 8fcc3aa..b52e64a 100644 --- a/app/Livewire/Components/Table.php +++ b/app/Livewire/Components/Table.php @@ -23,11 +23,13 @@ class Table extends Component public $sortDirection = 'asc'; public bool $hasSearch = true; + public $rowKey = 'id'; + public $startDate; public $endDate; // Pagination Configuration - public $perPage = 5; + public $perPage = 10; public $page = 1; public $renderKey = 0; // Dummy property to force re-render @@ -36,23 +38,26 @@ class Table extends Component public $modalMode = 'view'; public $modalData = []; - public function mount($columns, $rows, $addRoute = null) - { - $this->columns = $columns; - // Convert $rows to a plain array to ensure consistent pagination - $this->rows = collect($rows)->map(function ($row) { - return is_array($row) ? $row : (array) $row; - })->values()->toArray(); - $this->addRoute = $addRoute; - Log::info("Initial rows count: " . count($this->rows)); - } +public function mount($columns, $rows, $addRoute = null, $rowKey = 'id') +{ + $this->columns = $columns; + $this->rows = collect($rows)->map(function ($row) { + return is_array($row) ? $row : (array) $row; + })->values()->all(); + $this->addRoute = $addRoute; + $this->rowKey = $rowKey; + Log::info("Initial rows count: " . count($this->rows)); +} - public function viewRow($id) - { - $this->modalData = collect($this->rows)->firstWhere('id', $id) ?? []; - $this->modalMode = 'view'; - $this->showModal = true; - } + +public function viewRow($id) +{ + $this->modalData = collect($this->rows)->firstWhere($this->rowKey, $id) ?? []; + $this->modalMode = 'view'; + $this->showModal = true; +} + + public function editRow($id) { diff --git a/app/Livewire/LoginForm.php b/app/Livewire/LoginForm.php index 70b9d1a..9dbd7e4 100644 --- a/app/Livewire/LoginForm.php +++ b/app/Livewire/LoginForm.php @@ -25,12 +25,19 @@ class LoginForm extends Component ]); $json = $response->json(); + // dd($json); + if ($response->successful()) { if ($json['code'] === 200) { - Session::put('user', $json['data']['user'] ?? null); + Session::put('user', [ + 'admin' => $json['data']['admin'] ?? null, + 'access_token' => $json['data']['token'] ?? null, + ]); + // dd(Session::get('user')); // right before redirect + - return redirect('/main/profile'); + return $this->redirect('/main/profile'); } else { $this->addError('username', $json['message'] ?? 'Login failed.'); } @@ -49,59 +56,4 @@ class LoginForm extends Component return view('livewire.auth.login-form'); // This will point to the resource/views/livewire/auth/login-form.blade.php component } -} - - - -// validate([ -// 'username' => 'required|string', -// 'password' => 'required|string', -// ]); - -// try { -// $response = Http::post(config('services.backend_api.url') . '/api/cms/login_password', [ -// 'username' => $this->username, -// 'password' => $this->password, -// ]); - -// $json = $response->json(); - -// if ($response->successful()) { -// if ($json['code'] === 200) { -// Session::put('admin_uuid', $json['data']['admin_uuid'] ?? null); - -// return redirect('/main/profile'); -// } else { -// $this->addError('username', $json['message'] ?? 'Login failed.'); -// } -// } else { -// $message = $json['message'] ?? 'Login request failed. Please try again.'; -// $this->addError('username', $message); -// } -// } catch (\Exception $e) { -// $this->addError('username', 'An error occurred: ' . $e->getMessage()); -// } - -// } - -// public function render() -// { -// return view('livewire.auth.login-form'); // This will point to the resource/views/livewire/auth/login-form.blade.php component -// } - -// } \ No newline at end of file +} \ No newline at end of file diff --git a/app/Livewire/Notification.php b/app/Livewire/Notification.php index 380e407..0162b63 100644 --- a/app/Livewire/Notification.php +++ b/app/Livewire/Notification.php @@ -3,6 +3,9 @@ namespace App\Livewire; use Livewire\Component; +use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Session; + class Notification extends Component { public $notifs = []; @@ -13,10 +16,40 @@ class Notification extends Component $this->loadNotifications(); } - // Get filtered users based on the search input + // Get filtered notifications based on the search input public function loadNotifications() { - $this->notifs = collect(json_decode(file_get_contents(storage_path('app/notifs.json')), true)); + 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()); + } } diff --git a/app/Livewire/Profile.php b/app/Livewire/Profile.php index f301aeb..7c461e1 100644 --- a/app/Livewire/Profile.php +++ b/app/Livewire/Profile.php @@ -13,14 +13,15 @@ class Profile extends Component public function mount() { $this->user = Session::get('user'); - - if (!$this->user) { - return $this->redirect('/login'); // Livewire 3 way - } } public function render() { + + if (!$this->user) { + return $this->redirect('/login'); + } + return view('livewire.profile.profile'); } } \ No newline at end of file diff --git a/app/Livewire/UserManagement.php b/app/Livewire/UserManagement.php index da3760b..3918ba6 100644 --- a/app/Livewire/UserManagement.php +++ b/app/Livewire/UserManagement.php @@ -1,9 +1,10 @@ loadUsers(); // Load users initially + $this->loadUsers(); } public function loadUsers() { - $this->users = collect(json_decode(file_get_contents(storage_path('app/users.json')), true)); + 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/admin'); + + // dd($response->json()); + if ($response->successful()) { + // Properly use collect to handle the response data + $this->users = collect($response->json()['data'])->map(function ($user) { + return [ + 'admin_uuid' => $user['admin_uuid'], + 'username' => $user['username'], + 'firstname' => $user['firstname'], + 'lastname' => $user['lastname'], + 'email' => $user['email'], + 'role' => $user['role'], + 'status' => $user['status'], + ]; + }); + } else { + $this->addError('users', 'Failed to load users.'); + } + } catch (\Exception $e) { + $this->addError('users', 'Error: ' . $e->getMessage()); + } } public function render() @@ -26,3 +57,4 @@ class UserManagement extends Component ]); } } + diff --git a/config/session.php b/config/session.php index ba0aa60..062192f 100644 --- a/config/session.php +++ b/config/session.php @@ -18,7 +18,7 @@ return [ | */ - 'driver' => env('SESSION_DRIVER', 'database'), + 'driver' => env('SESSION_DRIVER', 'file'), /* |-------------------------------------------------------------------------- diff --git a/resources/views/livewire/components/table.blade.php b/resources/views/livewire/components/table.blade.php index 1b5a234..8c11a61 100644 --- a/resources/views/livewire/components/table.blade.php +++ b/resources/views/livewire/components/table.blade.php @@ -82,16 +82,15 @@
- @php - \Illuminate\Support\Facades\Log::info("Blade rows count: " . count($rows) . ", First Username: " . ($rows[0]['username'] ?? 'N/A')); - @endphp @forelse ($rows as $index => $row) -