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/page.tsx | 317 +++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 221 insertions(+), 96 deletions(-) (limited to 'app/page.tsx') diff --git a/app/page.tsx b/app/page.tsx index 88f0cc9..56ac851 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,103 +1,228 @@ -import Image from "next/image"; +"use client"; + +import type React from "react"; + +import { useState } from "react"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { + Card, + CardContent, + CardDescription, + CardFooter, + CardHeader, + CardTitle, +} from "@/components/ui/card"; +import { useRouter } from "next/navigation"; export default function Home() { + const [rm, setRm] = useState(""); + const [nome, setNome] = useState(""); + const [cpf, setCpf] = useState(""); + const [errors, setErrors] = useState<{ + rm?: string; + nome?: string; + cpf?: string; + }>({}); + const router = useRouter(); + + function validateCPF(cpf: string): boolean { + cpf = cpf.replace(/\D/g, ""); + + if (cpf.length !== 11 || /^(\d)\1{10}$/.test(cpf)) return false; + + const calc = (factor: number) => + cpf + .split("") + .slice(0, factor - 1) + .reduce( + (sum, num, index) => sum + parseInt(num) * (factor - index), + 0 + ) % + 11 < + 2 + ? 0 + : 11 - + (cpf + .split("") + .slice(0, factor - 1) + .reduce( + (sum, num, index) => sum + parseInt(num) * (factor - index), + 0 + ) % + 11); + + return calc(10) === parseInt(cpf[9]) && calc(11) === parseInt(cpf[10]); + } + + const handleSubmit = (e: React.FormEvent) => { + e.preventDefault(); + + const newErrors: { rm?: string; nome?: string; cpf?: string } = {}; + + if (!/^\d{5}$/.test(rm)) { + newErrors.rm = "O RM deve conter exatamente 5 dígitos numéricos."; + } + + if (!nome || nome.trim().length < 3) { + newErrors.nome = "Por favor, insira seu nome completo."; + } + + if (!validateCPF(cpf)) { + newErrors.cpf = "CPF inválido. Insira um CPF válido com 11 dígitos."; + } + + if (Object.keys(newErrors).length > 0) { + setErrors(newErrors); + return; + } + + setErrors({}); + + router.push( + `/confirmar?rm=${rm}&nome=${encodeURIComponent(nome)}&cpf=${cpf}` + ); + }; + + const formatCPF = (value: string) => { + const cpfClean = value.replace(/\D/g, ""); + let formatted = cpfClean; + + if (cpfClean.length > 3) { + formatted = cpfClean.substring(0, 3) + "." + cpfClean.substring(3); + } + if (cpfClean.length > 6) { + formatted = formatted.substring(0, 7) + "." + cpfClean.substring(6, 9); + } + if (cpfClean.length > 9) { + formatted = formatted.substring(0, 11) + "-" + cpfClean.substring(9, 11); + } + + return formatted; + }; + + const handleCPFChange = (e: React.ChangeEvent) => { + const value = e.target.value; + const formatted = formatCPF(value); + setCpf(formatted); + + if (errors.cpf) { + setErrors((prev) => ({ ...prev, cpf: undefined })); + } + }; + return ( -
-
- Next.js logo -
    -
  1. - Get started by editing{" "} - - app/page.tsx - - . -
  2. -
  3. - Save and see your changes instantly. -
  4. -
- -
- - Vercel logomark - Deploy now - - - Read our docs - +
+
+
+
+
+ JUSTIÇA ELEITORAL +
+
+
+
+ + + + ELEIÇÕES ESTUDANTIS + + Identificação do Eleitor + + + +
+
+ + { + setRm(e.target.value); + if (errors.rm) { + setErrors((prev) => ({ ...prev, rm: undefined })); + } + }} + placeholder="Digite os 5 dígitos do seu RM" + className="border-2 border-[#004a93]" + maxLength={5} + /> + {errors.rm && ( +

{errors.rm}

+ )} +
+ +
+ + { + setNome(e.target.value); + if (errors.nome) { + setErrors((prev) => ({ ...prev, nome: undefined })); + } + }} + placeholder="Digite seu nome completo" + className="border-2 border-[#004a93]" + /> + {errors.nome && ( +

{errors.nome}

+ )} +
+ +
+ + + {errors.cpf && ( +

{errors.cpf}

+ )} +
+ + +
+
+ + Seu voto é secreto e seguro. + +
+ +
+
+ © {new Date().getFullYear()} Justiça Eleitoral Estudantil +
-
- +
); } -- cgit v1.2.3