fix(Review): Revisão de código para refatoração
This commit is contained in:
parent
634fe16331
commit
c7f86522fb
14 changed files with 100 additions and 58 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,15 +16,22 @@ import { useGTBBairroSaveHook } from "../../_hooks/g_tb_bairro/useGTBBairroSaveH
|
|||
import { useGTBBairroRemoveHook } from "../../_hooks/g_tb_bairro/useGTBBairroRemoveHook";
|
||||
|
||||
import { GTBBairroInterface } from "../../_interfaces/GTBBairroInterface";
|
||||
import { SituacoesEnum } from "@/enums/SituacoesEnum";
|
||||
|
||||
const initialBairro: GTBBairroInterface = {
|
||||
sistema_id: null,
|
||||
tb_bairro_id: 0,
|
||||
descricao: '',
|
||||
situacao: 'A'
|
||||
situacao: SituacoesEnum.A
|
||||
}
|
||||
|
||||
export default function TTBAndamentoServico() {
|
||||
export default function GTBBairroPage() {
|
||||
|
||||
// Controle de exibição de respostas
|
||||
const { setResponse } = useResponse();
|
||||
|
||||
// Controle de estado do botão
|
||||
const [buttonIsLoading, setButtonIsLoading] = useState(false);
|
||||
|
||||
// Hooks para leitura e salvamento
|
||||
const { gTBBairro, fetchGTBBairro } = useGTBBairroReadHook();
|
||||
|
|
@ -39,7 +47,6 @@ export default function TTBAndamentoServico() {
|
|||
const {
|
||||
isOpen: isConfirmOpen,
|
||||
openDialog: openConfirmDialog,
|
||||
handleConfirm,
|
||||
handleCancel,
|
||||
} = useConfirmDialog();
|
||||
|
||||
|
|
@ -55,8 +62,19 @@ export default function TTBAndamentoServico() {
|
|||
}, []);
|
||||
|
||||
const handleSave = useCallback(async (data: GTBBairroInterface) => {
|
||||
|
||||
// Coloca o botão em estado de loading
|
||||
setButtonIsLoading(true);
|
||||
|
||||
// Realiza o procedimento desejado
|
||||
await saveGTBBairro(data);
|
||||
fetchGTBBairro(); // Atualiza a tabela após salvar
|
||||
|
||||
// Remove o botão do estado de loading
|
||||
setButtonIsLoading(false);
|
||||
|
||||
// Atualiza a tabela após salvar
|
||||
fetchGTBBairro();
|
||||
|
||||
}, [saveGTBBairro, fetchGTBBairro]);
|
||||
|
||||
// Ações de deleção
|
||||
|
|
@ -66,14 +84,29 @@ export default function TTBAndamentoServico() {
|
|||
}, [openConfirmDialog]);
|
||||
|
||||
const handleDelete = useCallback(async () => {
|
||||
if (!itemToDelete) return;
|
||||
|
||||
// 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;
|
||||
};
|
||||
|
||||
// Executa o hook de remoção
|
||||
await removeGTBBairro(itemToDelete);
|
||||
await fetchGTBBairro(); // Atualiza a tabela após remover
|
||||
|
||||
// Atualiza a tabela após remover
|
||||
await fetchGTBBairro();
|
||||
|
||||
// Limpa o item para remoção
|
||||
setItemToDelete(null)
|
||||
|
||||
handleCancel();
|
||||
|
||||
}, [itemToDelete, fetchGTBBairro, handleCancel]);
|
||||
|
||||
// Efeito para carregar os dados na montagem do componente
|
||||
|
|
@ -81,7 +114,10 @@ export default function TTBAndamentoServico() {
|
|||
fetchGTBBairro();
|
||||
}, []);
|
||||
|
||||
if (!gTBBairro) {
|
||||
/**
|
||||
* Tela de loading enquanto carrega os dados
|
||||
*/
|
||||
if (gTBBairro.length == 0) {
|
||||
return <Loading type={2} />;
|
||||
}
|
||||
|
||||
|
|
@ -124,6 +160,7 @@ export default function TTBAndamentoServico() {
|
|||
data={selectedBairro}
|
||||
onClose={handleCloseForm}
|
||||
onSave={handleSave}
|
||||
buttonIsLoading={buttonIsLoading}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import { useTTBAndamentoServicoDeleteHook } from "../../_hooks/t_tb_andamentoser
|
|||
import Header from "@/app/_components/structure/Header";
|
||||
|
||||
export default function TTBAndamentoServico() {
|
||||
|
||||
// Hooks para leitura e salvamento
|
||||
const { tTBAndamentosServicos, fetchTTBAndamentoServico } = useTTBAndamentoServicoReadHook();
|
||||
const { saveTTBAndamentoServico } = useTTBAndamentoServicoSaveHook();
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
'use client';
|
||||
|
||||
import z from "zod";
|
||||
import { useEffect } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useForm, Controller } from "react-hook-form";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
|
||||
|
|
@ -28,7 +28,8 @@ import { Input } from "@/components/ui/input";
|
|||
import { Label } from "@/components/ui/label";
|
||||
|
||||
import { GTBBairroSchema } from "../../_schemas/GTBBairroSchema";
|
||||
import { GTBBairroInterface } from "../../_interfaces/GTBBairroInterface";
|
||||
import LoadingButton from "@/app/_components/loadingButton/LoadingButton";
|
||||
import { SituacoesEnum } from "@/enums/SituacoesEnum";
|
||||
|
||||
type FormValues = z.infer<typeof GTBBairroSchema>;
|
||||
|
||||
|
|
@ -37,9 +38,11 @@ interface GTBBairroFormProps {
|
|||
data: FormValues | null;
|
||||
onClose: (item: null, isFormStatus: boolean) => void;
|
||||
onSave: (data: FormValues) => void;
|
||||
buttonIsLoading: boolean;
|
||||
}
|
||||
|
||||
export default function GTBBairroForm({ isOpen, data, onClose, onSave }: GTBBairroFormProps) {
|
||||
export default function GTBBairroForm({ isOpen, data, onClose, onSave, buttonIsLoading }: GTBBairroFormProps) {
|
||||
|
||||
// Inicializa o react-hook-form com o schema Zod
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(GTBBairroSchema),
|
||||
|
|
@ -47,7 +50,7 @@ export default function GTBBairroForm({ isOpen, data, onClose, onSave }: GTBBair
|
|||
sistema_id: null,
|
||||
tb_bairro_id: 0,
|
||||
descricao: "",
|
||||
situacao: "A",
|
||||
situacao: SituacoesEnum.A,
|
||||
},
|
||||
});
|
||||
|
||||
|
|
@ -113,14 +116,14 @@ export default function GTBBairroForm({ isOpen, data, onClose, onSave }: GTBBair
|
|||
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>
|
||||
|
||||
{/* Campos ocultos */}
|
||||
<input type="hidden" {...form.register("tb_bairro_id")} />
|
||||
<input type="hidden" {...form.register("sistema_id")} />
|
||||
|
||||
</form>
|
||||
</Form>
|
||||
</DialogContent>
|
||||
|
|
|
|||
|
|
@ -1,25 +1,15 @@
|
|||
import API from "@/services/api/Api";
|
||||
import { Methods } from "@/services/api/enums/ApiMethodEnum";
|
||||
import BairroMockDeDados from "./mockBairro";
|
||||
|
||||
const useMock = false
|
||||
|
||||
export default async function GTBBairroIndexData() {
|
||||
if (useMock) {
|
||||
console.log(BairroMockDeDados())
|
||||
return await BairroMockDeDados();
|
||||
}
|
||||
|
||||
const api = new API();
|
||||
try {
|
||||
const dados = await api.send({
|
||||
method: Methods.GET,
|
||||
endpoint: `administrativo/g_tb_bairro/`
|
||||
});
|
||||
return dados
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return error
|
||||
}
|
||||
|
||||
const dados = await api.send({
|
||||
method: Methods.GET,
|
||||
endpoint: `administrativo/g_tb_bairro/`
|
||||
});
|
||||
|
||||
return dados
|
||||
|
||||
}
|
||||
|
|
@ -1,24 +1,27 @@
|
|||
'use client'
|
||||
|
||||
import { useResponse } from "@/app/_response/ResponseContext"
|
||||
import { useState } from "react";
|
||||
import { use, useState } from "react";
|
||||
import { GTBBairroInterface } from "../../_interfaces/GTBBairroInterface";
|
||||
import GTBBairroIndexService from "../../_services/g_tb_bairro/GTBBairroIndexService";
|
||||
|
||||
export const useGTBBairroReadHook = () => {
|
||||
|
||||
const { setResponse } = useResponse();
|
||||
|
||||
// Controle dos dados obtidos via API
|
||||
const [gTBBairro, setGTBBairro] = useState<GTBBairroInterface[]>([]);
|
||||
|
||||
const fetchGTBBairro = async () => {
|
||||
|
||||
try {
|
||||
const response = await GTBBairroIndexService();
|
||||
// Realiza a requisição para a api
|
||||
const response = await GTBBairroIndexService();
|
||||
|
||||
setGTBBairro(response.data);
|
||||
// Armazena os dados da resposta
|
||||
setGTBBairro(response.data);
|
||||
|
||||
setResponse(response);
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
// Envia os dados da resposta para ser tratado
|
||||
setResponse(response);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ export const useGTBBairroSaveHook = () => {
|
|||
|
||||
const { setResponse } = useResponse();
|
||||
const [gTBBairro, setGTBBairro] = useState<GTBBairroInterface | null>(null);
|
||||
|
||||
// controla se o formulário está aberto ou fechado
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import { SituacoesEnum } from "@/enums/SituacoesEnum";
|
||||
|
||||
export interface GTBBairroInterface {
|
||||
sistema_id: number | null;
|
||||
tb_bairro_id: number;
|
||||
descricao: string;
|
||||
situacao: 'A' | 'I';
|
||||
situacao: SituacoesEnum;
|
||||
}
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
import { SituacoesEnum } from '@/enums/SituacoesEnum';
|
||||
import { z } from 'zod';
|
||||
|
||||
export const GTBBairroSchema = z.object({
|
||||
sistema_id: z.number().nullable(),
|
||||
tb_bairro_id: z.number(),
|
||||
descricao: z.string(),
|
||||
situacao: z.enum(['A', 'I']),
|
||||
sistema_id: z.number().nullable(),
|
||||
descricao: z.string().min(1, 'O campo deve ser preenchido'),
|
||||
situacao: z.enum(SituacoesEnum),
|
||||
});
|
||||
|
|
@ -2,11 +2,7 @@ import GTBBairroIndexData from "../../_data/GTBBairro/GTBBairroIndexData";
|
|||
|
||||
export default async function GTBBairroIndexService() {
|
||||
|
||||
try {
|
||||
const response = await GTBBairroIndexData();
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return error
|
||||
}
|
||||
const response = await GTBBairroIndexData();
|
||||
return response;
|
||||
|
||||
}
|
||||
|
|
@ -5,8 +5,6 @@ export default async function GTBBairroSaveService(data: GTBBairroInterface) {
|
|||
|
||||
const response = await GTBBairroSaveData(data);
|
||||
|
||||
console.log('GTBRegimeComunhaoSaveData', response)
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ const LoadingButton = forwardRef<HTMLButtonElement, LoadingButtonProps>(
|
|||
aria-busy={loading}
|
||||
aria-live="polite"
|
||||
className={clsx(
|
||||
"flex items-center justify-center gap-2 w-full my-3 cursor-pointer",
|
||||
"cursor-pointer",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { Button } from "@/components/ui/button";
|
||||
import Link from "next/link";
|
||||
import { PlusIcon } from "lucide-react";
|
||||
|
||||
interface HeaderProps {
|
||||
|
|
@ -23,10 +22,9 @@ export default function Header({ title, description, buttonText, buttonAction }:
|
|||
{description}
|
||||
</p>
|
||||
</div>
|
||||
<Button asChild>
|
||||
<Link href="/cadastros/minuta/formulario">
|
||||
Nova Minuta
|
||||
</Link>
|
||||
<Button onClick={() => buttonAction()} className="cursor-pointer">
|
||||
<PlusIcon className="mr-2" />
|
||||
{buttonText}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -30,6 +30,12 @@ export default function Response() {
|
|||
});
|
||||
break;
|
||||
|
||||
case 400:
|
||||
toast.error(response.error, {
|
||||
description: response.message
|
||||
});
|
||||
break;
|
||||
|
||||
case 600:
|
||||
toast.error(response.error, {
|
||||
description: response.message
|
||||
|
|
|
|||
6
src/enums/SituacoesEnum.ts
Normal file
6
src/enums/SituacoesEnum.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
export enum SituacoesEnum {
|
||||
|
||||
A = 'A',
|
||||
I = 'I',
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue