optimize useCustomerContacts
This commit is contained in:
parent
0281e99403
commit
2a830ceffb
@ -2,7 +2,7 @@ import { getClients, getMasters } from '@/actions/contacts';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
export const useClientsQuery = () =>
|
||||
useQuery({ queryFn: getClients, queryKey: ['contacts', 'clients', 'get'] });
|
||||
useQuery({ enabled: false, queryFn: getClients, queryKey: ['contacts', 'clients', 'get'] });
|
||||
|
||||
export const useMastersQuery = () =>
|
||||
useQuery({ queryFn: getMasters, queryKey: ['contacts', 'masters', 'get'] });
|
||||
useQuery({ enabled: false, queryFn: getMasters, queryKey: ['contacts', 'masters', 'get'] });
|
||||
|
||||
@ -2,27 +2,43 @@
|
||||
import { useClientsQuery, useMastersQuery } from './query';
|
||||
import { ContactsFilterContext } from '@/context/contacts-filter';
|
||||
import { sift } from 'radash';
|
||||
import { use, useMemo } from 'react';
|
||||
import { use, useEffect, useMemo } from 'react';
|
||||
|
||||
export function useCustomerContacts() {
|
||||
const { filter, setFilter } = use(ContactsFilterContext);
|
||||
|
||||
const { data: clientsData, isLoading: isLoadingClients } = useClientsQuery();
|
||||
const { data: mastersData, isLoading: isLoadingMasters } = useMastersQuery();
|
||||
const {
|
||||
data: clientsData,
|
||||
isLoading: loadingClients,
|
||||
refetch: refetchClients,
|
||||
} = useClientsQuery();
|
||||
|
||||
const clients = clientsData?.clients;
|
||||
const masters = mastersData?.masters;
|
||||
const isLoading = isLoadingClients || isLoadingMasters;
|
||||
const {
|
||||
data: mastersData,
|
||||
isLoading: loadingMasters,
|
||||
refetch: refetchMasters,
|
||||
} = useMastersQuery();
|
||||
|
||||
const clients = clientsData?.clients || [];
|
||||
const masters = mastersData?.masters || [];
|
||||
const isLoading = loadingClients || loadingMasters;
|
||||
|
||||
useEffect(() => {
|
||||
if (filter === 'clients') {
|
||||
refetchClients();
|
||||
} else if (filter === 'masters') {
|
||||
refetchMasters();
|
||||
} else {
|
||||
refetchClients();
|
||||
refetchMasters();
|
||||
}
|
||||
}, [filter, refetchClients, refetchMasters]);
|
||||
|
||||
const contacts = useMemo(() => {
|
||||
switch (filter) {
|
||||
case 'clients':
|
||||
return clients ? sift(clients) : [];
|
||||
case 'masters':
|
||||
return masters ? sift(masters) : [];
|
||||
default:
|
||||
return [...(clients ? sift(clients) : []), ...(masters ? sift(masters) : [])];
|
||||
}
|
||||
if (filter === 'clients') return sift(clients);
|
||||
if (filter === 'masters') return sift(masters);
|
||||
|
||||
return [...sift(clients), ...sift(masters)];
|
||||
}, [clients, masters, filter]);
|
||||
|
||||
return { contacts, filter, isLoading, setFilter };
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user