import React, { useState } from "react"; import { Bar } from "react-chartjs-2"; import { Chart as ChartJS, BarElement, CategoryScale, LinearScale, } from "chart.js"; import { useMutation, useQuery } from "react-query"; import { useParams } from "react-router-dom"; import getPollData from "../services/getPollData"; import ErrorFallback from "../components/Errors/ErrorFallback"; import createVoteService from "../services/createVoteService"; import { FaBookmark } from "react-icons/fa"; import { toast } from "react-toastify"; import { makeChartDataObjFromPollData } from "../utils/util"; import useBookmark from "../hooks/useBookmark"; ChartJS.register(BarElement, CategoryScale, LinearScale); function VotingPage() { const { pollId } = useParams(); const [seletedOption, setSeletedOption] = useState(null); const {handleBookmark} = useBookmark() const { data: poll, isLoading, isError, refetch, } = useQuery(["poll", pollId], () => getPollData(pollId), { cacheTime: 10 * 100 * 60, // 10 minutes staleTime: 20 * 100 * 60, // 20 minutes }); const mutation = useMutation(createVoteService, { onSuccess: (data) => { toast.success("Vote given successfully"); }, onError: (error) => { console.log(error); toast.error( error?.response?.data?.message || "An unexpected error occurred" ); }, }); const handleOptionSelect = (id) => { mutation.mutate({ pollId, optionId: id }); }; if (isLoading) { return
; } if (isError) { return ({poll?.data?.pollData?.description || "Loading.."}
{/* Voting Options */}