36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { useDataSource } from "@erp/core/hooks";
|
|
import { type DefaultError, type UseQueryResult, useQuery } from "@tanstack/react-query";
|
|
|
|
import { GetCustomerByIdAdapter } from "../adapters";
|
|
import { type GetCustomerByIdResult, getCustomerById } from "../api";
|
|
import type { Customer } from "../entities";
|
|
|
|
import { CUSTOMER_QUERY_KEY } from "./keys";
|
|
|
|
type CustomerGetQueryOptions = {
|
|
enabled?: boolean;
|
|
};
|
|
|
|
export const useCustomerGetQuery = (
|
|
customerId?: string,
|
|
options?: CustomerGetQueryOptions
|
|
): UseQueryResult<Customer, DefaultError> => {
|
|
//const queryClient = useQueryClient();
|
|
const dataSource = useDataSource();
|
|
const enabled = options?.enabled ?? Boolean(customerId);
|
|
|
|
return useQuery<Customer, DefaultError>({
|
|
queryKey: CUSTOMER_QUERY_KEY(customerId),
|
|
queryFn: async ({ signal }) => {
|
|
const dto: GetCustomerByIdResult = await getCustomerById(
|
|
dataSource,
|
|
{ id: customerId! },
|
|
signal
|
|
);
|
|
return GetCustomerByIdAdapter.fromDTO(dto);
|
|
},
|
|
enabled,
|
|
//placeholderData <-- No mostrar datos de una tupla anterior mientras se carga nuevos datos
|
|
});
|
|
};
|