diff --git a/src/app/actions/notificationActions.ts b/src/app/actions/notificationActions.ts index 5fe31bb..f500f33 100644 --- a/src/app/actions/notificationActions.ts +++ b/src/app/actions/notificationActions.ts @@ -128,3 +128,46 @@ export async function notificationsMarkAsRead(ids: number[]) { } } } + +export async function notificationsDelete(ids: number[]) { + const token = await getSessionData('token'); + console.log('notificationsDelete'); + + try { + const response = await fetch(`${API_BASE_URL}/api/v1/data`, { + method: 'POST', + body: JSON.stringify({ + version: 1, + msg_id: 30015, + message_body: { + action: 'delete', + authToken: token, + notificationIds: ids + } + }), + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + } + }); + + + if (response.ok) { + let parsed = await response.json(); + console.log('parsed'); + console.log(parsed); + + if (parsed?.message_code === 0) { + return parsed?.message_body + } else { + throw parsed; + } + } else { + throw (`${response.status}`); + } + } catch (error) { + return { + success: false + } + } +} diff --git a/src/app/ui/notifications/notifications-list.tsx b/src/app/ui/notifications/notifications-list.tsx index 69bc082..882d97a 100644 --- a/src/app/ui/notifications/notifications-list.tsx +++ b/src/app/ui/notifications/notifications-list.tsx @@ -4,7 +4,7 @@ import { useTranslations } from 'next-intl'; import { IconCheck, IconArrowLeft, IconArrowRight, IconDoubleArrowLeft, IconDoubleArrowRight } from '@/app/ui/icons/icons'; import { useEffect, useState } from 'react'; import { useAllNotifications } from '@/app/hooks/react-query/useAllNotificationsList'; -import { notificationsMarkAsRead } from '@/app/actions/notificationActions'; +import { notificationsMarkAsRead, notificationsDelete } from '@/app/actions/notificationActions'; import { useQueryClient } from '@tanstack/react-query'; import { formatDate, formatDateTime } from '@/app/lib/formatDate'; @@ -81,7 +81,7 @@ export function NotificationsList() { setSelectedFiles(new Set(allNotifications?.notifications.map(item => item.id))); } - async function markAsReadHandler(ids: number[]) { + async function notificationActionHandler(ids: number[], action: 'remove' | 'mark-as-read') { setLoadingIds(prev => { const newSet = new Set(prev); ids.forEach(id => newSet.add(id)); @@ -89,14 +89,21 @@ export function NotificationsList() { }); try { - const response = await notificationsMarkAsRead(ids); + let response; - if (response.success) { + if (action === 'mark-as-read') { + response = await notificationsMarkAsRead(ids); + } else if (action === 'remove') { + response = await notificationsDelete(ids); + } + + if (response?.success) { await queryClient.invalidateQueries({ queryKey: ['activeNotifications'] }); await queryClient.invalidateQueries({ queryKey: ['allNotifications'] }); } } catch (error) { - setError(error instanceof Error ? error.message : 'Failed to mark as read'); + const errorMessage = action === 'mark-as-read' ? 'Failed to mark as read' : 'Failed to remove'; + setError(errorMessage); } finally { setLoadingIds(prev => { const newSet = new Set(prev); @@ -112,15 +119,6 @@ export function NotificationsList() { } } - function removeHandler(id: number) { - console.log(id); - - setSelectedFiles(prev => { - prev.delete(id) - return new Set(prev); - }); - } - return (
{ const array = Array.from(selectedFiles); - markAsReadHandler(array); + notificationActionHandler(array, 'mark-as-read'); }} > - {t('mark-all-as-read')} + {t('mark-as-read')} )} @@ -225,7 +223,7 @@ export function NotificationsList() {