Skip to content

Commit

Permalink
Merge pull request #5126 from novuhq/nv-3414-bulk-delete-notification…
Browse files Browse the repository at this point in the history
…s-in-notification-center-hooks

feat(notification-center): add use remove notifications hook
  • Loading branch information
djabarovgeorge authored Feb 1, 2024
2 parents aa9f323 + 1fbbb8c commit 563190c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/notification-center/src/hooks/useRemoveNotifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useMutation, useQueryClient, UseMutationOptions } from '@tanstack/react-query';

import { useNovuContext } from './useNovuContext';
import { useFetchNotificationsQueryKey } from './useFetchNotificationsQueryKey';

interface IRemoveNotificationsVariables {
messageIds: string[];
}

export type ResponseDataType = Record<string, never>;

export const useRemoveNotifications = ({
onSuccess,
...options
}: {
onSuccess?: () => void;
} & UseMutationOptions<ResponseDataType, Error, IRemoveNotificationsVariables> = {}) => {
const queryClient = useQueryClient();
const { apiService } = useNovuContext();
const fetchNotificationsQueryKey = useFetchNotificationsQueryKey();

const { mutate, ...result } = useMutation<ResponseDataType, Error, IRemoveNotificationsVariables>(
({ messageIds }) => apiService.removeMessages(messageIds),
{
...options,
onSuccess: (data, variables, context) => {
queryClient.refetchQueries(fetchNotificationsQueryKey, { exact: false });
onSuccess?.(data, variables, context);
},
}
);

return { ...result, removeNotifications: mutate };
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useMarkNotificationsAsRead } from '../hooks/useMarkNotificationAsRead';
import { useMarkNotificationsAsSeen } from '../hooks/useMarkNotificationAsSeen';
import { useStore } from '../hooks/useStore';
import { StoreProvider } from './store-provider.context';
import { useRemoveNotifications } from '../hooks/useRemoveNotifications';

const DEFAULT_STORES = [{ storeId: 'default_store' }];

Expand Down Expand Up @@ -47,6 +48,7 @@ function NotificationsProviderInternal({ children }: { children: React.ReactNode
const { data: unreadCountData } = useUnreadCount();
const { markNotificationsAs } = useMarkNotificationsAs();
const { removeNotification } = useRemoveNotification();
const { removeNotifications } = useRemoveNotifications();
const { removeAllNotifications } = useRemoveAllNotifications();
const { markNotificationsAsRead } = useMarkNotificationsAsRead();
const { markNotificationsAsSeen } = useMarkNotificationsAsSeen();
Expand All @@ -61,6 +63,10 @@ function NotificationsProviderInternal({ children }: { children: React.ReactNode
[markNotificationsAs]
);
const removeMessage = useCallback((messageId: string) => removeNotification({ messageId }), [removeNotification]);
const removeMessages = useCallback(
(messageIds: string[]) => removeNotifications({ messageIds }),
[removeNotifications]
);
const removeAllMessages = useCallback(
(feedId?: string) => removeAllNotifications({ feedId }),
[removeAllNotifications]
Expand Down Expand Up @@ -138,6 +144,7 @@ function NotificationsProviderInternal({ children }: { children: React.ReactNode
markFetchedNotificationsAsRead,
markFetchedNotificationsAsSeen,
removeMessage,
removeMessages,
removeAllMessages,
markAllNotificationsAsRead,
markAllNotificationsAsSeen,
Expand All @@ -162,6 +169,7 @@ function NotificationsProviderInternal({ children }: { children: React.ReactNode
markFetchedNotificationsAsRead,
markFetchedNotificationsAsSeen,
removeMessage,
removeMessages,
removeAllMessages,
markAllNotificationsAsRead,
markAllNotificationsAsSeen,
Expand Down

0 comments on commit 563190c

Please sign in to comment.