[MVPTN-74] refactor(geral): implementando gerenciador de erro, removendo mock de dados e usando o botao de loading
This commit is contained in:
parent
c7f86522fb
commit
daeb956ef8
7 changed files with 47 additions and 60 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";
|
||||
|
|
@ -15,6 +16,7 @@ import { useGMedidaTipoSaveHook } from "../../_hooks/g_medidatipo/useGMedidaTipo
|
|||
import { useGMedidaTipoRemoveHook } from "../../_hooks/g_medidatipo/useGMedidaTipoRemoveHook";
|
||||
|
||||
import { GMedidaTipoInterface } from "../../_interfaces/GMedidaTipoInterface";
|
||||
import { SituacoesEnum } from "@/enums/SituacoesEnum";
|
||||
|
||||
const initialMedidaTipo: GMedidaTipoInterface = {
|
||||
medida_tipo_id: 0,
|
||||
|
|
@ -24,6 +26,12 @@ const initialMedidaTipo: GMedidaTipoInterface = {
|
|||
|
||||
export default function GMedidaTipoPage() {
|
||||
|
||||
// Controle de exibição de respostas
|
||||
const { setResponse } = useResponse();
|
||||
|
||||
// Controle de estado do botão
|
||||
const [buttonIsLoading, setButtonIsLoading] = useState(false);
|
||||
|
||||
// Hooks para leitura, salvamento e remoção
|
||||
const { gMedidaTipo, fetchGMedidaTipo } = useGMedidaTipoReadHook();
|
||||
const { saveGMedidaTipo } = useGMedidaTipoSaveHook();
|
||||
|
|
@ -38,7 +46,6 @@ export default function GMedidaTipoPage() {
|
|||
const {
|
||||
isOpen: isConfirmOpen,
|
||||
openDialog: openConfirmDialog,
|
||||
handleConfirm,
|
||||
handleCancel,
|
||||
} = useConfirmDialog();
|
||||
|
||||
|
|
@ -54,7 +61,11 @@ export default function GMedidaTipoPage() {
|
|||
}, []);
|
||||
|
||||
const handleSave = useCallback(async (data: GMedidaTipoInterface) => {
|
||||
// Coloca o botão em estado de loading
|
||||
setButtonIsLoading(true);
|
||||
await saveGMedidaTipo(data);
|
||||
// Remove o botão do estado de loading
|
||||
setButtonIsLoading(false);
|
||||
await fetchGMedidaTipo(); // Atualiza a tabela após salvar
|
||||
handleCloseForm();
|
||||
}, [saveGMedidaTipo, fetchGMedidaTipo]);
|
||||
|
|
@ -66,10 +77,20 @@ export default function GMedidaTipoPage() {
|
|||
}, [openConfirmDialog]);
|
||||
|
||||
const handleDelete = useCallback(async () => {
|
||||
if (itemToDelete) {
|
||||
await removeGMedidaTipo(itemToDelete);
|
||||
await fetchGMedidaTipo(); // Atualiza a tabela após remover
|
||||
}
|
||||
|
||||
// Verifica se existe item para remoção
|
||||
if (!itemToDelete) {
|
||||
|
||||
// Define os dados da resposta visual
|
||||
setResponse({
|
||||
status: 400,
|
||||
message: 'Não foi informado um registro para exclusão'
|
||||
});
|
||||
return;
|
||||
};
|
||||
|
||||
await removeGMedidaTipo(itemToDelete);
|
||||
await fetchGMedidaTipo(); // Atualiza a tabela após remover
|
||||
handleCancel();
|
||||
}, [itemToDelete, fetchGMedidaTipo, handleCancel]);
|
||||
|
||||
|
|
@ -122,6 +143,7 @@ export default function GMedidaTipoPage() {
|
|||
data={selectedMedidaTipo}
|
||||
onClose={handleCloseForm}
|
||||
onSave={handleSave}
|
||||
buttonIsLoading={buttonIsLoading}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ import {
|
|||
FormMessage
|
||||
} from "@/components/ui/form";
|
||||
import { Input } from "@/components/ui/input";
|
||||
|
||||
import LoadingButton from "@/app/_components/loadingButton/LoadingButton";
|
||||
import { GMedidaTipoSchema } from "../../_schemas/GMedidaTipoSchema";
|
||||
import { GMedidaTipoInterface } from "../../_interfaces/GMedidaTipoInterface";
|
||||
|
||||
|
|
@ -35,9 +35,10 @@ interface GMedidaTipoFormProps {
|
|||
data: FormValues | null;
|
||||
onClose: (item: null, isFormStatus: boolean) => void;
|
||||
onSave: (data: FormValues) => void;
|
||||
buttonIsLoading: boolean;
|
||||
}
|
||||
|
||||
export default function GMedidaTipoForm({ isOpen, data, onClose, onSave }: GMedidaTipoFormProps) {
|
||||
export default function GMedidaTipoForm({ isOpen, data, onClose, onSave, buttonIsLoading }: GMedidaTipoFormProps) {
|
||||
// Inicializa o react-hook-form com o schema Zod
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(GMedidaTipoSchema),
|
||||
|
|
@ -110,9 +111,8 @@ export default function GMedidaTipoForm({ isOpen, data, onClose, onSave }: GMedi
|
|||
Cancelar
|
||||
</Button>
|
||||
</DialogClose>
|
||||
<Button type="submit" className="cursor-pointer">
|
||||
Salvar
|
||||
</Button>
|
||||
{/* Botão de loading */}
|
||||
<LoadingButton text="Salvar" textLoading="Aguarde..." type="submit" loading={buttonIsLoading} />
|
||||
</DialogFooter>
|
||||
|
||||
{/* Campo oculto */}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,7 @@
|
|||
import API from "@/services/api/Api";
|
||||
import { Methods } from "@/services/api/enums/ApiMethodEnum";
|
||||
import MedidaTipoMockDeDados from "./mockMedidaTipo";
|
||||
|
||||
const useMock = true
|
||||
|
||||
export default async function GMedidaTipoIndexData() {
|
||||
if (useMock) {
|
||||
console.log(MedidaTipoMockDeDados())
|
||||
return await MedidaTipoMockDeDados();
|
||||
}
|
||||
|
||||
const api = new API();
|
||||
try {
|
||||
const dados = await api.send({
|
||||
|
|
|
|||
|
|
@ -1,28 +0,0 @@
|
|||
export default async function MedidaTipoMockDeDados() {
|
||||
return Promise.resolve({
|
||||
status: 200,
|
||||
message: 'Dados localizados',
|
||||
data: [
|
||||
{
|
||||
medida_tipo_id: 1.00,
|
||||
sigla: "Alqueire",
|
||||
descricao: "Alqueire"
|
||||
},
|
||||
{
|
||||
medida_tipo_id: 2.00,
|
||||
sigla: "ha",
|
||||
descricao: "Hectare"
|
||||
},
|
||||
{
|
||||
medida_tipo_id: 3.00,
|
||||
sigla: "m2",
|
||||
descricao: "Metros Quadrados"
|
||||
},
|
||||
{
|
||||
medida_tipo_id: 4.00,
|
||||
sigla: "braça",
|
||||
descricao: "Braça"
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
|
@ -1,12 +1,9 @@
|
|||
import { withClientErrorHandler } from "@/actions/withClientErrorHandler/withClientErrorHandler";
|
||||
import GMedidaTipoIndexData from "../../_data/GMedidoTipo/GMedidaTipoIndexData";
|
||||
|
||||
export default async function GMedidaTipoIndexService() {
|
||||
|
||||
try {
|
||||
async function executeGMedidaTipoIndexService() {
|
||||
const response = await GMedidaTipoIndexData();
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const GMedidaTipoIndexService = withClientErrorHandler(executeGMedidaTipoIndexService)
|
||||
|
|
@ -1,10 +1,13 @@
|
|||
import { withClientErrorHandler } from "@/actions/withClientErrorHandler/withClientErrorHandler";
|
||||
import GMedidaTipoRemoveData from "../../_data/GMedidoTipo/GMedidaTipoRemoveData";
|
||||
import { GMedidaTipoInterface } from "../../_interfaces/GMedidaTipoInterface";
|
||||
|
||||
export default async function GMedidaTipoRemoveService(data: GMedidaTipoInterface) {
|
||||
async function executeGMedidaTipoRemoveService(data: GMedidaTipoInterface) {
|
||||
|
||||
const response = await GMedidaTipoRemoveData(data);
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const GMedidaTipoRemoveService = withClientErrorHandler(executeGMedidaTipoRemoveService)
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
import { withClientErrorHandler } from "@/actions/withClientErrorHandler/withClientErrorHandler";
|
||||
import GMedidaTipoSaveData from "../../_data/GMedidoTipo/GMedidaTipoSaveData";
|
||||
import { GMedidaTipoInterface } from "../../_interfaces/GMedidaTipoInterface";
|
||||
|
||||
export default async function GMedidaTipoSaveService(data: GMedidaTipoInterface) {
|
||||
async function executeGMedidaTipoSaveService(data: GMedidaTipoInterface) {
|
||||
|
||||
const response = await GMedidaTipoSaveData(data);
|
||||
|
||||
console.log('GTBRegimeComunhaoSaveData', response)
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const GMedidaTipoSaveService = withClientErrorHandler(executeGMedidaTipoSaveService)
|
||||
Loading…
Add table
Reference in a new issue