diff --git a/app/Http/Controllers/Api/NotificationController.php b/app/Http/Controllers/Api/NotificationController.php index b27afe7..8aa6fcf 100644 --- a/app/Http/Controllers/Api/NotificationController.php +++ b/app/Http/Controllers/Api/NotificationController.php @@ -67,4 +67,11 @@ public function markAllAsRead(Request $request): JsonResponse return response()->json(['message' => 'All notifications marked as read.']); } + + public function deleteAll(Request $request): JsonResponse + { + $request->user()->notifications()->delete(); + + return response()->json(['message' => 'All notifications deleted.']); + } } diff --git a/resources/js/components/notifications/notification-bell.tsx b/resources/js/components/notifications/notification-bell.tsx index a3375cd..7a84a8d 100644 --- a/resources/js/components/notifications/notification-bell.tsx +++ b/resources/js/components/notifications/notification-bell.tsx @@ -1,5 +1,5 @@ import { router } from '@inertiajs/react'; -import { Bell, Check, Trash2 } from 'lucide-react'; +import { Bell, Check, CheckCheck, Trash2 } from 'lucide-react'; import { useCallback, useEffect, useRef, useState } from 'react'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; @@ -121,6 +121,22 @@ export function NotificationBell() { } }, []); + const deleteAll = useCallback(async () => { + try { + await fetch('/api/notifications', { + method: 'DELETE', + headers: { + 'X-Requested-With': 'XMLHttpRequest', + }, + }); + + setNotifications([]); + setUnreadCount(0); + } catch { + // Silently fail + } + }, []); + useEffect(() => { // eslint-disable-next-line react-hooks/set-state-in-effect -- initial data fetch is safe void fetchUnreadCount(); @@ -165,16 +181,34 @@ export function NotificationBell() {
Notifikasi - {unreadCount > 0 && ( - - )} +
+ {unreadCount > 0 && ( + + )} + {notifications.length > 0 && ( + + )} +
{notifications.length === 0 ? (
diff --git a/routes/api.php b/routes/api.php index 3d13250..f7b0071 100644 --- a/routes/api.php +++ b/routes/api.php @@ -41,3 +41,7 @@ Route::patch('/notifications/read-all', [NotificationController::class, 'markAllAsRead']) ->middleware(['web', 'auth', 'verified']) ->name('notifications.mark-all-as-read'); + +Route::delete('/notifications', [NotificationController::class, 'deleteAll']) + ->middleware(['web', 'auth', 'verified']) + ->name('notifications.delete-all');