add hook useContactsQuery

This commit is contained in:
vchikalkin 2025-02-08 21:59:14 +03:00
parent ebfd5570e9
commit 84cdee4272
3 changed files with 16 additions and 13 deletions

View File

@ -1 +1,2 @@
export * from './query';
export * from './use-customer-contacts';

View 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'] });
};

View File

@ -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 };
}