add translate, add remove notification function

This commit is contained in:
smanylov
2026-04-07 12:45:09 +07:00
parent 8048c0ff0e
commit c57685065a
4 changed files with 98 additions and 17 deletions
+15 -17
View File
@@ -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 (
<div
className="notifications-list-wrapper"
@@ -150,10 +148,10 @@ export function NotificationsList() {
className="btn btn-primary"
onClick={() => {
const array = Array.from(selectedFiles);
markAsReadHandler(array);
notificationActionHandler(array, 'mark-as-read');
}}
>
{t('mark-all-as-read')}
{t('mark-as-read')}
</button>
</>
)}
@@ -225,7 +223,7 @@ export function NotificationsList() {
<button
className="btn btn-remove"
onClick={() => {
removeHandler(item.id);
notificationActionHandler([item.id], 'remove');
}}
disabled={loadingIds.has(item.id)}
>