From 79670b4c51ebbdd242b894a5f0678618054cc2ef Mon Sep 17 00:00:00 2001 From: zwlucas Date: Mon, 31 Mar 2025 23:34:04 -0300 Subject: create eletrocast-eleicoes --- app/obrigado/page.tsx | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 app/obrigado/page.tsx (limited to 'app/obrigado/page.tsx') diff --git a/app/obrigado/page.tsx b/app/obrigado/page.tsx new file mode 100644 index 0000000..718e122 --- /dev/null +++ b/app/obrigado/page.tsx @@ -0,0 +1,169 @@ +"use client"; + +import { useEffect, useRef, useState } from "react"; +import { useSearchParams } from "next/navigation"; +import { + Card, + CardContent, + CardDescription, + CardHeader, + CardTitle, +} from "@/components/ui/card"; +import { CheckCircle2, AlertTriangle } from "lucide-react"; +import { getSupabaseClient } from "@/lib/supabase"; + +export default function ObrigadoPage() { + const searchParams = useSearchParams(); + const rm = searchParams.get("rm") || ""; + const name = searchParams.get("nome") || ""; + const cpf = searchParams.get("cpf") || ""; + const option = searchParams.get("option") || ""; + + const [countdown, setCountdown] = useState(5); + const [saveStatus, setSaveStatus] = useState<"loading" | "success" | "error">( + "loading" + ); + const [errorMessage, setErrorMessage] = useState(""); + + const hasRun = useRef(false); + + useEffect(() => { + if (!rm || !name || !cpf || !option) { + window.location.href = "/"; + return; + } + + const saveVote = async () => { + try { + const supabase = getSupabaseClient(); + + const { error } = await supabase.from("votes").insert([ + { + rm, + name, + cpf: cpf.replace(/\D/g, ""), + option_voted: option, + }, + ]); + + if (error) { + console.error("Erro ao salvar voto:", error); + setSaveStatus("error"); + + if (error.code === "23505") { + setErrorMessage( + "Você já votou anteriormente. Cada eleitor pode votar apenas uma vez." + ); + } else { + setErrorMessage( + "Ocorreu um erro ao registrar seu voto. Por favor, informe ao responsável." + ); + } + return; + } + + setSaveStatus("success"); + } catch (error) { + console.error("Erro ao salvar voto:", error); + setSaveStatus("error"); + setErrorMessage( + "Ocorreu um erro ao registrar seu voto. Por favor, informe ao responsável." + ); + } + }; + + if (!hasRun.current) { + hasRun.current = true; + saveVote(); + } + + const timer = setInterval(() => { + setCountdown((prev) => { + if (prev <= 1) { + clearInterval(timer); + window.location.href = "/"; + return 0; + } + return prev - 1; + }); + }, 1000); + + return () => clearInterval(timer); + }, [rm, name, cpf, option]); + + return ( +
+
+
+
+
+ JUSTIÇA ELEITORAL +
+
+
+
+ + + +
+ {saveStatus === "loading" ? ( +
+ ) : saveStatus === "success" ? ( + + ) : ( + + )} +
+ + {saveStatus === "loading" + ? "PROCESSANDO SEU VOTO..." + : saveStatus === "success" + ? "VOTO REGISTRADO COM SUCESSO!" + : "ERRO AO REGISTRAR VOTO"} + + + {saveStatus === "success" ? "FIM" : ""} + +
+ + {saveStatus === "loading" ? ( +

+ Aguarde enquanto registramos seu voto... +

+ ) : saveStatus === "success" ? ( + <> +

+ Você votou em:{" "} + {option} +

+

+ Obrigado por participar das eleições. +

+

+ Retornando à tela inicial em {countdown} segundos... +

+ + ) : ( + <> +

{errorMessage}

+

+ Retornando à tela inicial em {countdown} segundos... +

+ + )} +
+
+ +
+
+ © {new Date().getFullYear()} Justiça Eleitoral Estudantil +
+
+
+
+ ); +} -- cgit v1.2.3