realtine notification data aggregation

This commit is contained in:
Nouman Tahir 2022-11-16 14:44:42 +05:00
parent 10a9895386
commit ddfabc64f1

View File

@ -5,7 +5,7 @@ import {
useQueries,
useQueryClient,
} from '@tanstack/react-query';
import { useEffect, useState } from 'react';
import { useState } from 'react';
import { useIntl } from 'react-intl';
import { unionBy } from 'lodash';
import bugsnapInstance from '../../config/bugsnag';
@ -20,7 +20,6 @@ import QUERIES from './queryKeys';
const FETCH_LIMIT = 20;
export const useNotificationsQuery = (filter: NotificationFilters) => {
const [data, setData] = useState([]);
const [isRefreshing, setIsRefreshing] = useState(false);
const [pageParams, setPageParams] = useState(['']);
@ -37,11 +36,6 @@ 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({
@ -49,7 +43,6 @@ export const useNotificationsQuery = (filter: NotificationFilters) => {
queryKey: [QUERIES.NOTIFICATIONS.GET, filter, pageParam],
queryFn: () => _fetchNotifications(pageParam),
initialData: [],
onSuccess: _onSuccess,
})),
});
@ -74,8 +67,10 @@ export const useNotificationsQuery = (filter: NotificationFilters) => {
}
};
const _dataArrs = notificationQueries.map(query => query.data);
return {
data,
data: unionBy(..._dataArrs, 'id'),
isRefreshing,
isLoading: notificationQueries.lastItem.isLoading || notificationQueries.lastItem.isFetching,
fetchNextPage: _fetchNextPage,