import { Notification } from 'iconsax-react'; import { FC, useState } from 'react'; import XIcon from '../../assets/images/close-circle.svg'; import { useTranslation } from 'react-i18next'; import { clx } from '../../helpers/utils'; import StatusCircle from '../../components/StatusCircle'; import { useOutsideClick } from '../../hooks/useOutSideClick'; import { useGetNotification, useReadAll } from './hooks/useNotificationData'; import { NotificationItemType, NotificationTypeEnum } from './types/NotificationTypes'; import { timeAgo } from '../../config/func'; import InfiniteScroll from 'react-infinite-scroll-component'; import MoonLoader from "react-spinners/MoonLoader" import Button from '../../components/Button'; import { toast } from '../../components/Toast'; import { useGetDashboardSummary } from '../home/hooks/useHomeData'; import { useNavigate } from 'react-router-dom'; import { Pages } from '../../config/Pages'; import { ErrorType } from '../../helpers/types'; const Notifications: FC = () => { const navigate = useNavigate() const { t } = useTranslation('global'); const ref = useOutsideClick(() => setShowModal(false)); const [activeTab, setActiveTab] = useState<'read' | 'unread'>('unread'); const [showModal, setShowModal] = useState(false); const { data, fetchNextPage, hasNextPage, refetch } = useGetNotification(activeTab); const readAll = useReadAll() const posts = data?.pages.flatMap((page) => page.data?.notifications) || []; const getDashboard = useGetDashboardSummary() const handleAllRead = () => { readAll.mutate(undefined, { onSuccess: () => { getDashboard.refetch() refetch() toast(t('success'), 'success') }, onError: (error: ErrorType) => { toast(error.response?.data?.error?.message[0], 'error') } }) } const handleRedirect = (type: NotificationTypeEnum) => { switch (type) { case NotificationTypeEnum.USER_LOGIN: break; case NotificationTypeEnum.ANNOUNCEMENT: navigate(Pages.announcement.list) break; case NotificationTypeEnum.WALLET_CHARGE: navigate(Pages.transactions) break; case NotificationTypeEnum.WALLET_DEDUCTION: navigate(Pages.transactions) break; case NotificationTypeEnum.BILL_INVOICE_REMINDER: navigate(Pages.receipts.index) break; case NotificationTypeEnum.BILL_INVOICE: navigate(Pages.receipts.index) break; case NotificationTypeEnum.CREATE_INVOICE: navigate(Pages.receipts.index) break; case NotificationTypeEnum.CREATE_SERVICE: navigate(Pages.services.other) break; case NotificationTypeEnum.UNBLOCK_SERVICE: navigate(Pages.services.other) break; case NotificationTypeEnum.BLOCK_SERVICE: navigate(Pages.services.other) break; case NotificationTypeEnum.ANSWER_TICKET: navigate(Pages.ticket.list) break; case NotificationTypeEnum.CREATE_TICKET: navigate(Pages.ticket.list) break; default: break } setShowModal(false) } return (
setShowModal(!showModal)} className='relative cursor-pointer'>
{getDashboard.data?.data?.notificationCount}
{showModal && (
{t('notif.natification')}
close setShowModal(false)} />
setActiveTab('unread')} className={clx( 'flex-1 border-l cursor-pointer text-white border-white border-opacity-70 bg-transparent flex justify-center items-center', activeTab === 'unread' ? 'bg-white bg-opacity-70 text-black' : '' )} > خوانده نشده
setActiveTab('read')} className={clx( 'flex-1 cursor-pointer text-white border-white bg-transparent flex justify-center items-center', activeTab === 'read' ? 'bg-white bg-opacity-70 text-black' : '' )} > خوانده شده
} scrollableTarget="notificationsContainer" > { posts.map((item: NotificationItemType) => (
handleRedirect(item.type)} className="bg-white cursor-pointer h-fit gap-4 items-start rounded-xl bg-opacity-60 p-3 mb-4 flex" key={item.id}> { !item.isRead &&
}
{item.message}
{timeAgo(item.createdAt)}
{item.title}
)) }
)} ); }; export default Notifications;