From d9113a4b2f1cad763c70c1d7052bfeb93831e48b Mon Sep 17 00:00:00 2001 From: Damian Date: Tue, 24 Sep 2024 17:26:44 -0300 Subject: [PATCH] Show voted candidates and allow to change vote --- packages/nextjs/components/PollDetail.tsx | 84 ++++++++++++++------ packages/nextjs/components/card/VoteCard.tsx | 18 ++++- 2 files changed, 75 insertions(+), 27 deletions(-) diff --git a/packages/nextjs/components/PollDetail.tsx b/packages/nextjs/components/PollDetail.tsx index 7cce6fe..12ece4d 100644 --- a/packages/nextjs/components/PollDetail.tsx +++ b/packages/nextjs/components/PollDetail.tsx @@ -26,6 +26,8 @@ export default function PollDetail({ id }: { id: bigint }) { const isAnyInvalid = Object.values(isVotesInvalid).some(v => v); const [result, setResult] = useState<{ candidate: string; votes: number }[] | null>(null); const [status, setStatus] = useState(); + const [voted, setVoted] = useState(false); + const [voting, setVoting] = useState(false); useEffect(() => { if (!poll || !poll.metadata) { @@ -130,6 +132,8 @@ export default function PollDetail({ id }: { id: bigint }) { return; } + setVoting(true); + const votesToMessage = votes.map((v, i) => getMessageAndEncKeyPair( stateIndex, @@ -167,9 +171,12 @@ export default function PollDetail({ id }: { id: bigint }) { } notification.success("Vote casted successfully"); + setVoted(true); } catch (err) { console.log("err", err); notification.error("Casting vote failed, please try again "); + } finally { + setVoting(false); } }; @@ -224,32 +231,61 @@ export default function PollDetail({ id }: { id: bigint }) {
-
Vote for {poll?.name}
-
- {poll?.options.map((candidate, index) => ( -
- voteUpdated(index, checked, votes)} - isInvalid={Boolean(isVotesInvalid[index])} - setIsInvalid={status => setIsVotesInvalid({ ...isVotesInvalid, [index]: status })} - /> +
+ Vote for {poll?.name} + {status === PollStatus.CLOSED && " (Closed)"}
- ))} - {status === PollStatus.OPEN && ( -
- +
+ {voted ? ( +
+

Voted:

+
    + {votes.map(vote => ( +
  • + {poll?.options[vote.index]}: {vote.votes} votes +
  • + ))} +
+ {status === PollStatus.OPEN && ( +
+ +
+ )}
+ ) : ( + <> + {poll?.options.map((candidate, index) => ( +
+ v.index === index)?.votes} + pollType={pollType} + onChange={(checked, votes) => voteUpdated(index, checked, votes)} + isInvalid={Boolean(isVotesInvalid[index])} + setIsInvalid={status => setIsVotesInvalid({ ...isVotesInvalid, [index]: status })} + /> +
+ ))} + {status === PollStatus.OPEN && ( +
+ +
+ )} + )} {result && ( diff --git a/packages/nextjs/components/card/VoteCard.tsx b/packages/nextjs/components/card/VoteCard.tsx index 60b2e68..2a02d11 100644 --- a/packages/nextjs/components/card/VoteCard.tsx +++ b/packages/nextjs/components/card/VoteCard.tsx @@ -10,11 +10,21 @@ type VoteCardProps = { setIsInvalid: (value: boolean) => void; isInvalid: boolean; pollOpen: boolean; + currentVotes?: number; }; -const VoteCard = ({ index, candidate, onChange, pollType, isInvalid, setIsInvalid, pollOpen }: VoteCardProps) => { - const [selected, setSelected] = useState(false); - const [votes, setVotes] = useState(0); +const VoteCard = ({ + index, + candidate, + onChange, + pollType, + isInvalid, + setIsInvalid, + pollOpen, + currentVotes, +}: VoteCardProps) => { + const [selected, setSelected] = useState(currentVotes && currentVotes > 0 ? true : false); + const [votes, setVotes] = useState(currentVotes || 0); const votesFieldRef = useRef(null); return ( @@ -25,6 +35,7 @@ const VoteCard = ({ index, candidate, onChange, pollType, isInvalid, setIsInvali type={pollType === PollType.SINGLE_VOTE ? "radio" : "checkbox"} className="mr-2" value={index} + checked={selected} onChange={e => { console.log(e.target.checked); setSelected(e.target.checked); @@ -72,6 +83,7 @@ const VoteCard = ({ index, candidate, onChange, pollType, isInvalid, setIsInvali placeholder="Votes" min={0} step={1} + defaultValue={currentVotes || ""} onChange={function (e) { if ( Number(e.currentTarget.value) < 0 ||