Commit f8ad2c84 by Arjun Jhukal

updated the notification read functionality

parent 0f9f3779
...@@ -17,6 +17,10 @@ import { Notification } from '@wandersonalwes/iconsax-react'; ...@@ -17,6 +17,10 @@ import { Notification } from '@wandersonalwes/iconsax-react';
import Link from 'next/link'; import Link from 'next/link';
import { NotificationProps } from '@/types/notification'; import { NotificationProps } from '@/types/notification';
import { Pagination } from '@/types/game'; import { Pagination } from '@/types/game';
import { useReadNotificationMutation } from '@/services/notificationApi';
import { useAppDispatch } from '@/hooks/hook';
import { showToast, ToastVariant } from '@/slice/toastSlice';
import { formatDateTime } from '@/utils/formatDateTime';
export default function NotificationPage({ export default function NotificationPage({
...@@ -37,8 +41,28 @@ export default function NotificationPage({ ...@@ -37,8 +41,28 @@ export default function NotificationPage({
}; };
const id = open ? 'popper' : undefined; const id = open ? 'popper' : undefined;
const dispatch = useAppDispatch();
// const [readNotification,{isLoading}] const [readNotification, { isLoading }] = useReadNotificationMutation();
const handleNotificationClick = async (id: string) => {
try {
const response = await readNotification({ id }).unwrap();
// dispatch(
// showToast({
// message: "Notification read successfully",
// variant: ToastVariant.SUCCESS
// })
// )
}
catch (e: any) {
dispatch(
showToast({
message: e.message || "Unable to read notification",
variant: ToastVariant.ERROR
})
)
}
}
return ( return (
<Box> <Box>
<IconButton <IconButton
...@@ -48,7 +72,7 @@ export default function NotificationPage({ ...@@ -48,7 +72,7 @@ export default function NotificationPage({
className="!bg-light-gray" className="!bg-light-gray"
> >
<Badge <Badge
badgeContent={notifications.length} badgeContent={notifications.filter((item) => item.has_read === false).length}
color="success" color="success"
sx={{ '& .MuiBadge-badge': { top: 2, right: 4 } }} sx={{ '& .MuiBadge-badge': { top: 2, right: 4 } }}
> >
...@@ -86,11 +110,15 @@ export default function NotificationPage({ ...@@ -86,11 +110,15 @@ export default function NotificationPage({
notifications.length ? ( notifications.length ? (
<List className='max-h-[320px] overflow-auto px-1'> <List className='max-h-[320px] overflow-auto px-1'>
{ {
notifications.map((notification, index) => ( notifications.map((notification, index) => {
<ListItem className={`border-b-solid border-b-gray-100 border-b-[1px] rounded-sm !p-2 cursor-pointer ${notification.has_read ? "" : "bg-gray-100"} ${index > 0 ? "mb-2 " : ""}`} key={notification.id}> const { date, time } = formatDateTime(notification.created_at);
return (
<ListItem className={`border-b-solid border-b-gray-100 border-b-[1px] rounded-sm !p-2 cursor-pointer ${notification.has_read ? "" : "bg-gray-100"} ${index > 0 ? "mb-2 " : ""}`} key={notification.id} onClick={() => handleNotificationClick(notification.id)}>
<p className='text-[12px] lg:text-[14px] leading-[120%] text-title'>{notification.message}</p> <p className='text-[12px] lg:text-[14px] leading-[120%] text-title'>{notification.message}</p>
<p className='text-[8px] mt-1 lg:text-[14px] leading-[120%] text-para-light'>{date}</p>
</ListItem> </ListItem>
)) )
})
} }
</List> </List>
) : ( ) : (
......
...@@ -26,9 +26,10 @@ export const notificationApi = createApi({ ...@@ -26,9 +26,10 @@ export const notificationApi = createApi({
query: ({ id }) => ({ query: ({ id }) => ({
url: `/api/admin/notification/${id}`, url: `/api/admin/notification/${id}`,
method: "POST", method: "POST",
}) }),
invalidatesTags: ["Notification"]
}) })
}) })
}) })
export const { useGetAllNotificationQuery } = notificationApi export const { useGetAllNotificationQuery, useReadNotificationMutation } = notificationApi
\ No newline at end of file \ No newline at end of file
...@@ -4,6 +4,7 @@ export interface NotificationProps { ...@@ -4,6 +4,7 @@ export interface NotificationProps {
id: string; id: string;
message: string, message: string,
has_read: boolean has_read: boolean
created_at?: string;
} }
export interface NotificationResponse { export interface NotificationResponse {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment