[MVPTN-72] refactor(Geral): Conecta o crud com os endpoints definitivos, aplica o botão de loading, encapsula as funções para trativas de erros
This commit is contained in:
parent
c7f86522fb
commit
eeab66149f
11 changed files with 80 additions and 62 deletions
|
|
@ -18,6 +18,10 @@ import TCensecInterface from "../../_interfaces/TCensecInterface";
|
|||
import Header from "@/app/_components/structure/Header";
|
||||
|
||||
export default function TTBAndamentoServico() {
|
||||
|
||||
// Controle de estado do botão
|
||||
const [buttonIsLoading, setButtonIsLoading] = useState(false);
|
||||
|
||||
// Hooks para leitura e salvamento
|
||||
const { tCensec, fetchTCensec } = useTCensecReadHook();
|
||||
const { saveTCensec } = useTCensecSaveHook();
|
||||
|
|
@ -61,9 +65,15 @@ export default function TTBAndamentoServico() {
|
|||
*/
|
||||
const handleSave = useCallback(async (formData: TCensecInterface) => {
|
||||
|
||||
// Coloca o botão em estado de loading
|
||||
setButtonIsLoading(true);
|
||||
|
||||
// Aguarda salvar o registro
|
||||
await saveTCensec(formData);
|
||||
|
||||
// Remove o botão em estado de loading
|
||||
setButtonIsLoading(false);
|
||||
|
||||
// Atualiza a lista de dados
|
||||
fetchTCensec();
|
||||
|
||||
|
|
@ -114,7 +124,7 @@ export default function TTBAndamentoServico() {
|
|||
/**
|
||||
* Tela de loading enquanto carrega os dados
|
||||
*/
|
||||
if (!tCensec) {
|
||||
if (tCensec.length == 0) {
|
||||
return <Loading type={2} />;
|
||||
}
|
||||
|
||||
|
|
@ -157,6 +167,7 @@ export default function TTBAndamentoServico() {
|
|||
data={selectedAndamento}
|
||||
onClose={handleCloseForm}
|
||||
onSave={handleSave}
|
||||
buttonIsLoading={buttonIsLoading}
|
||||
/>
|
||||
</div>
|
||||
); 4
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ import { Input } from "@/components/ui/input";
|
|||
import { Label } from "@/components/ui/label";
|
||||
|
||||
import { TCensecSchema } from "../../_schemas/TCensecSchema";
|
||||
import { SituacoesEnum } from "@/enums/SituacoesEnum";
|
||||
import LoadingButton from "@/app/_components/loadingButton/LoadingButton";
|
||||
|
||||
type FormValues = z.infer<typeof TCensecSchema>;
|
||||
|
||||
|
|
@ -36,15 +38,16 @@ interface Props {
|
|||
data: FormValues | null;
|
||||
onClose: (item: null, isFormStatus: boolean) => void;
|
||||
onSave: (data: FormValues) => void;
|
||||
buttonIsLoading: boolean;
|
||||
}
|
||||
|
||||
export default function TCensecForm({ isOpen, data, onClose, onSave }: Props) {
|
||||
export default function TCensecForm({ isOpen, data, onClose, onSave, buttonIsLoading }: Props) {
|
||||
// Inicializa o react-hook-form com schema zod
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(TCensecSchema),
|
||||
defaultValues: {
|
||||
descricao: "",
|
||||
situacao: "A",
|
||||
situacao: SituacoesEnum.A,
|
||||
censec_id: 0,
|
||||
},
|
||||
});
|
||||
|
|
@ -112,9 +115,8 @@ export default function TCensecForm({ isOpen, data, onClose, onSave }: Props) {
|
|||
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,10 +1,17 @@
|
|||
import API from "@/services/api/Api";
|
||||
import TCensecInterface from "../../_interfaces/TCensecInterface";
|
||||
import { Methods } from "@/services/api/enums/ApiMethodEnum";
|
||||
import { withClientErrorHandler } from "@/actions/withClientErrorHandler/withClientErrorHandler";
|
||||
|
||||
export default async function TCensecDeleteData(data: TCensecInterface) {
|
||||
async function executeTCensecDeleteData(data: TCensecInterface) {
|
||||
|
||||
return Promise.resolve({
|
||||
message: 'Dados removidos',
|
||||
status: 200
|
||||
const api = new API();
|
||||
|
||||
return await api.send({
|
||||
method: Methods.DELETE,
|
||||
endpoint: `administrativo/t_censec/${data.censec_id}`,
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const TCensecDeleteData = withClientErrorHandler(executeTCensecDeleteData);
|
||||
|
|
@ -1,30 +1,16 @@
|
|||
export default async function TCensecIndexData() {
|
||||
import { withClientErrorHandler } from "@/actions/withClientErrorHandler/withClientErrorHandler";
|
||||
import API from "@/services/api/Api";
|
||||
import { Methods } from "@/services/api/enums/ApiMethodEnum";
|
||||
|
||||
return Promise.resolve({
|
||||
message: 'Dados localizados',
|
||||
status: 200,
|
||||
data: [
|
||||
{
|
||||
censec_id: 1.00,
|
||||
descricao: 'CEP - Escritura e Procurações Diversas',
|
||||
situacao: 'A'
|
||||
},
|
||||
{
|
||||
censec_id: 2.00,
|
||||
descricao: 'CESDI - Escrituras Lei 11.441',
|
||||
situacao: 'A'
|
||||
},
|
||||
{
|
||||
censec_id: 3.00,
|
||||
descricao: 'RCTO - Testamentos',
|
||||
situacao: 'A'
|
||||
},
|
||||
{
|
||||
censec_id: 9.00,
|
||||
descricao: 'Não Possui',
|
||||
situacao: 'A'
|
||||
}
|
||||
]
|
||||
async function executeTCensecIndexData() {
|
||||
|
||||
const api = new API();
|
||||
|
||||
return await api.send({
|
||||
method: Methods.GET,
|
||||
endpoint: `administrativo/t_censec/`,
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const TCensecIndexData = withClientErrorHandler(executeTCensecIndexData);
|
||||
|
|
@ -1,15 +1,20 @@
|
|||
import API from "@/services/api/Api";
|
||||
import { Methods } from "@/services/api/enums/ApiMethodEnum";
|
||||
import TCensecInterface from "../../_interfaces/TCensecInterface";
|
||||
import { withClientErrorHandler } from "@/actions/withClientErrorHandler/withClientErrorHandler";
|
||||
|
||||
export default async function TCensecSaveData(data: TCensecInterface) {
|
||||
async function executeTCensecSaveData(data: TCensecInterface) {
|
||||
|
||||
return Promise.resolve({
|
||||
message: 'Dados salvos',
|
||||
status: 201,
|
||||
data: {
|
||||
censec_id: 9.00,
|
||||
descricao: 'Não Possui',
|
||||
situacao: 'A'
|
||||
}
|
||||
const isUpdate = Boolean(data.censec_id);
|
||||
|
||||
const api = new API();
|
||||
|
||||
return await api.send({
|
||||
method: isUpdate ? Methods.PUT : Methods.POST,
|
||||
endpoint: `administrativo/t_censec/${data.censec_id || ''}`,
|
||||
body: data
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const TCensecSaveData = withClientErrorHandler(executeTCensecSaveData);
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import { useResponse } from "@/app/_response/ResponseContext"
|
||||
import TCensecInterface from "../../_interfaces/TCensecInterface";
|
||||
import TCensecDeleteService from "../../_services/t_censec/TCensecDeleteService";
|
||||
import { TCensecDeleteService } from "../../_services/t_censec/TCensecDeleteService";
|
||||
|
||||
export const useTCensecDeleteHook = () => {
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { useResponse } from "@/app/_response/ResponseContext"
|
||||
import TCensecIndexService from "../../_services/t_censec/TCensecIndexService";
|
||||
import { TCensecIndexService } from "../../_services/t_censec/TCensecIndexService";
|
||||
import { useState } from "react";
|
||||
import TCensecInterface from "../../_interfaces/TCensecInterface";
|
||||
|
||||
|
|
@ -17,8 +17,6 @@ export const useTCensecReadHook = () => {
|
|||
|
||||
setResponse(response);
|
||||
|
||||
return response
|
||||
|
||||
}
|
||||
|
||||
return { tCensec, fetchTCensec }
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
import { useResponse } from "@/app/_response/ResponseContext"
|
||||
import { useState } from "react";
|
||||
import TCensecInterface from "../../_interfaces/TCensecInterface";
|
||||
import TCensecSaveService from "../../_services/t_censec/TCensecSaveService";
|
||||
import { TCensecSaveService } from "../../_services/t_censec/TCensecSaveService";
|
||||
|
||||
export const useTCensecSaveHook = () => {
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
import TCensecDeleteData from "../../_data/TCensec/TCensecDeleteData";
|
||||
import { withClientErrorHandler } from "@/actions/withClientErrorHandler/withClientErrorHandler";
|
||||
import { TCensecDeleteData } from "../../_data/TCensec/TCensecDeleteData";
|
||||
import TCensecInterface from "../../_interfaces/TCensecInterface";
|
||||
|
||||
export default async function TCensecDeleteService(data: TCensecInterface) {
|
||||
async function executeTCensecDeleteService(data: TCensecInterface) {
|
||||
|
||||
const response = await TCensecDeleteData(data);
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const TCensecDeleteService = withClientErrorHandler(executeTCensecDeleteService);
|
||||
|
|
@ -1,9 +1,12 @@
|
|||
import TCensecIndexData from "../../_data/TCensec/TCensecIndexData";
|
||||
import { withClientErrorHandler } from "@/actions/withClientErrorHandler/withClientErrorHandler";
|
||||
import { TCensecIndexData } from "../../_data/TCensec/TCensecIndexData";
|
||||
|
||||
export default async function TCensecIndexService() {
|
||||
export default async function executeTCensecIndexService() {
|
||||
|
||||
const response = await TCensecIndexData();
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const TCensecIndexService = withClientErrorHandler(executeTCensecIndexService);
|
||||
|
|
@ -1,10 +1,13 @@
|
|||
import TCensecSaveData from "../../_data/TCensec/TCensecSaveData";
|
||||
import { withClientErrorHandler } from "@/actions/withClientErrorHandler/withClientErrorHandler";
|
||||
import { TCensecSaveData } from "../../_data/TCensec/TCensecSaveData";
|
||||
import TCensecInterface from "../../_interfaces/TCensecInterface";
|
||||
|
||||
export default async function TCensecSaveService(data: TCensecInterface) {
|
||||
async function executeTCensecSaveService(data: TCensecInterface) {
|
||||
|
||||
const response = await TCensecSaveData(data);
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const TCensecSaveService = withClientErrorHandler(executeTCensecSaveService);
|
||||
Loading…
Add table
Reference in a new issue