From 43d236acf7d2fa87ffc002fa1af94367395d0b13 Mon Sep 17 00:00:00 2001 From: keven Date: Mon, 15 Sep 2025 16:32:24 -0300 Subject: [PATCH] =?UTF-8?q?[MVPTN-66]=20feat(Componente):=20Cria=20compone?= =?UTF-8?q?nte=20de=20bot=C3=A3o=20loading=20que=20=C3=A9=20acionado=20ao?= =?UTF-8?q?=20enviar=20formul=C3=A1rio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../(g_tb_profissao)/profissoes/page.tsx | 1 + .../buttonLoading/LoadingButton.tsx | 37 +++++++++++++++++++ .../buttonLoading/useAsyncAction.ts | 22 +++++++++++ 3 files changed, 60 insertions(+) create mode 100644 src/app/_components/buttonLoading/LoadingButton.tsx create mode 100644 src/app/_components/buttonLoading/useAsyncAction.ts diff --git a/src/app/(protected)/(cadastros)/cadastros/(g_tb_profissao)/profissoes/page.tsx b/src/app/(protected)/(cadastros)/cadastros/(g_tb_profissao)/profissoes/page.tsx index 847d1d2..ceee629 100644 --- a/src/app/(protected)/(cadastros)/cadastros/(g_tb_profissao)/profissoes/page.tsx +++ b/src/app/(protected)/(cadastros)/cadastros/(g_tb_profissao)/profissoes/page.tsx @@ -161,6 +161,7 @@ export default function TTBAndamentoServico() { onClose={handleCloseForm} onSave={handleSave} /> + ); 4 } \ No newline at end of file diff --git a/src/app/_components/buttonLoading/LoadingButton.tsx b/src/app/_components/buttonLoading/LoadingButton.tsx new file mode 100644 index 0000000..6e110e5 --- /dev/null +++ b/src/app/_components/buttonLoading/LoadingButton.tsx @@ -0,0 +1,37 @@ +// components/LoadingButton.tsx +"use client"; + +import { Button } from "@/components/ui/button"; +import { Loader2 } from "lucide-react"; +import { ButtonProps } from "@/components/ui/button"; + +interface LoadingButtonProps extends ButtonProps { + isLoading?: boolean; + loadingText?: string; +} + +export function LoadingButton({ + children, + isLoading = false, + loadingText = "Salvando...", + disabled, + type = "submit", // padrão para funcionar com forms + ...props +}: LoadingButtonProps) { + return ( + + ); +} diff --git a/src/app/_components/buttonLoading/useAsyncAction.ts b/src/app/_components/buttonLoading/useAsyncAction.ts new file mode 100644 index 0000000..a4c2d10 --- /dev/null +++ b/src/app/_components/buttonLoading/useAsyncAction.ts @@ -0,0 +1,22 @@ +// hooks/useAsyncAction.ts +import { useState, useCallback } from "react"; + +export function useAsyncAction Promise>(asyncFunction: T) { + const [loading, setLoading] = useState(false); + + const execute = useCallback( + async (...args: Parameters): Promise | void> => { + try { + setLoading(true); + return await asyncFunction(...args); + } catch (error) { + console.error("Erro na ação assíncrona:", error); + } finally { + setLoading(false); + } + }, + [asyncFunction] + ); + + return { execute, loading }; +}