first commit
This commit is contained in:
commit
c9fb67e751
423 files changed
+53287
No files matched your search
@@ -0,0 +1,70 @@
|
||||
import { useQuery, useMutation, useQueryClient } from "react-query";
|
||||
import axios from "axios";
|
||||
import { Cliente } from "../types/entities";
|
||||
|
||||
export function useClientes() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const { data: clientes = [], isLoading } = useQuery("clientes", async () => {
|
||||
try {
|
||||
const response = await axios.get<Cliente[]>("/api/clientes");
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Erro ao buscar clientes:", error);
|
||||
return [];
|
||||
}
|
||||
});
|
||||
|
||||
const createCliente = useMutation(
|
||||
async (data: Partial<Cliente>) => {
|
||||
const response = await axios.post<Cliente>("/api/clientes", data);
|
||||
return response.data;
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries("clientes");
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const updateCliente = useMutation(
|
||||
async ({ id, data }: { id: string; data: Partial<Cliente> }) => {
|
||||
console.log("Enviando atualização:", { id, data });
|
||||
const response = await axios.put<Cliente>(`/api/clientes/${id}`, data);
|
||||
return response.data;
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries("clientes");
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error("Erro ao atualizar cliente:", error);
|
||||
if (axios.isAxiosError(error)) {
|
||||
throw new Error(
|
||||
error.response?.data?.error || "Erro ao atualizar cliente",
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const deleteCliente = useMutation(
|
||||
async (id: string) => {
|
||||
await axios.delete(`/api/clientes/${id}`);
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries("clientes");
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
clientes,
|
||||
isLoading,
|
||||
createCliente: createCliente.mutateAsync,
|
||||
updateCliente: updateCliente.mutateAsync,
|
||||
deleteCliente: deleteCliente.mutateAsync,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user