From 505136c38eff3c1fba51349d7cd8f30dd2a3dcda Mon Sep 17 00:00:00 2001 From: Kushal Baral <136962554+kushal1o1@users.noreply.github.com> Date: Tue, 2 Jul 2024 20:40:12 +0545 Subject: [PATCH] build:maintained poll percentage ,voted poll using localstorage and added styles --- BackEnd/db.sqlite3 | Bin 163840 -> 167936 bytes FrontEnd/polling-app/src/App.css | 118 +++++++++++++++---- FrontEnd/polling-app/src/components/Polls.js | 92 ++++++++++++--- 3 files changed, 169 insertions(+), 41 deletions(-) diff --git a/BackEnd/db.sqlite3 b/BackEnd/db.sqlite3 index 847f0b91cc7a7ad6739107164872256d782d58bb..2e7bc16304aceb9e3962fea44d983f57fb4d4e0f 100644 GIT binary patch delta 7203 zcmeHMZE##ydA@gd?|$ENRwDk2Ea|Q#S(as8opZnM%91S0lI+NGY$d^UsEOCo##$op zwY4j|E~RT=_?Q+ZBq`nUgO9d!UZNs{{D4}TBsa^;sFJM~Mw zYnG_dynHTLT&&)AA*hvFbJ=LmN_lP}D9@GlJ#u02yiBgVIJouH!(WR8U7HEH-%*P;zF^8tPa6) z3YM>1+;c4E7g7@4f-E1*VYi(j$Fy|Aw+)whK3IB7XvyK6uNeaOJl%3UZn>@pq?V9m zuW;IhA<>`CUEeX8;{u0<94j=_7E(CVxo303o^-$;jufv!uFC^?LEr`3)osRn(=OOx zBXrm@xiMd@qC3K8?i38BGnX^YJ!{QpyrVmxUNrd$k1Kz-GYSz-$Qfm!O}vhIhoupSWGuOLs(-?$4tLk zTfh#xMTb_w=DP2=mhTM@0;wGxGrc20O`BPs3z<4FnNhG{N?`!pn$m$8&X?(qovsFp z%eB(tC9Qa|R1MCCks;O`+HhAlI5RBI9e_6BQkl+?pgDgGrkUxk>wA{%?OAivK2Zt> z%!MIdFuBf6pV{niKMZ&n-EO^l;U!TqIb750+Y9$g{TbacaFp2PO`0<|rq^1Euw^QB7Y#;%aa zeVtjhWt!tVVUYbuw2vk($<7TC-t8(%2TJy5Dd z-gYu+z{C$QcYql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Y*7%^;Or=lU56%PJ&?YnwkLz4r9YCLd$~G*sis|m+ z^3b>HFb;6|i~8Qcb#(C+zQE@=;#{7GW@@+&oq=U|DTFcacvqmyjda9uEx!Se6mNj4 zh2`xz?@X7QRG!N2cCpF(W(P#2*^4EX^amc|7yOJnSfZq7gq~&`6OsTRhA7vdq!12B zNO(sI65bM7jUKU*Fez3Lc8lz5#CacKl$QzDO247CqmsumM-Nb@A=WAH5;iF!EgB$p zsk6O { const [polls, setPolls] = useState([]); + const [votedPolls, setVotedPolls] = useState({}); useEffect(() => { fetchPolls(); + loadVotedPollsFromStorage(); // Load voted polls from localStorage on component mount }, []); const fetchPolls = async () => { @@ -21,9 +21,10 @@ const Polls = () => { const handleVote = async (pollId, choiceId) => { try { - const response = await axios.post(`http://localhost:8000/polls/${pollId}/vote/`, { choice_id: choiceId }); - alert(response.data.status); - fetchPolls(); // Refresh the poll data after voting + await axios.post(`http://localhost:8000/polls/${pollId}/vote/`, { choice_id: choiceId }); + const updatedVotedPolls = { ...votedPolls, [pollId]: choiceId }; + setVotedPolls(updatedVotedPolls); + saveVotedPollsToStorage(updatedVotedPolls); // Save updated voted polls to localStorage } catch (error) { if (error.response && error.response.data.error) { alert(error.response.data.error); @@ -33,24 +34,77 @@ const Polls = () => { } }; + const calculatePercentage = (choiceVotes, totalVotes) => { + return totalVotes === 0 ? 0 : ((choiceVotes / totalVotes) * 100).toFixed(1); + }; + + const loadVotedPollsFromStorage = () => { + const storedVotedPolls = localStorage.getItem('votedPolls'); + if (storedVotedPolls) { + setVotedPolls(JSON.parse(storedVotedPolls)); + } + }; + + const saveVotedPollsToStorage = (updatedVotedPolls) => { + localStorage.setItem('votedPolls', JSON.stringify(updatedVotedPolls)); + }; + return ( -
-

Vote on Polls

- {polls.map((poll) => ( -
-

{poll.question}

-
    - {poll.choices.map((choice) => ( -
  • - {choice.choice_text} - {choice.votes} votes - -
  • - ))} -
+
+

Vote on Polls

+ {polls.length > 0 && ( +
+

Latest Poll

+
+
+

{polls[0].question}

+
+
    + {polls[0].choices.map((choice) => { + const totalVotes = polls[0].choices.reduce((acc, c) => acc + c.votes, 0); + const percentage = calculatePercentage(choice.votes, totalVotes); + const isVoted = votedPolls[polls[0].id] === choice.id; + return ( +
  • handleVote(polls[0].id, choice.id)}> + {choice.choice_text} +
    + {percentage}% +
    +
  • + ); + })} +
+
- ))} + )} +
+

Poll History

+ {polls.map((poll) => ( +
+
+

{poll.question}

+
+
    + {poll.choices.map((choice) => { + const totalVotes = poll.choices.reduce((acc, c) => acc + c.votes, 0); + const percentage = calculatePercentage(choice.votes, totalVotes); + const isVoted = votedPolls[poll.id] === choice.id; + return ( +
  • + {choice.choice_text} +
    + {percentage}% +
    +
  • + ); + })} +
+
+ ))} +
); }; export default Polls; +