From a74ad96c06d0998415a27d90864dcd8607d13500 Mon Sep 17 00:00:00 2001 From: zwlucas Date: Wed, 2 Apr 2025 00:16:18 -0300 Subject: new results page --- app/resultados/loading.tsx | 3 + app/resultados/page.tsx | 192 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 app/resultados/loading.tsx create mode 100644 app/resultados/page.tsx (limited to 'app/resultados') diff --git a/app/resultados/loading.tsx b/app/resultados/loading.tsx new file mode 100644 index 0000000..4349ac3 --- /dev/null +++ b/app/resultados/loading.tsx @@ -0,0 +1,3 @@ +export default function Loading() { + return null; +} diff --git a/app/resultados/page.tsx b/app/resultados/page.tsx new file mode 100644 index 0000000..fdd644d --- /dev/null +++ b/app/resultados/page.tsx @@ -0,0 +1,192 @@ +"use client"; + +import { + Card, + CardContent, + CardDescription, + CardHeader, + CardTitle, +} from "@/components/ui/card"; +import { getSupabaseClient } from "@/lib/supabase"; +import { Loader2 } from "lucide-react"; +import { useEffect, useState } from "react"; + +type voteResult = { + total: number; + options: { + [key: string]: { + votes: number; + percentage: number; + }; + }; +}; + +export default function Home() { + const [results, setResults] = useState(null); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + + useEffect(() => { + const fetchResults = async () => { + try { + setLoading(true); + const supabase = getSupabaseClient(); + + const { data, error } = await supabase.from("votes").select("option"); + + if (error) throw new Error(error.message); + + if (!data || data.length === 0) { + setResults({ total: 0, options: {} }); + return; + } + + const total = data.length; + const count: { [key: string]: number } = {}; + + data.forEach((vote) => { + const option = vote.option as string; + count[option] = (count[option] || 0) + 1; + }); + + const options: { + [key: string]: { votes: number; percentage: number }; + } = {}; + + Object.keys(count).forEach((option) => { + if (option !== "NULO") { + options[option] = { + votes: count[option], + percentage: Number.parseFloat( + ((count[option] / total) * 100).toFixed(2) + ), + }; + } + }); + + setResults({ total, options }); + } catch (error) { + console.error("Erro ao buscar resultados:", error); + setError("Ocorreu um erro ao carregar os resultados da votação."); + } finally { + setLoading(false); + } + }; + + fetchResults(); + }, []); + + const getBarColor = (option: string) => { + if (option === "SIE") return "bg-blue-500"; + if (option === "Liderança Jovem") return "bg-green-500"; + if (option === "NULO") return "bg-red-500"; + return "bg-purple-500"; + }; + + return ( +
+
+
+
+
+ JUSTIÇA ELEITORAL ESTUDANTIL +
+
+
+
+ + + + + RESULTADOS DA VOTAÇÃO + + + Estatísticas de participação e votos por candidato + + + + {loading ? ( +
+ +

+ Carregando resultados... +

+
+ ) : error ? ( +
+ {error} +
+ ) : ( +
+
+

+ Total de Votos +

+
+ + {results?.total || 0} + +

+ eleitores participaram da votação +

+
+
+ +
+

+ Distribuição dos Votos +

+ + {results && results.total > 0 ? ( +
+ {Object.keys(results.options).map((opcao) => ( +
+
+ + {opcao} + + + {results.options[opcao].votes} votos ( + {results.options[opcao].percentage}%) + +
+
+
+
+
+ ))} +
+ ) : ( +

+ Nenhum voto registrado até o momento. +

+ )} +
+ +
+

+ Os resultados são atualizados automaticamente a cada vez que + a página é carregada. +

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