2022-07-30 13:25:46 +00:00
|
|
|
import { gql, useMutation } from '@apollo/client'
|
2023-07-24 18:35:05 +00:00
|
|
|
import Button from 'react-bootstrap/Button'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { fixedDecimal, numWithUnits } from '@/lib/format'
|
|
|
|
import { timeLeft } from '@/lib/time'
|
2022-07-30 13:25:46 +00:00
|
|
|
import { useMe } from './me'
|
|
|
|
import styles from './poll.module.css'
|
2024-03-20 00:37:31 +00:00
|
|
|
import Check from '@/svgs/checkbox-circle-fill.svg'
|
2023-07-29 19:38:20 +00:00
|
|
|
import { signIn } from 'next-auth/react'
|
2022-07-30 13:25:46 +00:00
|
|
|
import ActionTooltip from './action-tooltip'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { POLL_COST } from '@/lib/constants'
|
2023-10-06 20:01:51 +00:00
|
|
|
import { payOrLoginError, useInvoiceModal } from './invoice'
|
2022-07-30 13:25:46 +00:00
|
|
|
|
|
|
|
export default function Poll ({ item }) {
|
|
|
|
const me = useMe()
|
|
|
|
const [pollVote] = useMutation(
|
|
|
|
gql`
|
2023-08-31 15:10:24 +00:00
|
|
|
mutation pollVote($id: ID!, $hash: String, $hmac: String) {
|
|
|
|
pollVote(id: $id, hash: $hash, hmac: $hmac)
|
2022-07-30 13:25:46 +00:00
|
|
|
}`, {
|
|
|
|
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 }) => {
|
2023-10-06 20:01:51 +00:00
|
|
|
const showInvoiceModal = useInvoiceModal(async ({ hash, hmac }, { variables }) => {
|
|
|
|
await pollVote({ variables: { ...variables, hash, hmac } })
|
|
|
|
}, [pollVote])
|
|
|
|
|
|
|
|
const variables = { id: v.id }
|
|
|
|
|
2022-07-30 13:25:46 +00:00
|
|
|
return (
|
2024-04-17 19:22:17 +00:00
|
|
|
<ActionTooltip placement='left' notForm overlayText='1 sat'>
|
2022-07-30 13:25:46 +00:00
|
|
|
<Button
|
|
|
|
variant='outline-info' className={styles.pollButton}
|
|
|
|
onClick={me
|
|
|
|
? async () => {
|
2023-07-25 14:14:45 +00:00
|
|
|
try {
|
|
|
|
await pollVote({
|
2023-10-06 20:01:51 +00:00
|
|
|
variables,
|
2023-07-25 14:14:45 +00:00
|
|
|
optimisticResponse: {
|
|
|
|
pollVote: v.id
|
2022-07-30 13:25:46 +00:00
|
|
|
}
|
2023-07-25 14:14:45 +00:00
|
|
|
})
|
|
|
|
} catch (error) {
|
2023-10-06 20:01:51 +00:00
|
|
|
if (payOrLoginError(error)) {
|
|
|
|
showInvoiceModal({ amount: item.pollCost || POLL_COST }, { variables })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
throw new Error({ message: error.toString() })
|
2022-07-30 13:25:46 +00:00
|
|
|
}
|
2023-07-25 14:14:45 +00:00
|
|
|
}
|
2022-07-30 13:25:46 +00:00
|
|
|
: signIn}
|
|
|
|
>
|
|
|
|
{v.option}
|
|
|
|
</Button>
|
|
|
|
</ActionTooltip>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-02-21 18:18:43 +00:00
|
|
|
const hasExpiration = !!item.pollExpiresAt
|
|
|
|
const timeRemaining = timeLeft(new Date(item.pollExpiresAt))
|
2022-07-30 13:25:46 +00:00
|
|
|
const mine = item.user.id === me?.id
|
2024-02-21 18:18:43 +00:00
|
|
|
const showPollButton = (!hasExpiration || timeRemaining) && !item.poll.meVoted && !mine
|
2022-07-30 13:25:46 +00:00
|
|
|
return (
|
|
|
|
<div className={styles.pollBox}>
|
|
|
|
{item.poll.options.map(v =>
|
2024-02-21 18:18:43 +00:00
|
|
|
showPollButton
|
2022-07-30 13:25:46 +00:00
|
|
|
? <PollButton key={v.id} v={v} />
|
|
|
|
: <PollResult
|
|
|
|
key={v.id} v={v}
|
|
|
|
progress={item.poll.count ? fixedDecimal(v.count * 100 / item.poll.count, 1) : 0}
|
|
|
|
/>)}
|
2024-02-21 18:18:43 +00:00
|
|
|
<div className='text-muted mt-1'>
|
|
|
|
{numWithUnits(item.poll.count, { unitSingular: 'vote', unitPlural: 'votes' })}
|
|
|
|
{hasExpiration && ` \\ ${timeRemaining ? `${timeRemaining} left` : 'poll ended'}`}
|
|
|
|
</div>
|
2022-07-30 13:25:46 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function PollResult ({ v, progress }) {
|
|
|
|
return (
|
|
|
|
<div className={styles.pollResult}>
|
2023-09-28 21:52:11 +00:00
|
|
|
<span className={styles.pollOption}>{v.option}{v.meVoted && <Check className='fill-grey ms-1 align-self-center flex-shrink-0' width={16} height={16} />}</span>
|
2023-07-24 18:35:05 +00:00
|
|
|
<span className='ms-auto me-2 align-self-center'>{progress}%</span>
|
2022-07-30 13:25:46 +00:00
|
|
|
<div className={styles.pollProgress} style={{ width: `${progress}%` }} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|