add hook useContactsQuery
This commit is contained in:
parent
ebfd5570e9
commit
84cdee4272
@ -1 +1,2 @@
|
||||
export * from './query';
|
||||
export * from './use-customer-contacts';
|
||||
|
||||
10
apps/web/hooks/contacts/query.ts
Normal file
10
apps/web/hooks/contacts/query.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { getClients, getMasters } from '@/actions/contacts';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
export const useClientsQuery = () => {
|
||||
return useQuery({ queryFn: getClients, queryKey: ['contacts', 'clients', 'get'] });
|
||||
};
|
||||
|
||||
export const useMastersQuery = () => {
|
||||
return useQuery({ queryFn: getMasters, queryKey: ['contacts', 'masters', 'get'] });
|
||||
};
|
||||
@ -1,26 +1,18 @@
|
||||
'use client';
|
||||
import { getClients, getMasters } from '@/actions/contacts';
|
||||
import { useClientsQuery, useMastersQuery } from './query';
|
||||
import { ContactsFilterContext } from '@/context/contacts-filter';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { sift } from 'radash';
|
||||
import { use, useMemo } from 'react';
|
||||
|
||||
export function useCustomerContacts() {
|
||||
const { filter } = use(ContactsFilterContext);
|
||||
|
||||
const { data: clientsData, isLoading: isLoadingClients } = useQuery({
|
||||
queryFn: getClients,
|
||||
queryKey: ['contacts', 'clients'],
|
||||
});
|
||||
const { data: clientsData, isLoading: isLoadingClients } = useClientsQuery();
|
||||
const { data: mastersData, isLoading: isLoadingMasters } = useMastersQuery();
|
||||
|
||||
const clients = clientsData?.clients;
|
||||
|
||||
const { data: mastersData, isLoading: isLoadingMasters } = useQuery({
|
||||
queryFn: getMasters,
|
||||
queryKey: ['contacts', 'masters'],
|
||||
});
|
||||
|
||||
const masters = mastersData?.masters;
|
||||
const isLoading = isLoadingClients || isLoadingMasters;
|
||||
|
||||
const contacts = useMemo(() => {
|
||||
switch (filter) {
|
||||
@ -33,5 +25,5 @@ export function useCustomerContacts() {
|
||||
}
|
||||
}, [clients, masters, filter]);
|
||||
|
||||
return { contacts, isLoading: isLoadingClients || isLoadingMasters };
|
||||
return { contacts, isLoading };
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user