[MVPTN-2] feat(CRUD): Finalização de ações do CRUD
This commit is contained in:
parent
0407483d23
commit
44d20d6f81
7 changed files with 91 additions and 30 deletions
|
|
@ -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<TTBAndamentoServicoInterface | null>(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]);
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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 */}
|
||||
<DialogFooter className="mt-4">
|
||||
<DialogClose asChild>
|
||||
<Button variant="outline" type="button" onClick={() => onClose(null, false)}>
|
||||
<Button variant="outline" type="button" onClick={() => onClose(null, false)} className="cursor-pointer">
|
||||
Cancelar
|
||||
</Button>
|
||||
</DialogClose>
|
||||
<Button type="submit">Salvar</Button>
|
||||
<Button type="submit" className="cursor-pointer">
|
||||
Salvar
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
|
||||
{/* Campo oculto */}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<TTBAndamentoServicoInteface>();
|
||||
|
||||
const deleteTTBAndamentoServico = async (data: TTBAndamentoServicoInteface) => {
|
||||
|
||||
const response = await TTBAndamentoServicoRemoveData(data);
|
||||
|
||||
setTTBAndamentoServico(data);
|
||||
|
||||
setResponse(response);
|
||||
|
||||
}
|
||||
|
||||
return { tTBAndamentoServico, deleteTTBAndamentoServico }
|
||||
|
||||
}
|
||||
|
|
@ -11,9 +11,9 @@ export const useTTBAndamentoServicoSaveHook = () => {
|
|||
|
||||
const [tTBAndamentoServico, setTTBAndamentoServico] = useState<TTBAndamentoServicoInteface>();
|
||||
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -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"),
|
||||
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue