import { gql, useMutation } from '@apollo/client' import { Button } from 'react-bootstrap' import { fixedDecimal } from '../lib/format' import { timeLeft } from '../lib/time' import { useMe } from './me' import styles from './poll.module.css' import Check from '../svgs/checkbox-circle-fill.svg' import { signIn } from 'next-auth/client' import { useFundError } from './fund-error' import ActionTooltip from './action-tooltip' export default function Poll ({ item }) { const me = useMe() const { setError } = useFundError() const [pollVote] = useMutation( gql` mutation pollVote($id: ID!) { pollVote(id: $id) }`, { update (cache, { data: { pollVote } }) { cache.modify({ id: `Item:${item.id}`, fields: { poll (existingPoll) { const poll = { ...existingPoll } poll.meVoted = true poll.count += 1 return poll } } }) cache.modify({ id: `PollOption:${pollVote}`, fields: { count (existingCount) { return existingCount + 1 }, meVoted () { return true } } }) } } ) const PollButton = ({ v }) => { return ( ) } const expiresIn = timeLeft(new Date(+new Date(item.createdAt) + 864e5)) const mine = item.user.id === me?.id return (
{item.poll.options.map(v => expiresIn && !item.poll.meVoted && !mine ? : )}
{item.poll.count} votes \ {expiresIn ? `${expiresIn} left` : 'poll ended'}
) } function PollResult ({ v, progress }) { return (
{v.option}{v.meVoted && } {progress}%
) }