This commit is contained in:
Nouman Tahir 2022-09-22 18:06:36 +05:00
parent d5a0e3aa06
commit 4fa75eec2c
2 changed files with 39 additions and 44 deletions

View File

@ -1,25 +1,28 @@
import { useInfiniteQuery } from "@tanstack/react-query";
import { getNotifications } from "../ecency/ecency";
import { NotificationFilters } from "../ecency/ecency.types";
import QUERIES from "./queryKeys";
import { useInfiniteQuery } from '@tanstack/react-query';
import { getNotifications } from '../ecency/ecency';
import { NotificationFilters } from '../ecency/ecency.types';
import QUERIES from './queryKeys';
export const useNotificationsQuery = (filter: NotificationFilters) => {
const _fetchLimit = 20
return useInfiniteQuery<any[]>([QUERIES.NOTIFICATIONS.GET, filter], async ({ pageParam }) => {
console.log("fetching page since:", pageParam)
const response = getNotifications({ filter, since: pageParam, limit: _fetchLimit })
console.log("new page fetched", response)
return response || []
}, {
initialData: {
pageParams: [undefined],
pages: []
},
getNextPageParam: (lastPage) => {
const lastId = lastPage.length === _fetchLimit ? lastPage.lastItem.id : undefined;
console.log("extracting next page parameter", lastId);
return lastId;
}
});
}
const _fetchLimit = 20;
return useInfiniteQuery<any[]>(
[QUERIES.NOTIFICATIONS.GET, filter],
async ({ pageParam }) => {
console.log('fetching page since:', pageParam);
const response = getNotifications({ filter, since: pageParam, limit: _fetchLimit });
console.log('new page fetched', response);
return response || [];
},
{
initialData: {
pageParams: [undefined],
pages: [],
},
getNextPageParam: (lastPage) => {
const lastId = lastPage.length === _fetchLimit ? lastPage.lastItem.id : undefined;
console.log('extracting next page parameter', lastId);
return lastId;
},
},
);
};

View File

@ -7,6 +7,7 @@ import { useIntl } from 'react-intl';
// Actions and Services
import { useEffect } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { markNotifications } from '../../../providers/ecency/ecency';
import { updateUnreadActivityCount } from '../../../redux/actions/accountAction';
@ -21,11 +22,8 @@ import bugsnapInstance from '../../../config/bugsnag';
import { useAppSelector } from '../../../hooks';
import { useNotificationsQuery } from '../../../providers/queries';
import { NotificationFilters } from '../../../providers/ecency/ecency.types';
import { useQueryClient } from '@tanstack/react-query';
import QUERIES from '../../../providers/queries/queryKeys';
const NotificationContainer = ({ navigation }) => {
const intl = useIntl();
const dispatch = useDispatch();
@ -39,41 +37,35 @@ const NotificationContainer = ({ navigation }) => {
const unreadCountRef = useRef(currentAccount.unread_acitivity_count || 0);
const [selectedFilter, setSelectedFilter] = useState<NotificationFilters>(NotificationFilters.ACTIVITIES);
const notificationsQuery = useNotificationsQuery(selectedFilter)
const [selectedFilter, setSelectedFilter] = useState<NotificationFilters>(
NotificationFilters.ACTIVITIES,
);
const notificationsQuery = useNotificationsQuery(selectedFilter);
useEffect(() => {
queryClient.refetchQueries([QUERIES.NOTIFICATIONS.GET]);
}, [currentAccount.username]);
useEffect(() => {
if (currentAccount.unread_activity_count > unreadCountRef.current) {
queryClient.refetchQueries([QUERIES.NOTIFICATIONS.GET], {
refetchPage: (page, index) => index === 0
refetchPage: (page, index) => index === 0,
});
}
unreadCountRef.current = currentAccount.unread_activity_count;
}, [currentAccount.unread_activity_count]);
const _getActivities = (loadMore = false) => {
if (loadMore) {
console.log("load more notifications")
console.log('load more notifications');
notificationsQuery.fetchNextPage();
} else {
console.log("refetching all")
console.log('refetching all');
notificationsQuery.refetch();
}
};
const _navigateToNotificationRoute = (data) => {
const type = get(data, 'type');
const permlink = get(data, 'permlink');
@ -122,7 +114,6 @@ const NotificationContainer = ({ navigation }) => {
dispatch(showProfileModal(username));
};
//TODO: handle mark as read mutations
const _readAllNotification = () => {
if (!isConnected) {
@ -168,10 +159,11 @@ const NotificationContainer = ({ navigation }) => {
setSelectedFilter(value);
};
const _notifications = useMemo(() =>
notificationsQuery.data?.pages?.reduce(
(prevData, curData) => prevData.concat(curData), []
), [notificationsQuery.data?.pages]);
const _notifications = useMemo(
() =>
notificationsQuery.data?.pages?.reduce((prevData, curData) => prevData.concat(curData), []),
[notificationsQuery.data?.pages],
);
return (
<NotificationScreen