diff --git a/app/Http/Controllers/NotificationController.php b/app/Http/Controllers/NotificationController.php new file mode 100644 index 0000000..e459dd3 --- /dev/null +++ b/app/Http/Controllers/NotificationController.php @@ -0,0 +1,127 @@ +route('login')->with('error', 'Please log in to view notifications.'); + } + + // Fetch all notifications directly from the API + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $accessToken, + ])->get("{$this->apiBaseUrl}/cms/notification"); + + $json = $response->json(); + Log::info("Response from index endpoint: ", $json); + + $notifications = []; + if ($response->status() === 401 || $response->status() === 403) { + Log::warning('Unauthorized or Forbidden API response for index endpoint: ', $json); + return redirect()->route('login')->with('error', 'Your session has expired. Please log in again.'); + } + + if ($response->successful() && isset($json['data']) && is_array($json['data'])) { + $notifications = array_map(function ($notification) { + return [ + 'id' => $notification['id'] ?? null, + 'subject' => $notification['subject'] ?? 'Untitled', + 'description' => $notification['description'] ?? '', + 'isScheduled' => $notification['trigger_schedule'] ? 'Scheduled' : 'Not Scheduled', + 'schedule' => $notification['trigger_schedule'] ?? '', + 'expiration' => $notification['expiration_date'] ?? '', + ]; + }, $json['data']); + } else { + Log::warning('Failed to fetch notifications: ', $json); + } + + // Update session with the fetched notification IDs (optional, for future use) + $notificationIds = array_column($notifications, 'id'); + Session::put('notification_ids', $notificationIds); + + return view('pages.notification', [ + 'notifications' => $notifications, + ])->with('error', empty($notifications) ? 'No notification data available. Please create a new notification.' : ''); + } catch (\Exception $e) { + Log::error('Error fetching notification data: ' . $e->getMessage()); + return view('pages.notification', [ + 'notifications' => [], + ])->with('error', 'An error occurred while fetching notifications: ' . $e->getMessage()); + } +} + public function create() + { + return view('pages.add-notification'); + } + + public function store(Request $request) + { + try { + $user = Session::get('user'); + $accessToken = $user['access_token'] ?? null; + + if (!$accessToken) { + return redirect()->route('login')->with('error', 'Please log in to add a notification.'); + } + + $data = [ + 'subject' => $request->input('subject'), + 'description' => $request->input('description'), + 'schedule' => $request->input('schedule') ?: null, + 'expiration' => $request->input('expiration') ?: null, + ]; + + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $accessToken, + ])->post("{$this->apiBaseUrl}/cms/notification", $data); + + if ($response->successful()) { + return redirect()->route('notification')->with('success', 'Notification created successfully.'); + } else { + Log::warning('Failed to create notification: ', $response->json()); + return redirect()->back()->with('error', $response->json()['message'] ?? 'Failed to create notification. Please try again.'); + } + } catch (\Exception $e) { + Log::error('Error creating notification: ' . $e->getMessage()); + return redirect()->back()->with('error', 'An error occurred while creating the notification: ' . $e->getMessage()); + } + } + + public function edit($id) + { + return redirect()->route('notification')->with('error', 'Edit functionality is not available due to backend limitations.'); + } + + public function update(Request $request, $id) + { + return redirect()->route('notification')->with('error', 'Update functionality is not available due to backend limitations.'); + } + + public function show($id) + { + return redirect()->route('notification')->with('error', 'View functionality is not available due to backend limitations.'); + } + + public function destroy($id) + { + return redirect()->route('notification')->with('error', 'Delete functionality is not available due to backend limitations.'); + } +} \ No newline at end of file diff --git a/resources/views/components/notification-component.blade.php b/resources/views/components/notification-component.blade.php new file mode 100644 index 0000000..22b99c5 --- /dev/null +++ b/resources/views/components/notification-component.blade.php @@ -0,0 +1,269 @@ +@props([ + 'pageTitle' => 'Notification', + 'data' => [], + 'columns' => [ + ['name' => 'ID', 'key' => 'id', 'sortable' => true], + ['name' => 'Subject', 'key' => 'subject', 'sortable' => true], + ['name' => 'Content', 'key' => 'description', 'sortable' => true], + ['name' => 'Is Scheduled', 'key' => 'isScheduled', 'sortable' => true], + ['name' => 'Schedule', 'key' => 'schedule', 'sortable' => true], + ['name' => 'Expiration', 'key' => 'expiration', 'sortable' => true], + ], + 'showAddButton' => true, + 'addButtonUrl' => route('notification.create'), +]) + +
+
+
+
{{ $pageTitle }}
+ @if ($showAddButton) + + Add {{ $pageTitle }} + + @endif +
+
+
+ +
+
+
+ + + + +
+
+
+ +
+
+ + +
+ + + + @foreach ($columns as $index => $column) + + @endforeach + + + +
+ {{ $column['name'] }} + @if ($column['sortable']) + + @endif +
+
+

No notifications found.

+
+
+
+
+ + + + + + \ No newline at end of file diff --git a/resources/views/pages/add-notification.blade.php b/resources/views/pages/add-notification.blade.php index 4744c18..7e2182e 100644 --- a/resources/views/pages/add-notification.blade.php +++ b/resources/views/pages/add-notification.blade.php @@ -3,133 +3,38 @@ @section('page_title', 'Add Notification') @section('content') -
-
Add Notification
-
- - -
- -
-
-
-
- - -
-
- - -
-
- - -
-
-
- - -
-
-
- - +
+
+
Add New Notification
- - - - - +
+ @if (session('error')) + + @endif +
+ @csrf +
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + Cancel +
+
+
@endsection \ No newline at end of file diff --git a/resources/views/pages/notification.blade.php b/resources/views/pages/notification.blade.php index 9fe23ac..3e948ba 100644 --- a/resources/views/pages/notification.blade.php +++ b/resources/views/pages/notification.blade.php @@ -3,42 +3,23 @@ @section('page_title', 'Notification') @section('content') - @php - $notifications = [ - ['id' => 1, 'subject' => 'Welcome Message', 'content' => 'Welcome to our platform! Get started today.', 'isScheduled' => 'Scheduled', 'schedule' => '2025-04-16 10:00', 'expiration' => '2025-04-30 23:59'], - ['id' => 2, 'subject' => 'System Update', 'content' => 'Scheduled maintenance on April 20th.', 'isScheduled' => 'Scheduled', 'schedule' => '2025-04-20 02:00', 'expiration' => '2025-04-21 02:00'], - ['id' => 3, 'subject' => 'Promotion Offer', 'content' => '50% off your next purchase this week!', 'isScheduled' => 'Not Scheduled', 'schedule' => '', 'expiration' => '2025-04-22 23:59'], - ['id' => 4, 'subject' => 'Account Reminder', 'content' => 'Please update your profile details.', 'isScheduled' => 'Scheduled', 'schedule' => '2025-04-18 09:00', 'expiration' => '2025-04-25 23:59'] - ]; - @endphp - - @include('components.table-component', [ - 'pageTitle' => 'Notification', - 'data' => $notifications, - 'columns' => [ - ['name' => 'ID', 'key' => 'id', 'sortable' => true], - ['name' => 'Subject', 'key' => 'subject', 'sortable' => true], - ['name' => 'Content', 'key' => 'content', 'sortable' => true], - ['name' => 'Is Scheduled', 'key' => 'isScheduled', 'sortable' => true], - ['name' => 'Schedule', 'key' => 'schedule', 'sortable' => true], - ['name' => 'Expiration', 'key' => 'expiration', 'sortable' => true] - ], - - 'showAddButton' => true, - 'addButtonUrl' => '/add-notification', - 'showCheckboxes' => false, - 'showBatchDelete' => false, - 'showEditModal' => false, - 'showViewModal' => true - ]) - - +
+ @if (session('success')) + + @endif + @if (session('error') || $errors->has('error')) + + @endif + +
@endsection \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index ba30758..92338b9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -6,6 +6,7 @@ use App\Http\Controllers\AuthController; use App\Http\Controllers\UserManagementController; use App\Http\Controllers\PhotoSliderController; use App\Http\Controllers\TopUpController; +use App\Http\Controllers\NotificationController; @@ -26,10 +27,6 @@ Route::get('/dashboard', function () { return view('dashboard'); }); -Route::get('/notification', function () { - return view('pages.notification'); -})->name('notification'); - Route::get('/card-member', function () { return view('pages.member management.card-member'); })->name('card-member'); @@ -181,3 +178,8 @@ Route::put('/photo-slider/{id}', [PhotoSliderController::class, 'update'])->name Route::get('/photo-slider/{id}', [PhotoSliderController::class, 'show'])->name('photo-slider.show'); Route::delete('/photo-slider/{id}', [PhotoSliderController::class, 'destroy'])->name('photo-slider.destroy'); Route::delete('/photo-slider/batch', [PhotoSliderController::class, 'batchDelete'])->name('photo-slider.batchDelete'); + +//Notification +Route::get('/notification', [NotificationController::class, 'index'])->name('notification'); +Route::get('/notification/create', [NotificationController::class, 'create'])->name('notification.create'); +Route::post('/notification', [NotificationController::class, 'store'])->name('notification.store'); \ No newline at end of file