From 74f0d422aad0b198a6dfacfad90566487561b38d Mon Sep 17 00:00:00 2001 From: Manik Maity Date: Tue, 12 Nov 2024 21:22:05 +0530 Subject: [PATCH] Added vote in frontend --- frontend/src/pages/VotingPage.jsx | 38 +++++++++++++++++++--- frontend/src/services/createVoteService.js | 8 +++++ frontend/src/utils/util.js | 8 +++++ 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 frontend/src/services/createVoteService.js create mode 100644 frontend/src/utils/util.js diff --git a/frontend/src/pages/VotingPage.jsx b/frontend/src/pages/VotingPage.jsx index cfe3363..8f795dd 100644 --- a/frontend/src/pages/VotingPage.jsx +++ b/frontend/src/pages/VotingPage.jsx @@ -1,24 +1,42 @@ // VotingPage.js -import React from "react"; +import React, { useState } from "react"; import { Bar } from "react-chartjs-2"; import { Chart as ChartJS, BarElement, CategoryScale, LinearScale } from "chart.js"; -import { useQuery } from "react-query"; +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"; ChartJS.register(BarElement, CategoryScale, LinearScale); function VotingPage() { const { pollId } = useParams(); + const [seletedOption, setSeletedOption] = useState(null); const { data: poll, isLoading, isError, refetch } = useQuery(["poll", pollId], () => getPollData(pollId), { cacheTime : 10*100*60, // 10 minutes staleTime : 20*100*60, // 20 minutes }); - // Dummy chart data for visualization + 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 }); + } + + const chartData = { labels: poll?.data?.pollData?.options.map(option => option.name), datasets: [ @@ -43,6 +61,7 @@ function VotingPage() { return (
+
{/* Poll Creator Info */}
{poll?.data?.creatorData?.username || "Unknown"}
+ {/* BookMark Button */} + + +
+ {/* Poll Title */} -

{poll?.data?.pollData?.title || "Loading.."}

+

{poll?.data?.pollData?.title || "Loading.."}

+ + {/* Poll Description */} +

{poll?.data?.pollData?.description || "Loading.."}

{/* Voting Options */}
{poll?.data?.pollData?.options.map(option => (
handleOptionSelect(option._id)} key={option._id} - 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" + className= {`md:p-4 p-2 ${seletedOption == option._id ? "bg-blue-500" : "bg-base-100"} rounded-lg shadow-md flex items-center justify-center cursor-pointer ${seletedOption == option._id ? "outline" :"hover:bg-base-300"} transition`} > {option.name}
diff --git a/frontend/src/services/createVoteService.js b/frontend/src/services/createVoteService.js new file mode 100644 index 0000000..985c80b --- /dev/null +++ b/frontend/src/services/createVoteService.js @@ -0,0 +1,8 @@ +import axiosInstance from "../helper/axiosInstance"; + +async function createVoteService(data) { + const response = await axiosInstance.post("/poll/vote", data); + return response.data; +} + +export default createVoteService diff --git a/frontend/src/utils/util.js b/frontend/src/utils/util.js new file mode 100644 index 0000000..d6acb8b --- /dev/null +++ b/frontend/src/utils/util.js @@ -0,0 +1,8 @@ +export const saveSelectedOption = (pollId, seletedOptionId) => { + localStorage.setItem(pollId, seletedOptionId); +} + +export const getSelectedOption = (pollId) => { + const selecetedId = localStorage.getItem(pollId); + return selecetedId; +} \ No newline at end of file