2024-11-07 12:05:27 +05:30
|
|
|
// VotingPage.js
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { Bar } from "react-chartjs-2";
|
|
|
|
|
import { Chart as ChartJS, BarElement, CategoryScale, LinearScale } from "chart.js";
|
2024-11-10 16:16:28 +05:30
|
|
|
import { useQuery } from "react-query";
|
|
|
|
|
import { useParams } from "react-router-dom";
|
|
|
|
|
import getPollData from "../services/getPollData";
|
2024-11-12 19:47:58 +05:30
|
|
|
import ErrorFallback from "../components/Errors/ErrorFallback";
|
2024-11-07 12:05:27 +05:30
|
|
|
|
|
|
|
|
ChartJS.register(BarElement, CategoryScale, LinearScale);
|
|
|
|
|
|
|
|
|
|
function VotingPage() {
|
2024-11-10 16:16:28 +05:30
|
|
|
|
|
|
|
|
const { pollId } = useParams();
|
|
|
|
|
|
2024-11-12 19:47:58 +05:30
|
|
|
const { data: poll, isLoading, isError, refetch } = useQuery(["poll", pollId], () => getPollData(pollId), {
|
2024-11-10 16:16:28 +05:30
|
|
|
cacheTime : 10*100*60, // 10 minutes
|
|
|
|
|
staleTime : 20*100*60, // 20 minutes
|
|
|
|
|
});
|
2024-11-07 12:05:27 +05:30
|
|
|
|
|
|
|
|
// Dummy chart data for visualization
|
|
|
|
|
const chartData = {
|
2024-11-12 19:47:58 +05:30
|
|
|
labels: poll?.data?.pollData?.options.map(option => option.name),
|
2024-11-07 12:05:27 +05:30
|
|
|
datasets: [
|
|
|
|
|
{
|
|
|
|
|
label: "Votes",
|
2024-11-12 19:47:58 +05:30
|
|
|
data: poll?.data?.pollData?.options.map(option => option.voteCount),
|
2024-11-07 12:05:27 +05:30
|
|
|
backgroundColor: ["#3B82F6", "#EF4444", "#10B981", "#F59E0B"],
|
|
|
|
|
borderWidth: 1,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-12 19:47:58 +05:30
|
|
|
if (isLoading) {
|
|
|
|
|
return <div className="skeleton h-64 w-full max-w-lg mt-12 mx-auto"></div>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isError){
|
|
|
|
|
return <div className="h-64 w-full max-w-lg mt-12 mx-auto">
|
|
|
|
|
<ErrorFallback onRetry={refetch}/>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 12:05:27 +05:30
|
|
|
return (
|
|
|
|
|
<div className="bg-base-200 min-h-screen p-6 text-white flex flex-col items-center">
|
|
|
|
|
{/* Poll Creator Info */}
|
|
|
|
|
<div className="flex items-center justify-center gap-4 mb-6">
|
|
|
|
|
<img
|
2024-11-12 19:47:58 +05:30
|
|
|
src="https://via.placeholder.com/100"
|
|
|
|
|
alt={poll?.data?.creatorData?.username}
|
2024-11-07 12:05:27 +05:30
|
|
|
className="rounded-full h-7 md:h-10 w-7 md:w-10"
|
|
|
|
|
/>
|
2024-11-12 19:47:58 +05:30
|
|
|
<h2 className="text-lg md:text-xl font-semibold">{poll?.data?.creatorData?.username || "Unknown"}</h2>
|
2024-11-07 12:05:27 +05:30
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Poll Title */}
|
2024-11-12 19:47:58 +05:30
|
|
|
<h1 className="text-xl md:text-3xl font-bold mb-4 text-center">{poll?.data?.pollData?.title || "Loading.."}</h1>
|
2024-11-07 12:05:27 +05:30
|
|
|
|
|
|
|
|
{/* Voting Options */}
|
|
|
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 w-full max-w-lg mb-6">
|
2024-11-12 19:47:58 +05:30
|
|
|
{poll?.data?.pollData?.options.map(option => (
|
2024-11-07 12:05:27 +05:30
|
|
|
<div
|
2024-11-12 19:47:58 +05:30
|
|
|
key={option._id}
|
2024-11-07 12:05:27 +05:30
|
|
|
className="md:p-4 p-2 bg-base-100 rounded-lg shadow-md flex items-center justify-center cursor-pointer hover:bg-base-300 transition"
|
|
|
|
|
>
|
2024-11-12 19:47:58 +05:30
|
|
|
<span className="text-lg">{option.name}</span>
|
2024-11-07 12:05:27 +05:30
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Chart Visualization */}
|
|
|
|
|
<div className="w-full max-w-lg">
|
|
|
|
|
<Bar data={chartData} options={{ responsive: true, maintainAspectRatio: false }} />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default VotingPage;
|