updated refetch behaviour

This commit is contained in:
Nouman Tahir 2022-09-22 19:44:32 +05:00
parent 4fa75eec2c
commit a8979f5b74
3 changed files with 23 additions and 17 deletions

View File

@ -231,7 +231,7 @@ class NotificationView extends PureComponent {
onEndReachedThreshold={0.3}
ListFooterComponent={this._renderFooterLoading}
ListEmptyComponent={
isLoading ? <ListPlaceHolder/> : (
isNotificationRefreshing ? <ListPlaceHolder/> : (
<Text style={globalStyles.hintText}>
{intl.formatMessage({ id: 'notification.noactivity' })}
</Text>

View File

@ -5,6 +5,8 @@ import QUERIES from './queryKeys';
export const useNotificationsQuery = (filter: NotificationFilters) => {
const _fetchLimit = 20;
return useInfiniteQuery<any[]>(
[QUERIES.NOTIFICATIONS.GET, filter],
async ({ pageParam }) => {
@ -19,7 +21,7 @@ export const useNotificationsQuery = (filter: NotificationFilters) => {
pages: [],
},
getNextPageParam: (lastPage) => {
const lastId = lastPage.length === _fetchLimit ? lastPage.lastItem.id : undefined;
const lastId = lastPage && lastPage.length === _fetchLimit ? lastPage.lastItem.id : undefined;
console.log('extracting next page parameter', lastId);
return lastId;
},

View File

@ -36,6 +36,7 @@ const NotificationContainer = ({ navigation }) => {
const globalProps = useAppSelector((state) => state.account.globalProps);
const unreadCountRef = useRef(currentAccount.unread_acitivity_count || 0);
const curUsername = useRef(currentAccount.username);
const [selectedFilter, setSelectedFilter] = useState<NotificationFilters>(
NotificationFilters.ACTIVITIES,
@ -43,25 +44,31 @@ const NotificationContainer = ({ navigation }) => {
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,
});
queryClient.removeQueries([QUERIES.NOTIFICATIONS.GET])
notificationsQuery.refetch();
curUsername.current = currentAccount.useranme
}, [currentAccount.username]);
useEffect(() => {
if (currentAccount.unread_activity_count < unreadCountRef.current) {
queryClient.invalidateQueries([QUERIES.NOTIFICATIONS.GET]);
}
unreadCountRef.current = currentAccount.unread_activity_count;
}, [currentAccount.unread_activity_count]);
unreadCountRef.current = currentAccount.unread_activity_count
}, [currentAccount.unread_activity_count])
const _getActivities = (loadMore = false) => {
if (loadMore) {
console.log('load more notifications');
notificationsQuery.fetchNextPage();
} else {
console.log('refetching all');
console.log('refreshing');
notificationsQuery.remove();
notificationsQuery.refetch();
}
};
@ -159,11 +166,8 @@ const NotificationContainer = ({ navigation }) => {
setSelectedFilter(value);
};
const _notifications = useMemo(
() =>
notificationsQuery.data?.pages?.reduce((prevData, curData) => prevData.concat(curData), []),
[notificationsQuery.data?.pages],
);
console.log("query data: ", notificationsQuery.data)
const _notifications = notificationsQuery.data?.pages?.flatMap(page => page);
return (
<NotificationScreen