FactuGES_ERP/modules/customers/src/web/shared/hooks/use-customer-get-query.ts

36 lines
1.1 KiB
TypeScript
Raw Normal View History

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";
2026-04-04 16:58:32 +00:00
import { type GetCustomerByIdResult, getCustomerById } from "../api";
2026-03-31 08:21:48 +00:00
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-04 16:58:32 +00:00
const dto: GetCustomerByIdResult = await getCustomerById(
dataSource,
{ id: 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
});
};