mirror of
https://github.com/ecency/ecency-mobile.git
synced 2024-12-19 19:31:54 +03:00
Merge pull request #2532 from ecency/nt/fix-notifi-duplicate
doing an id based union of notifications while compiling list data
This commit is contained in:
commit
b5aa293325
@ -5,7 +5,7 @@ import {
|
||||
useQueries,
|
||||
useQueryClient,
|
||||
} from '@tanstack/react-query';
|
||||
import { useState } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useIntl } from 'react-intl';
|
||||
import bugsnapInstance from '../../config/bugsnag';
|
||||
import { useAppDispatch, useAppSelector } from '../../hooks';
|
||||
@ -15,13 +15,17 @@ import { getNotifications, markNotifications } from '../ecency/ecency';
|
||||
import { NotificationFilters } from '../ecency/ecency.types';
|
||||
import { markHiveNotifications } from '../hive/dhive';
|
||||
import QUERIES from './queryKeys';
|
||||
import { unionBy } from 'lodash';
|
||||
|
||||
|
||||
const FETCH_LIMIT = 20;
|
||||
|
||||
export const useNotificationsQuery = (filter: NotificationFilters) => {
|
||||
const [data, setData] = useState([]);
|
||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||
const [pageParams, setPageParams] = useState(['']);
|
||||
|
||||
|
||||
const _fetchNotifications = async (pageParam: string) => {
|
||||
console.log('fetching page since:', pageParam);
|
||||
const response = await getNotifications({ filter, since: pageParam, limit: FETCH_LIMIT });
|
||||
@ -35,12 +39,19 @@ export const useNotificationsQuery = (filter: NotificationFilters) => {
|
||||
return lastId;
|
||||
};
|
||||
|
||||
const _onSuccess = () => {
|
||||
const dataArrs = notificationQueries.map((query)=>query.data);
|
||||
const _data = unionBy(...dataArrs, 'id')
|
||||
setData(_data);
|
||||
}
|
||||
|
||||
// query initialization
|
||||
const notificationQueries = useQueries({
|
||||
queries: pageParams.map((pageParam) => ({
|
||||
queryKey: [QUERIES.NOTIFICATIONS.GET, filter, pageParam],
|
||||
queryFn: () => _fetchNotifications(pageParam),
|
||||
initialData: [],
|
||||
onSuccess:_onSuccess
|
||||
})),
|
||||
});
|
||||
|
||||
@ -66,7 +77,7 @@ export const useNotificationsQuery = (filter: NotificationFilters) => {
|
||||
};
|
||||
|
||||
return {
|
||||
data: notificationQueries.flatMap((query) => query.data),
|
||||
data,
|
||||
isRefreshing,
|
||||
isLoading: notificationQueries.lastItem.isLoading || notificationQueries.lastItem.isFetching,
|
||||
fetchNextPage: _fetchNextPage,
|
||||
|
Loading…
Reference in New Issue
Block a user