65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "react-query";
|
|
import axios from "axios";
|
|
import api from "../services/api";
|
|
|
|
export function useAudiencias(processoId: string) {
|
|
const queryClient = useQueryClient();
|
|
|
|
const addAudiencia = useMutation(
|
|
async (data: any) => {
|
|
if (!data.tipo || !data.data || !data.local) {
|
|
throw new Error("Campos obrigatórios não preenchidos");
|
|
}
|
|
|
|
const response = await axios.post(
|
|
`/api/processos/${processoId}/audiencias`,
|
|
data,
|
|
);
|
|
return response.data;
|
|
},
|
|
{
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries(["processo", processoId]);
|
|
},
|
|
},
|
|
);
|
|
|
|
const updateAudiencia = useMutation(
|
|
async ({ id, data }: { id: string; data: any }) => {
|
|
const response = await axios.put(
|
|
`/api/processos/${processoId}/audiencias/${id}`,
|
|
data,
|
|
);
|
|
return response.data;
|
|
},
|
|
{
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries(["processo", processoId]);
|
|
},
|
|
},
|
|
);
|
|
|
|
const deleteAudiencia = useMutation(
|
|
async (id: string) => {
|
|
await axios.delete(`/api/processos/${processoId}/audiencias/${id}`);
|
|
},
|
|
{
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries(["processo", processoId]);
|
|
},
|
|
},
|
|
);
|
|
|
|
const { data: audiencias } = useQuery("audiencias", async () => {
|
|
const { data } = await api.get("/audiencias");
|
|
return data;
|
|
});
|
|
|
|
return {
|
|
addAudiencia: addAudiencia.mutateAsync,
|
|
updateAudiencia: updateAudiencia.mutateAsync,
|
|
deleteAudiencia: deleteAudiencia.mutateAsync,
|
|
audiencias,
|
|
};
|
|
}
|