Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions client/src/modules/notification/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useState,useRef,useCallback } from 'react';
import NotificationCard from './components/notificationCard';
import { notificationFilters } from './constants';
import { useSetAtom } from 'jotai';
Expand All @@ -23,38 +23,66 @@ import {
import { NOTIFICATION_FILTER_TYPE } from '../../infra/rest/typings';

const Notifications = () => {
const prevFilterRef = useRef<NOTIFICATION_FILTER_TYPE | null>(null);
const setNotifications = useSetAtom(NotificationsAtom);
const [filter, setFilter] = useState<NOTIFICATION_FILTER_TYPE>(
NOTIFICATION_FILTER_TYPE.ALL
);
const { fetchNotifications, notifications } = useNotifications();
const { isAuthenticated } = useAuth();
const [isLoadingMore, setIsLoadingMore] = useState(false);
const [isFilterLoading, setIsFilterLoading] = useState(false);


// Avoid refetching notifications when the same filter is selected again
useEffect(() => {
if (isAuthenticated()) {
setNotifications(null);
fetchNotifications({ page: 1, filter, deletedDocCount: 0 });
if (!isAuthenticated()) return;
if (prevFilterRef.current === filter && notifications !== null) {
return;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [filter]);
prevFilterRef.current = filter;
setIsFilterLoading(true);
setNotifications(null);
fetchNotifications({
page: 1,
filter,
deletedDocCount: 0,
}).finally(() => {
setIsFilterLoading(false);
});

}, [filter, isAuthenticated, fetchNotifications, notifications, setNotifications]);

const handleFilter = (filterName: string) => {

const handleFilter = useCallback((filterName: string) => {
setFilter(filterName as NOTIFICATION_FILTER_TYPE);
};
}, []);

const handleLoadMore = () => {

const handleLoadMore = async () => {
if (
notifications &&
notifications.results.length < notifications.totalDocs
!notifications ||
isLoadingMore ||
notifications.results.length >= notifications.totalDocs
) {
fetchNotifications({
return;
}

try {
setIsLoadingMore(true);
await fetchNotifications({
page: notifications.page + 1,
filter,
deletedDocCount: notifications.deleteDocCount || 0,
});
}

finally {
setIsLoadingMore(false);
}
};


return (
<Box sx={{ maxWidth: 900, mx: 'auto', p: 3 }}>
<Box sx={{ mb: 4 }}>
Expand Down Expand Up @@ -93,6 +121,7 @@ const Notifications = () => {
variant={isActive ? 'contained' : 'outlined'}
color={isActive ? 'primary' : 'inherit'}
onClick={() => handleFilter(filterName)}
disabled={isFilterLoading}
startIcon={
filterName === 'all' ? (
<NotificationsIcon />
Expand Down Expand Up @@ -181,8 +210,12 @@ const Notifications = () => {
onClick={handleLoadMore}
variant="outlined"
size="medium"
disabled={isLoadingMore}
startIcon={
isLoadingMore ? <CircularProgress size={16} /> : null
}
>
Load More
{isLoadingMore ? 'Loading...' : 'Load More'}
</Button>
</Box>
)}
Expand Down
Loading