[MVPTN-75] refactor(geral): implementando gerenciador de erro, removendo mock de dados e usando o botao de loading
This commit is contained in:
parent
c7f86522fb
commit
6c1e91035d
8 changed files with 46 additions and 147 deletions
|
|
@ -3,6 +3,7 @@
|
|||
import { useEffect, useState, useCallback } from "react";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
import { useConfirmDialog } from "@/app/_components/confirm_dialog/useConfirmDialog";
|
||||
import { useResponse } from "@/app/_response/ResponseContext"
|
||||
|
||||
import Header from "@/app/_components/structure/Header";
|
||||
import ConfirmDialog from "@/app/_components/confirm_dialog/ConfirmDialog";
|
||||
|
|
@ -16,15 +17,22 @@ import { useTCensecNaturezaLitigioSaveHook } from "../../_hooks/t_censecnatureza
|
|||
import { useTCensecNaturezaLitigioRemoveHook } from "../../_hooks/t_censecnaturezalitigio/useTCensecNaturezaLitigioRemoveHook";
|
||||
|
||||
import { TCensecNaturezaLitigioInterface } from "../../_interfaces/TCensecNaturezaLitigioInterface";
|
||||
import { SituacoesEnum } from "@/enums/SituacoesEnum";
|
||||
|
||||
const initialCensecNaturezaLitigio: TCensecNaturezaLitigioInterface = {
|
||||
censec_naturezalitigio_id: 0,
|
||||
descricao: "",
|
||||
situacao: 'A'
|
||||
situacao: SituacoesEnum.A
|
||||
}
|
||||
|
||||
export default function TCensecNaturezaLitigioPage() {
|
||||
|
||||
// Controle de exibição de respostas
|
||||
const { setResponse } = useResponse();
|
||||
|
||||
// Controle de estado do botão
|
||||
const [buttonIsLoading, setButtonIsLoading] = useState(false);
|
||||
|
||||
// Hooks
|
||||
const { tCensecNaturezaLitigio, fetchTCensecNaturezaLitigio } = useTCensecNaturezaLitigioReadHook();
|
||||
const { saveTCensecNaturezaLitigio } = useTCensecNaturezaLitigioSaveHook();
|
||||
|
|
@ -39,7 +47,6 @@ export default function TCensecNaturezaLitigioPage() {
|
|||
const {
|
||||
isOpen: isConfirmOpen,
|
||||
openDialog: openConfirmDialog,
|
||||
handleConfirm,
|
||||
handleCancel,
|
||||
} = useConfirmDialog();
|
||||
|
||||
|
|
@ -57,22 +64,32 @@ export default function TCensecNaturezaLitigioPage() {
|
|||
|
||||
// Salvar item
|
||||
const handleSave = useCallback(async (formData: TCensecNaturezaLitigioInterface) => {
|
||||
// Coloca o botão em estado de loading
|
||||
setButtonIsLoading(true);
|
||||
await saveTCensecNaturezaLitigio(formData);
|
||||
console.log(formData)
|
||||
// Remove o botão do estado de loading
|
||||
setButtonIsLoading(false);
|
||||
await fetchTCensecNaturezaLitigio();
|
||||
}, [saveTCensecNaturezaLitigio, fetchTCensecNaturezaLitigio]);
|
||||
|
||||
// Confirmar remoção
|
||||
const handleConfirmDelete = useCallback((item: TCensecNaturezaLitigioInterface) => {
|
||||
console.log("item", item)
|
||||
setItemToDelete(item);
|
||||
openConfirmDialog();
|
||||
}, [openConfirmDialog]);
|
||||
|
||||
// Executar remoção
|
||||
const handleDelete = useCallback(async () => {
|
||||
if (!itemToDelete) return;
|
||||
console.log("item to delete",itemToDelete)
|
||||
if (!itemToDelete) {
|
||||
|
||||
// Define os dados da resposta visual
|
||||
setResponse({
|
||||
status: 400,
|
||||
message: 'Não foi informado um registro para exclusão'
|
||||
});
|
||||
return;
|
||||
};
|
||||
|
||||
await removeTCensecNaturezaLitigio(itemToDelete);
|
||||
await fetchTCensecNaturezaLitigio();
|
||||
setItemToDelete(null);
|
||||
|
|
@ -128,6 +145,7 @@ export default function TCensecNaturezaLitigioPage() {
|
|||
data={selectedItem}
|
||||
onClose={handleCloseForm}
|
||||
onSave={handleSave}
|
||||
buttonIsLoading={buttonIsLoading}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import { useEffect } from 'react';
|
|||
import { useForm, Controller } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
|
||||
import LoadingButton from '@/app/_components/loadingButton/LoadingButton';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Checkbox } from '@/components/ui/checkbox';
|
||||
import {
|
||||
|
|
@ -36,14 +37,10 @@ interface TCensecNaturezaLitigioFormProps {
|
|||
data: FormValues | null;
|
||||
onClose: (item: null, isFormStatus: boolean) => void;
|
||||
onSave: (data: FormValues) => void;
|
||||
buttonIsLoading: boolean;
|
||||
}
|
||||
|
||||
export default function TCensecNaturezaLitigioForm({
|
||||
isOpen,
|
||||
data,
|
||||
onClose,
|
||||
onSave
|
||||
}: TCensecNaturezaLitigioFormProps) {
|
||||
export default function TCensecNaturezaLitigioForm({isOpen, data, onClose, onSave, buttonIsLoading}: TCensecNaturezaLitigioFormProps) {
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(TCensecNaturezaLitigioSchema),
|
||||
defaultValues: {
|
||||
|
|
@ -113,9 +110,8 @@ export default function TCensecNaturezaLitigioForm({
|
|||
Cancelar
|
||||
</Button>
|
||||
</DialogClose>
|
||||
<Button type="submit">
|
||||
Salvar
|
||||
</Button>
|
||||
{/* Botão de loading */}
|
||||
<LoadingButton text="Salvar" textLoading="Aguarde..." type="submit" loading={buttonIsLoading} />
|
||||
</DialogFooter>
|
||||
|
||||
{/* Campo oculto */}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,8 @@
|
|||
import API from "@/services/api/Api";
|
||||
import { Methods } from "@/services/api/enums/ApiMethodEnum";
|
||||
import CensecNaturezaLitigioMockDeDados from "./mockCensecNaturezaLitigio";
|
||||
|
||||
const useMock = false
|
||||
|
||||
export default async function TCensecNaturezaLitigioIndexData() {
|
||||
|
||||
if (useMock) {
|
||||
return await CensecNaturezaLitigioMockDeDados();
|
||||
}
|
||||
|
||||
const api = new API();
|
||||
try {
|
||||
const dados = await api.send({
|
||||
|
|
|
|||
|
|
@ -1,113 +0,0 @@
|
|||
export default async function CensecNaturezaLitigioMockDeDados() {
|
||||
return Promise.resolve({
|
||||
status: 200,
|
||||
message: 'Dados localizados',
|
||||
data: [
|
||||
{
|
||||
censec_naturezaltigio_id: 1,
|
||||
descricao: "Bancário",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 2,
|
||||
descricao: "Concessionária de Água",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 3,
|
||||
descricao: "Concessionária de Gás",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 4,
|
||||
descricao: "Concessionária de Luz",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 5,
|
||||
descricao: "Consumidor",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 6,
|
||||
descricao: "Contrato",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 7,
|
||||
descricao: "Empresarial",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 8,
|
||||
descricao: "Família",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 9,
|
||||
descricao: "Locação",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 10,
|
||||
descricao: "Mobiliário",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 11,
|
||||
descricao: "Previdência",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 12,
|
||||
descricao: "Saúde",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 13,
|
||||
descricao: "Seguro",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 14,
|
||||
descricao: "Serviço Público",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 15,
|
||||
descricao: "Sucessões",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 16,
|
||||
descricao: "Telefonia",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 17,
|
||||
descricao: "Transporte",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 18,
|
||||
descricao: "Transporte - Avião",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 19,
|
||||
descricao: "Transporte - Barco",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 20,
|
||||
descricao: "Transporte Metrô",
|
||||
situacao: "A"
|
||||
},
|
||||
{
|
||||
censec_naturezaltigio_id: 21,
|
||||
descricao: "Transporte - Ônibus",
|
||||
situacao: "A"
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
|
@ -2,6 +2,6 @@ import { z } from 'zod';
|
|||
|
||||
export const TCensecNaturezaLitigioSchema = z.object({
|
||||
censec_naturezalitigio_id: z.number().optional(),
|
||||
descricao: z.string(),
|
||||
descricao: z.string().min(1, 'O campo deve ser preenchido'),
|
||||
situacao: z.enum(['A', 'I']),
|
||||
});
|
||||
|
|
@ -1,12 +1,11 @@
|
|||
import { withClientErrorHandler } from "@/actions/withClientErrorHandler/withClientErrorHandler";
|
||||
import TCensecNaturezaLitigioIndexData from "../../_data/TCensecNaturezaLitigio/TCensecNaturezaLitigioIndexData";
|
||||
|
||||
export default async function TCensecNaturezaLitigioIndexService() {
|
||||
async function executeTCensecNaturezaLitigioIndexService() {
|
||||
|
||||
try {
|
||||
const response = await TCensecNaturezaLitigioIndexData();
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const TCensecNaturezaLitigioIndexService = withClientErrorHandler(executeTCensecNaturezaLitigioIndexService)
|
||||
|
|
@ -1,10 +1,13 @@
|
|||
import TCensecNaturezaLitigioRemoveData from "../../_data/TCensecNaturezaLitigio/TCensecNaturezaLitigioRemoveData";
|
||||
import { TCensecNaturezaLitigioInterface } from "../../_interfaces/TCensecNaturezaLitigioInterface";
|
||||
import { withClientErrorHandler } from "@/actions/withClientErrorHandler/withClientErrorHandler";
|
||||
|
||||
export default async function TCensecNaturezaLitigioRemoveService(data: TCensecNaturezaLitigioInterface) {
|
||||
async function executeTCensecNaturezaLitigioRemoveService(data: TCensecNaturezaLitigioInterface) {
|
||||
|
||||
const response = await TCensecNaturezaLitigioRemoveData(data);
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const TCensecNaturezaLitigioRemoveService = withClientErrorHandler(executeTCensecNaturezaLitigioRemoveService)
|
||||
|
|
@ -1,10 +1,13 @@
|
|||
import { withClientErrorHandler } from "@/actions/withClientErrorHandler/withClientErrorHandler";
|
||||
import TCensecNaturezaLitigioSaveData from "../../_data/TCensecNaturezaLitigio/TCensecNaturezaLitigioSaveData";
|
||||
import { TCensecNaturezaLitigioInterface } from "../../_interfaces/TCensecNaturezaLitigioInterface";
|
||||
|
||||
export default async function TCensecNaturezaLitigioSaveService(data: TCensecNaturezaLitigioInterface) {
|
||||
async function executeTCensecNaturezaLitigioSaveService(data: TCensecNaturezaLitigioInterface) {
|
||||
|
||||
const response = await TCensecNaturezaLitigioSaveData(data);
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const TCensecNaturezaLitigioSaveService = withClientErrorHandler(executeTCensecNaturezaLitigioSaveService)
|
||||
Loading…
Add table
Reference in a new issue