import dayjs from 'dayjs' import { Progress, Button, Radio, Label, Checkbox } from 'flowbite-react' import { Entity, MegalodonInterface } from 'megalodon' import { HTMLAttributes } from 'react' import { FormattedMessage } from 'react-intl' type Props = { poll: Entity.Poll client: MegalodonInterface onRefresh: () => void } & HTMLAttributes export default function Poll(props: Props) { if (props.poll.voted || props.poll.expired) { return } else if (props.poll.multiple) { return } else { return } } function SimplePoll(props: Props) { const vote = async () => { const elements = document.getElementsByName(props.poll.id) let checked: number | null = null elements.forEach((element, index) => { if ((element as HTMLInputElement).checked) { checked = index } }) if (checked !== null) { await props.client.votePoll(props.poll.id, [checked]) props.onRefresh() } } return (
{props.poll.options.map((option, index) => (
))}
) } function MultiplePoll(props: Props) { const vote = async () => { let checked: Array = [] props.poll.options.forEach((value, index) => { const element = document.getElementById(value.title) as HTMLInputElement if (element.checked) { checked = [...checked, index] } }) if (checked.length > 0) { await props.client.votePoll(props.poll.id, checked) props.onRefresh() } } return (
{props.poll.options.map((option, index) => (
))}
) } function PollResult(props: Props) { return (
{props.poll.options.map((option, index) => (
{percent(option.votes_count ?? 0, props.poll.votes_count)}% {option.title}
))}
{props.poll.expired ? (
) : (
)}
) } const percent = (votes: number, all: number) => { if (all > 0) { return Math.round((votes * 100) / all) } else { return 0 } }