2026-03-10 17:10:11 +00:00
|
|
|
import { useDataSource } from "@erp/core/hooks";
|
2026-04-03 19:49:59 +00:00
|
|
|
import { type DefaultError, type UseQueryResult, useQuery } from "@tanstack/react-query";
|
2026-03-10 17:10:11 +00:00
|
|
|
|
2026-03-31 08:21:48 +00:00
|
|
|
import { GetCustomerByIdAdapter } from "../adapters";
|
|
|
|
|
import { getCustomerById } from "../api";
|
|
|
|
|
import type { Customer } from "../entities";
|
2026-03-10 17:10:11 +00:00
|
|
|
|
2026-04-03 19:49:59 +00:00
|
|
|
import { CUSTOMER_QUERY_KEY } from "./keys";
|
2026-03-10 17:10:11 +00:00
|
|
|
|
2026-04-03 19:49:59 +00:00
|
|
|
type CustomerGetQueryOptions = {
|
2026-03-10 17:10:11 +00:00
|
|
|
enabled?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useCustomerGetQuery = (
|
|
|
|
|
customerId?: string,
|
2026-04-03 19:49:59 +00:00
|
|
|
options?: CustomerGetQueryOptions
|
2026-03-10 17:10:11 +00:00
|
|
|
): UseQueryResult<Customer, DefaultError> => {
|
2026-04-03 19:49:59 +00:00
|
|
|
//const queryClient = useQueryClient();
|
2026-03-10 17:10:11 +00:00
|
|
|
const dataSource = useDataSource();
|
|
|
|
|
const enabled = options?.enabled ?? Boolean(customerId);
|
|
|
|
|
|
|
|
|
|
return useQuery<Customer, DefaultError>({
|
|
|
|
|
queryKey: CUSTOMER_QUERY_KEY(customerId),
|
2026-03-31 08:21:48 +00:00
|
|
|
queryFn: async ({ signal }) => {
|
2026-04-03 19:49:59 +00:00
|
|
|
const dto = await getCustomerById(dataSource, String(customerId), signal);
|
2026-03-31 08:21:48 +00:00
|
|
|
return GetCustomerByIdAdapter.fromDTO(dto);
|
|
|
|
|
},
|
2026-03-10 17:10:11 +00:00
|
|
|
enabled,
|
2026-04-03 19:49:59 +00:00
|
|
|
//placeholderData <-- No mostrar datos de una tupla anterior mientras se carga nuevos datos
|
2026-03-10 17:10:11 +00:00
|
|
|
});
|
|
|
|
|
};
|