48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
|
|
import { useDataSource } from "@erp/core/hooks";
|
||
|
|
import {
|
||
|
|
type DefaultError,
|
||
|
|
type QueryKey,
|
||
|
|
type UseQueryResult,
|
||
|
|
useQuery,
|
||
|
|
} from "@tanstack/react-query";
|
||
|
|
|
||
|
|
import { type Customer, getCustomerById } from "../api";
|
||
|
|
|
||
|
|
export const CUSTOMER_QUERY_KEY = (customerId?: string): QueryKey => [
|
||
|
|
"customers:detail",
|
||
|
|
{
|
||
|
|
customerId,
|
||
|
|
},
|
||
|
|
];
|
||
|
|
|
||
|
|
type CustomerQueryOptions = {
|
||
|
|
enabled?: boolean;
|
||
|
|
};
|
||
|
|
|
||
|
|
export const useCustomerGetQuery = (
|
||
|
|
customerId?: string,
|
||
|
|
options?: CustomerQueryOptions
|
||
|
|
): UseQueryResult<Customer, DefaultError> => {
|
||
|
|
const dataSource = useDataSource();
|
||
|
|
const enabled = options?.enabled ?? Boolean(customerId);
|
||
|
|
|
||
|
|
return useQuery<Customer, DefaultError>({
|
||
|
|
queryKey: CUSTOMER_QUERY_KEY(customerId),
|
||
|
|
queryFn: async ({ signal }) => getCustomerById(dataSource, signal, customerId),
|
||
|
|
enabled,
|
||
|
|
placeholderData: (previousData, _previousQuery) => previousData, // Mantener datos previos mientras se carga nueva datos (antiguo `keepPreviousData`)
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
/*export function invalidateCustomerDetailCache(qc: QueryClient, id: string) {
|
||
|
|
return qc.invalidateQueries({
|
||
|
|
queryKey: getCustomerQueryKey(id ?? "unknown"),
|
||
|
|
exact: Boolean(id),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function setCustomerDetailCache(qc: QueryClient, id: string, data: unknown) {
|
||
|
|
qc.setQueryData(getCustomerQueryKey(id), data);
|
||
|
|
}
|
||
|
|
*/
|