notification count

This commit is contained in:
hamid zarghami
2025-12-21 15:05:23 +03:30
parent 2a1c81181e
commit 7cb14b0aa7
4 changed files with 28 additions and 4 deletions
+3 -2
View File
@@ -5,7 +5,7 @@ import { useTranslation } from 'react-i18next';
import { clx } from '../../helpers/utils'; import { clx } from '../../helpers/utils';
import StatusCircle from '../../components/StatusCircle'; import StatusCircle from '../../components/StatusCircle';
import { useOutsideClick } from '../../hooks/useOutSideClick'; import { useOutsideClick } from '../../hooks/useOutSideClick';
import { useGetNotification, useReadAll } from './hooks/useNotificationData'; import { useGetNotification, useGetUnseenCount, useReadAll } from './hooks/useNotificationData';
import { type NotificationItemType } from './types/NotificationTypes'; import { type NotificationItemType } from './types/NotificationTypes';
import { timeAgo } from '../../config/func'; import { timeAgo } from '../../config/func';
import InfiniteScroll from 'react-infinite-scroll-component'; import InfiniteScroll from 'react-infinite-scroll-component';
@@ -19,6 +19,7 @@ const Notifications: FC = () => {
const ref = useOutsideClick(() => setShowModal(false)); const ref = useOutsideClick(() => setShowModal(false));
const [activeTab, setActiveTab] = useState<'seen' | 'unseen'>('unseen'); const [activeTab, setActiveTab] = useState<'seen' | 'unseen'>('unseen');
const [showModal, setShowModal] = useState<boolean>(false); const [showModal, setShowModal] = useState<boolean>(false);
const { data: unseenCount } = useGetUnseenCount();
const { data, fetchNextPage, hasNextPage, refetch } = useGetNotification(activeTab); const { data, fetchNextPage, hasNextPage, refetch } = useGetNotification(activeTab);
const readAll = useReadAll() const readAll = useReadAll()
const posts = data?.pages.flatMap((page: { data: NotificationItemType[] }) => page.data || []) || []; const posts = data?.pages.flatMap((page: { data: NotificationItemType[] }) => page.data || []) || [];
@@ -82,7 +83,7 @@ const Notifications: FC = () => {
<div onClick={() => setShowModal(!showModal)} className='relative cursor-pointer'> <div onClick={() => setShowModal(!showModal)} className='relative cursor-pointer'>
<Notification size={18} color="black" /> <Notification size={18} color="black" />
<div className="absolute top-0 right-0 -mt-1 -mr-1 rounded-full bg-red-500 text-white text-[8px] size-3 flex pt-0.5 pl-[1px] justify-center items-center"> <div className="absolute top-0 right-0 -mt-1 -mr-1 rounded-full bg-red-500 text-white text-[8px] size-3 flex pt-0.5 pl-[1px] justify-center items-center">
{/* {getDashboard.data?.data?.notificationCount} */} {unseenCount?.data?.count}
</div> </div>
</div> </div>
@@ -1,5 +1,5 @@
import * as api from "../service/NotificationService"; import * as api from "../service/NotificationService";
import { useInfiniteQuery, useMutation } from "@tanstack/react-query"; import { useInfiniteQuery, useMutation, useQuery } from "@tanstack/react-query";
export const useGetNotification = (status: "all" | "seen" | "unseen") => { export const useGetNotification = (status: "all" | "seen" | "unseen") => {
return useInfiniteQuery({ return useInfiniteQuery({
@@ -18,3 +18,10 @@ export const useReadAll = () => {
mutationFn: () => api.readAll(), mutationFn: () => api.readAll(),
}); });
}; };
export const useGetUnseenCount = () => {
return useQuery({
queryKey: ["unseen-count"],
queryFn: () => api.getUnseenCount(),
});
};
@@ -1,5 +1,8 @@
import axios from "../../../config/axios"; import axios from "../../../config/axios";
import type { GetNotificationsResponse } from "../types/NotificationTypes"; import type {
GetNotificationsResponse,
GetUnseenCountResponse,
} from "../types/NotificationTypes";
export const getNotifications = async ( export const getNotifications = async (
cursor: string | undefined, cursor: string | undefined,
@@ -20,3 +23,10 @@ export const readAll = async () => {
const { data } = await axios.patch(`/admin/notifications/read-all`); const { data } = await axios.patch(`/admin/notifications/read-all`);
return data; return data;
}; };
export const getUnseenCount = async (): Promise<GetUnseenCountResponse> => {
const { data } = await axios.get<GetUnseenCountResponse>(
`/admin/notifications/unseen-count`
);
return data;
};
@@ -43,6 +43,12 @@ export type GetNotificationsResponse = IResponse<NotificationItemType[]> & {
nextCursor?: string; nextCursor?: string;
}; };
export type UnseenCountData = {
count: number;
};
export type GetUnseenCountResponse = IResponse<UnseenCountData>;
export enum NotificationTypeEnum { export enum NotificationTypeEnum {
USER_LOGIN = "USER_LOGIN", USER_LOGIN = "USER_LOGIN",
ANNOUNCEMENT = "ANNOUNCEMENT", ANNOUNCEMENT = "ANNOUNCEMENT",