import React, { useContext, useEffect, useState } from "react"; import { MyContext } from "../../App"; import { Card, CardContent, CardHeader, Typography, RadioGroup, Radio, FormControlLabel, Button, Box, ButtonBase, Divider, } from "@mui/material"; import { getNameInfo } from "../Group/Group"; import PollIcon from "@mui/icons-material/Poll"; import { getFee } from "../../background"; import RefreshIcon from "@mui/icons-material/Refresh"; import { Spacer } from "../../common/Spacer"; import OpenInNewIcon from "@mui/icons-material/OpenInNew"; import { CustomLoader } from "../../common/CustomLoader"; export const PollCard = ({ poll, setInfoSnack, setOpenSnack, refresh, openExternal, external, isLoadingParent, errorMsg, }) => { const [selectedOption, setSelectedOption] = useState(""); const [ownerName, setOwnerName] = useState(""); const [showResults, setShowResults] = useState(false); const [isOpen, setIsOpen] = useState(false); const { show, userInfo } = useContext(MyContext); const [isLoadingSubmit, setIsLoadingSubmit] = useState(false); const handleVote = async () => { const fee = await getFee("VOTE_ON_POLL"); await show({ message: `Do you accept this VOTE_ON_POLL transaction? POLLS are public!`, publishFee: fee.fee + " QORT", }); setIsLoadingSubmit(true); window .sendMessage( "voteOnPoll", { pollName: poll?.info?.pollName, optionIndex: +selectedOption, }, 60000 ) .then((response) => { setIsLoadingSubmit(false); if (response.error) { setInfoSnack({ type: "error", message: response?.error || "Unable to vote.", }); setOpenSnack(true); return; } else { setInfoSnack({ type: "success", message: "Successfully voted. Please wait a couple minutes for the network to propogate the changes.", }); setOpenSnack(true); } }) .catch((error) => { setIsLoadingSubmit(false); setInfoSnack({ type: "error", message: error?.message || "Unable to vote.", }); setOpenSnack(true); }); }; const getName = async (owner) => { try { const res = await getNameInfo(owner); if (res) { setOwnerName(res); } } catch (error) {} }; useEffect(() => { if (poll?.info?.owner) { getName(poll.info.owner); } }, [poll?.info?.owner]); return ( POLL embed {external && ( )} Created by {ownerName || poll?.info?.owner} {!isOpen && !errorMsg && ( <> )} {isLoadingParent && isOpen && ( {" "} {" "} )} {errorMsg && ( {" "} {errorMsg} {" "} )} Options setSelectedOption(e.target.value)} > {poll?.info?.pollOptions?.map((option, index) => ( } label={option?.optionName} sx={{ "& .MuiFormControlLabel-label": { fontSize: "14px", }, }} /> ))} {" "} {`${poll?.votes?.totalVotes} ${ poll?.votes?.totalVotes === 1 ? " vote" : " votes" }`} item?.voterPublicKey === userInfo?.publicKey ) ? "visible" : "hidden", }} > You've already voted. {isLoadingSubmit && ( Is processing transaction, please wait... )} { setShowResults((prev) => !prev); }} > {showResults ? "hide " : "show "} results {showResults && } ); }; const PollResults = ({ votes }) => { const maxVotes = Math.max( ...votes?.voteCounts?.map((option) => option.voteCount) ); const options = votes?.voteCounts; return ( {options .sort((a, b) => b.voteCount - a.voteCount) // Sort options by votes (highest first) .map((option, index) => ( {`${index + 1}. ${option.optionName}`} {option.voteCount} votes ))} ); };