diff --git a/src/app/(protected)/(cadastros)/cadastros/(t_tb_andamentoservico)/andamentos/page.tsx b/src/app/(protected)/(cadastros)/cadastros/(t_tb_andamentoservico)/andamentos/page.tsx index 7fe708a..2f9fb14 100644 --- a/src/app/(protected)/(cadastros)/cadastros/(t_tb_andamentoservico)/andamentos/page.tsx +++ b/src/app/(protected)/(cadastros)/cadastros/(t_tb_andamentoservico)/andamentos/page.tsx @@ -16,11 +16,13 @@ import ConfirmDialog from "@/app/_components/confirm_dialog/ConfirmDialog"; import { useConfirmDialog } from "@/app/_components/confirm_dialog/useConfirmDialog"; import TTBAndamentoServicoInterface from "../../_interfaces/TTBAndamentoServicoInterface"; +import { useTTBAndamentoServicoDeleteHook } from "../../_hooks/t_tb_andamentoservico/useTTBAndamentoServicoDeleteHook"; export default function TTBAndamentoServico() { // Hooks para leitura e salvamento const { tTBAndamentosServicos, fetchTTBAndamentoServico } = useTTBAndamentoServicoReadHook(); const { saveTTBAndamentoServico } = useTTBAndamentoServicoSaveHook(); + const { deleteTTBAndamentoServico } = useTTBAndamentoServicoDeleteHook(); // Estados const [selectedAndamento, setSelectedAndamento] = useState(null); @@ -58,34 +60,49 @@ export default function TTBAndamentoServico() { /** * Salva os dados do formulário */ - const handleSave = useCallback( - async (formData: TTBAndamentoServicoInterface) => { - await saveTTBAndamentoServico(formData); - handleCloseForm(); - fetchTTBAndamentoServico(); // Atualiza a lista após salvar - }, - [saveTTBAndamentoServico, fetchTTBAndamentoServico, handleCloseForm] - ); + const handleSave = useCallback(async (formData: TTBAndamentoServicoInterface) => { + + // Aguarda salvar o registro + await saveTTBAndamentoServico(formData); + + // Encerra o fomulário + handleCloseForm(); + + // Atualiza a lista de dados + fetchTTBAndamentoServico(); + + }, [saveTTBAndamentoServico, fetchTTBAndamentoServico, handleCloseForm]); /** * Quando o usuário clica em "remover" na tabela */ const handleConfirmDelete = useCallback((item: TTBAndamentoServicoInterface) => { + + // Define o item atual para remoção setItemToDelete(item); - openConfirmDialog(); // Abre o modal de confirmação + + // Abre o modal de confirmação + openConfirmDialog(); + }, [openConfirmDialog]); /** * Executa a exclusão de fato quando o usuário confirma */ - const handleDelete = useCallback(() => { - if (!itemToDelete) return; - console.log("Deletando andamento:", itemToDelete); + const handleDelete = useCallback(async () => { + + // Executa o Hook de remoção + await deleteTTBAndamentoServico(itemToDelete); + + // Atualiza a lista + await fetchTTBAndamentoServico(); + + // Limpa o item selecionado + setItemToDelete(null); + + // Fecha o modal + handleCancel(); - // TODO: Implementar lógica de exclusão na API - fetchTTBAndamentoServico(); // Atualiza a lista - setItemToDelete(null); // Limpa o item selecionado - handleCancel(); // Fecha o modal }, [itemToDelete, fetchTTBAndamentoServico, handleCancel]); /** diff --git a/src/app/(protected)/(cadastros)/cadastros/_components/t_tb_andamentoservico/TTBAndamentoServicoForm.tsx b/src/app/(protected)/(cadastros)/cadastros/_components/t_tb_andamentoservico/TTBAndamentoServicoForm.tsx index bd85c25..f93cd52 100644 --- a/src/app/(protected)/(cadastros)/cadastros/_components/t_tb_andamentoservico/TTBAndamentoServicoForm.tsx +++ b/src/app/(protected)/(cadastros)/cadastros/_components/t_tb_andamentoservico/TTBAndamentoServicoForm.tsx @@ -55,7 +55,7 @@ export default function TTBAndamentoServicoForm({ isOpen, data, onClose, onSave tipo: "", situacao: "A", usa_email: "I", - tb_andamentoservico_id: undefined, + tb_andamentoservico_id: 0, }, }); @@ -170,11 +170,13 @@ export default function TTBAndamentoServicoForm({ isOpen, data, onClose, onSave {/* Rodapé do Dialog */} - - + {/* Campo oculto */} diff --git a/src/app/(protected)/(cadastros)/cadastros/_data/TTBAndamentoServico/TTBAndamentoServicoRemoveData.ts b/src/app/(protected)/(cadastros)/cadastros/_data/TTBAndamentoServico/TTBAndamentoServicoRemoveData.ts new file mode 100644 index 0000000..5ad8fc9 --- /dev/null +++ b/src/app/(protected)/(cadastros)/cadastros/_data/TTBAndamentoServico/TTBAndamentoServicoRemoveData.ts @@ -0,0 +1,18 @@ +'use server' + +import API from "@/services/api/Api"; +import TTBAndamentoServicoInteface from "../../_interfaces/TTBAndamentoServicoInterface"; +import { Methods } from "@/services/api/enums/ApiMethodEnum"; + +export default async function TTBAndamentoServicoRemoveData(tTBAndamentoServico: TTBAndamentoServicoInteface) { + + const api = new API(); + + const response = await api.send({ + method: Methods.DELETE, + endpoint: `administrativo/t_tb_andamentoservico/${tTBAndamentoServico.tb_andamentoservico_id}` + }); + + return response; + +} \ No newline at end of file diff --git a/src/app/(protected)/(cadastros)/cadastros/_data/TTBAndamentoServico/TTBAndamentoServicoSaveData.ts b/src/app/(protected)/(cadastros)/cadastros/_data/TTBAndamentoServico/TTBAndamentoServicoSaveData.ts index fdc5943..c8ebd33 100644 --- a/src/app/(protected)/(cadastros)/cadastros/_data/TTBAndamentoServico/TTBAndamentoServicoSaveData.ts +++ b/src/app/(protected)/(cadastros)/cadastros/_data/TTBAndamentoServico/TTBAndamentoServicoSaveData.ts @@ -4,14 +4,14 @@ import API from "@/services/api/Api"; import TTBAndamentoServicoInteface from "../../_interfaces/TTBAndamentoServicoInterface"; import { Methods } from "@/services/api/enums/ApiMethodEnum"; -export default async function TTBAndamentoServicoSaveData(andamentoServico: TTBAndamentoServicoInteface) { +export default async function TTBAndamentoServicoSaveData(data: TTBAndamentoServicoInteface) { const api = new API(); const response = await api.send({ - method: Methods.POST, - endpoint: `administrativo/t_tb_andamentoservico/`, - body: andamentoServico + method: data.tb_andamentoservico_id ? Methods.PUT : Methods.POST, + endpoint: `administrativo/t_tb_andamentoservico/${data.tb_andamentoservico_id ? data.tb_andamentoservico_id : ''}`, + body: data }); return response; diff --git a/src/app/(protected)/(cadastros)/cadastros/_hooks/t_tb_andamentoservico/useTTBAndamentoServicoDeleteHook.ts b/src/app/(protected)/(cadastros)/cadastros/_hooks/t_tb_andamentoservico/useTTBAndamentoServicoDeleteHook.ts new file mode 100644 index 0000000..c3407ee --- /dev/null +++ b/src/app/(protected)/(cadastros)/cadastros/_hooks/t_tb_andamentoservico/useTTBAndamentoServicoDeleteHook.ts @@ -0,0 +1,24 @@ +import { useResponse } from "@/app/_response/ResponseContext" +import { useState } from "react"; +import TTBAndamentoServicoInteface from "../../_interfaces/TTBAndamentoServicoInterface"; +import TTBAndamentoServicoRemoveData from "../../_data/TTBAndamentoServico/TTBAndamentoServicoRemoveData"; + +export const useTTBAndamentoServicoDeleteHook = () => { + + const { setResponse } = useResponse(); + + const [tTBAndamentoServico, setTTBAndamentoServico] = useState(); + + const deleteTTBAndamentoServico = async (data: TTBAndamentoServicoInteface) => { + + const response = await TTBAndamentoServicoRemoveData(data); + + setTTBAndamentoServico(data); + + setResponse(response); + + } + + return { tTBAndamentoServico, deleteTTBAndamentoServico } + +} \ No newline at end of file diff --git a/src/app/(protected)/(cadastros)/cadastros/_hooks/t_tb_andamentoservico/useTTBAndamentoServicoSaveHook.ts b/src/app/(protected)/(cadastros)/cadastros/_hooks/t_tb_andamentoservico/useTTBAndamentoServicoSaveHook.ts index 18f6c0d..c5c1c37 100644 --- a/src/app/(protected)/(cadastros)/cadastros/_hooks/t_tb_andamentoservico/useTTBAndamentoServicoSaveHook.ts +++ b/src/app/(protected)/(cadastros)/cadastros/_hooks/t_tb_andamentoservico/useTTBAndamentoServicoSaveHook.ts @@ -11,9 +11,9 @@ export const useTTBAndamentoServicoSaveHook = () => { const [tTBAndamentoServico, setTTBAndamentoServico] = useState(); - const saveTTBAndamentoServico = async (form: TTBAndamentoServicoInteface) => { + const saveTTBAndamentoServico = async (data: TTBAndamentoServicoInteface) => { - const response = await TTBAndamentoServicoSaveData(form); + const response = await TTBAndamentoServicoSaveData(data); // Armazena os dados da repsota setTTBAndamentoServico(response.data); diff --git a/src/app/(protected)/(cadastros)/cadastros/_schemas/TTBAndamentoServicoSchema.ts b/src/app/(protected)/(cadastros)/cadastros/_schemas/TTBAndamentoServicoSchema.ts index af713ff..7a326bc 100644 --- a/src/app/(protected)/(cadastros)/cadastros/_schemas/TTBAndamentoServicoSchema.ts +++ b/src/app/(protected)/(cadastros)/cadastros/_schemas/TTBAndamentoServicoSchema.ts @@ -2,10 +2,10 @@ import { z } from 'zod'; export const TTBAndamentoServicoSchema = z.object({ - tb_andamentoservico_id: z.number(), - descricao: z.string(), - situacao: z.string(), - tipo: z.string(), - usa_email: z.string() + tb_andamentoservico_id: z.number().optional(), + descricao: z.string().min(1, "Descrição Obrigatória"), + situacao: z.string().min(1, "Situação Obrigatória"), + tipo: z.string().min(1, "Tipo Obrigatória"), + usa_email: z.string().min(1, "Email Obrigatória"), }); \ No newline at end of file