import { useState, useEffect, useRef, useContext } from "react"; import classNames from "classnames"; const Answers = (props) => { const { poll, user, socket, id, pid } = props; const [winner, setWinner] = useState(0); const token = localStorage.getItem(id) const onVote = async (answer) => { const data = { user:user, answer:answer, pid:pid, token:token } socket.emit("vote", data); }; useEffect(() => { var res = poll.users.reduce(function (obj, v) { obj[v.vote] = (obj[v.vote] || 0) + 1; return obj; }, {}) const arr = Object.values(res); const max = Math.max(...arr) setWinner(max) }, [poll]) const countVotes = (index) => { let count = 0 poll.users.filter(function (item) { if (item.vote === index) { count = count + 1 } }) return (
{count}
) } const buttonCN = classNames(`w-10/12 text-center bg-white w`) return (
{poll.answers.map((answer, index) => (
{countVotes(index)}
{ poll.users.map((user, i) => { if (user.name != props.user && poll.anonymous) { return null } if (index === user.vote) { return (
{user.name === props.user && } {poll.anonymous ? 'Me' : user.name}
) } }) }
))}
); } export default Answers