Files
mtdb_movie/common/resources/client/notifications/requests/use-mark-notifications-as-read.ts
maher 703f50a09d
Some checks failed
Build / run (push) Has been cancelled
first commit
2025-10-29 11:42:25 +01:00

38 lines
1.2 KiB
TypeScript
Executable File

import {useMutation} from '@tanstack/react-query';
import {BackendResponse} from '../../http/backend-response/backend-response';
import {apiClient, queryClient} from '../../http/query-client';
import {useUserNotifications} from '../dialog/requests/user-notifications';
import {showHttpErrorToast} from '../../utils/http/show-http-error-toast';
import {useBootstrapData} from '../../core/bootstrap-data/bootstrap-data-context';
interface Response extends BackendResponse {
unreadCount: number;
}
interface Payload {
ids?: string[];
markAllAsUnread?: boolean;
}
export function useMarkNotificationsAsRead() {
const {data, mergeBootstrapData} = useBootstrapData();
return useMutation({
mutationFn: (props: Payload) => UseMarkNotificationsAsRead(props),
onSuccess: response => {
queryClient.invalidateQueries({queryKey: useUserNotifications.key});
if (response.unreadCount === 0) {
mergeBootstrapData({
user: {...data.user!, unread_notifications_count: 0},
});
}
},
onError: err => showHttpErrorToast(err),
});
}
function UseMarkNotificationsAsRead(payload: Payload): Promise<Response> {
return apiClient
.post('notifications/mark-as-read', payload)
.then(r => r.data);
}