export const useProducts = () => {
const productSearchValue = useProductSearchValue();
const { customerId, port, customerOrganizationId } = usePreferencesValue();
const { data, isError, isLoading, isFetching, isFetched } = useQuery({
queryKey: [QueryKeys.Products, customerId, port, customerOrganizationId],
retry: false,
useErrorBoundary: true,
enabled: !!customerId && !!port && !!customerOrganizationId,
queryFn: async ({ signal }) => {
try {
const req = await fetchApi<Product[]>(
`/api/products/all?customer=${customerId}&port=${port?.id}&organization=${customerOrganizationId}`,
{
method: "GET",
signal: signal,
},
);
return req;
} catch (err) {
if (err instanceof Error) {
throw new Error(err.message);
}
}
},
select: (data) => {
const regExp = new RegExp(productSearchValue, "gi");
return data?.filter((p) => p.name.match(regExp) || p.id.match(regExp));
},
});
return { data, isLoading, isFetched, isFetching, isError };
};
export const useProducts = () => {
const productSearchValue = useProductSearchValue();
const { customerId, port, customerOrganizationId } = usePreferencesValue();
const { data, isError, isLoading, isFetching, isFetched } = useQuery({
queryKey: [QueryKeys.Products, customerId, port, customerOrganizationId],
retry: false,
useErrorBoundary: true,
enabled: !!customerId && !!port && !!customerOrganizationId,
queryFn: async ({ signal }) => {
try {
const req = await fetchApi<Product[]>(
`/api/products/all?customer=${customerId}&port=${port?.id}&organization=${customerOrganizationId}`,
{
method: "GET",
signal: signal,
},
);
return req;
} catch (err) {
if (err instanceof Error) {
throw new Error(err.message);
}
}
},
select: (data) => {
const regExp = new RegExp(productSearchValue, "gi");
return data?.filter((p) => p.name.match(regExp) || p.id.match(regExp));
},
});
return { data, isLoading, isFetched, isFetching, isError };
};