import { noCase } from 'change-case'; import { useState, useEffect, useContext } from 'react'; // @mui import { Box, List, Badge, Button, Avatar, Tooltip, Divider, Typography, ListItemText, ListSubheader, ListItemAvatar, ListItemButton, Stack } from '@mui/material'; // utils import { fToNow } from '@/utils/formatTime'; // _mock_ //import { _notifications } from '@/_mock'; // components import Iconify from '@/components/Iconify'; import Scrollbar from '@/components/Scrollbar'; import MenuPopover from '@/components/MenuPopover'; import { IconButtonAnimate } from '@/components/animate'; import axios from '@/utils/axios'; import { useSnackbar } from 'notistack'; import { LanguageContext } from '@/contexts/LanguageContext'; // ---------------------------------------------------------------------- export default function NotificationsPopover() { const { localeData }: any = useContext(LanguageContext); const [notifications, setNotifications] = useState([]); const {enqueueSnackbar} = useSnackbar(); const getDataNotifications = async () => { axios .get('notifications/1') .then((response) => { setNotifications(response.data.data.notifications); }) .catch((error) => { enqueueSnackbar(error.response.data.meta.message, {variant : "error"}); }); }; useEffect(() => { getDataNotifications(); }, []); const totalUnRead = notifications.filter((item) => item.isUnRead === 1).length; const [open, setOpen] = useState(null); const handleOpen = (event: React.MouseEvent) => { setOpen(event.currentTarget); }; const handleClose = () => { setOpen(null); }; const handleMarkAllAsRead = () => { setNotifications( notifications.map((notification) => ({ ...notification, isUnRead: 0, })) ); }; return ( <> {localeData.txtNotifications} {localeData.txtYouHave} {totalUnRead} {localeData.txtUnm} {/* {totalUnRead > 0 && ( )} */} {notifications && notifications.length > 0 ? ( <> {localeData.txtNew} } > {notifications.slice(0, 2).map((notification) => ( ))} {localeData.txtBeforeThat} } > {notifications.slice(2, 5).map((notification) => ( ))} ): ''} {/* */} ); } // ---------------------------------------------------------------------- type NotificationItemProps = { id: string; title: string; description: string; avatar: string | null; type: string; createdAt: Date; isUnRead: boolean; }; function NotificationItem({ notification, getDataNotifications }: { notification: NotificationItemProps, getDataNotifications: () => void}) { const { avatar, title } = renderContent(notification); const {enqueueSnackbar} = useSnackbar(); const handleClick = () => { const data = { 'user_id' : 1, 'id' : notification.id, }; if(notification.isUnRead) { axios .post('set-read-notification', data) .then((response) => { enqueueSnackbar(response.data.meta.message, {variant : "success"}); getDataNotifications(); }) .catch((error) => { enqueueSnackbar(error.response.data.meta.message, {variant : "error"}); }); } }; return ( {avatar} {fToNow(notification.createdAt)} } /> ); } // ---------------------------------------------------------------------- function renderContent(notification: NotificationItemProps) { const title = ( {notification.title} {notification.description} ); if (notification.type === 'order_placed') { return { avatar: ( {notification.title} ), title, }; } if (notification.type === 'order_shipped') { return { avatar: ( {notification.title} ), title, }; } if (notification.type === 'mail' || notification.type === 'request_document' || notification.type === 'grab_status') { return { avatar: ( {notification.title} ), title, }; } if (notification.type === 'chat_message') { return { avatar: ( {notification.title} ), title, }; } return { avatar: notification.avatar ? {notification.title} : null, title, }; }