feat: mark all as read (#1982)

This commit is contained in:
Aaryan Khandelwal 2023-08-28 13:26:38 +05:30 committed by GitHub
parent 47abe9db5e
commit a1acd2772e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 16 deletions

View file

@ -9,11 +9,14 @@ import useSWRInfinite from "swr/infinite";
// services
import userNotificationServices from "services/notifications.service";
// hooks
import useToast from "./use-toast";
// fetch-keys
import { UNREAD_NOTIFICATIONS_COUNT, getPaginatedNotificationKey } from "constants/fetch-keys";
// type
import type { NotificationType, NotificationCount } from "types";
import type { NotificationType, NotificationCount, IMarkAllAsReadPayload } from "types";
const PER_PAGE = 30;
@ -21,6 +24,8 @@ const useUserNotification = () => {
const router = useRouter();
const { workspaceSlug } = router.query;
const { setToastAlert } = useToast();
const [snoozed, setSnoozed] = useState<boolean>(false);
const [archived, setArchived] = useState<boolean>(false);
const [readNotification, setReadNotification] = useState<boolean>(false);
@ -274,6 +279,29 @@ const useUserNotification = () => {
}
};
const markAllNotificationsAsRead = async () => {
if (!workspaceSlug) return;
let markAsReadParams: IMarkAllAsReadPayload;
if (snoozed) markAsReadParams = { archived: false, snoozed: true };
else if (archived) markAsReadParams = { archived: true, snoozed: false };
else markAsReadParams = { archived: false, snoozed: false, type: selectedTab };
await userNotificationServices
.markAllNotificationsAsRead(workspaceSlug.toString(), markAsReadParams)
.catch(() => {
setToastAlert({
type: "error",
title: "Error!",
message: "Something went wrong. Please try again.",
});
})
.finally(() => {
notificationMutate();
});
};
return {
notifications,
notificationMutate,
@ -304,6 +332,7 @@ const useUserNotification = () => {
isRefreshing,
setFetchNotifications,
markNotificationAsRead,
markAllNotificationsAsRead,
};
};