- clients.ts, catalog.ts: corrige bug res.ok/res.json() — apiFetch retorna JSON direto - catalog.service.ts: corrige nomes de coluna da vw_produtos (descr_det, lista_pauta, remove preco_com_ipi inexistente) - CatalogPage.tsx: nova tela — código, descrição, grupo, marca, preço, estoque - router.tsx: adiciona rota /catalogo Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { useQuery } from '@tanstack/react-query';
|
|
import {
|
|
ClientListResponseSchema,
|
|
ClientDetailSchema,
|
|
type ClientListQuery,
|
|
type ClientListResponse,
|
|
type ClientDetail,
|
|
} from '@sar/api-interface';
|
|
import { apiFetch } from '../api-client';
|
|
|
|
export const CLIENT_KEYS = {
|
|
all: ['clients'] as const,
|
|
list: (params: Partial<ClientListQuery>) => ['clients', 'list', params] as const,
|
|
detail: (id: number) => ['clients', 'detail', id] as const,
|
|
};
|
|
|
|
export function useClientList(params: Partial<ClientListQuery> = {}) {
|
|
const qs = new URLSearchParams();
|
|
if (params.q) qs.set('q', params.q);
|
|
if (params.status) qs.set('status', params.status);
|
|
if (params.page) qs.set('page', String(params.page));
|
|
if (params.limit) qs.set('limit', String(params.limit));
|
|
|
|
const query = qs.toString();
|
|
|
|
return useQuery<ClientListResponse, Error>({
|
|
queryKey: CLIENT_KEYS.list(params),
|
|
queryFn: async () => {
|
|
const res = await apiFetch(`/clients${query ? `?${query}` : ''}`);
|
|
return ClientListResponseSchema.parse(res);
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useClientDetail(id: number | string | undefined) {
|
|
return useQuery<ClientDetail, Error>({
|
|
queryKey: CLIENT_KEYS.detail(Number(id)),
|
|
queryFn: async () => {
|
|
const res = await apiFetch(`/clients/${id}`);
|
|
return ClientDetailSchema.parse(res);
|
|
},
|
|
enabled: !!id,
|
|
});
|
|
}
|