import classNames from 'classnames';
import React, { useState } from 'react';
import { defineMessages, useIntl, FormattedMessage } from 'react-intl';
import { spring } from 'react-motion';
import { useDispatch } from 'react-redux';
import { openModal } from 'soapbox/actions/modals';
import { vote, fetchPoll } from 'soapbox/actions/polls';
import Icon from 'soapbox/components/icon';
import { Text, Button, Stack, HStack } from 'soapbox/components/ui';
import Motion from 'soapbox/features/ui/util/optional_motion';
import { useAppSelector } from 'soapbox/hooks';
import RelativeTimestamp from './relative_timestamp';
import type { Poll as PollEntity, PollOption as PollOptionEntity } from 'soapbox/types/entities';
const messages = defineMessages({
closed: { id: 'poll.closed', defaultMessage: 'Closed' },
voted: { id: 'poll.voted', defaultMessage: 'You voted for this answer' },
votes: { id: 'poll.votes', defaultMessage: '{votes, plural, one {# vote} other {# votes}}' },
});
const PollPercentageBar: React.FC<{percent: number, leading: boolean}> = ({ percent, leading }): JSX.Element => {
return (
{({ width }) =>(
)}
);
};
interface IPollOptionText extends IPollOption {
percent: number,
}
const PollOptionText: React.FC = ({ poll, option, index, active, percent, showResults, onToggle }) => {
const intl = useIntl();
const voted = poll.own_votes?.includes(index);
const message = intl.formatMessage(messages.votes, { votes: option.votes_count });
const handleOptionChange: React.EventHandler = () => {
onToggle(index);
};
const handleOptionKeyPress: React.EventHandler = e => {
if (e.key === 'Enter' || e.key === ' ') {
onToggle(index);
e.stopPropagation();
e.preventDefault();
}
};
return (
);
};
interface IPollOption {
poll: PollEntity,
option: PollOptionEntity,
index: number,
showResults?: boolean,
active: boolean,
onToggle: (value: number) => void,
}
const PollOption: React.FC = (props): JSX.Element | null => {
const { poll, option, showResults } = props;
if (!poll) return null;
const percent = poll.votes_count === 0 ? 0 : (option.votes_count / poll.votes_count) * 100;
const leading = poll.options.filterNot(other => other.title === option.title).every(other => option.votes_count >= other.votes_count);
return (
);
};
const RefreshButton: React.FC<{poll: PollEntity}> = ({ poll }): JSX.Element => {
const dispatch = useDispatch();
const handleRefresh: React.EventHandler = (e) => {
dispatch(fetchPoll(poll.id));
e.stopPropagation();
e.preventDefault();
};
return (
);
};
const VoteButton: React.FC<{poll: PollEntity, selected: Selected}> = ({ poll, selected }): JSX.Element => {
const dispatch = useDispatch();
const handleVote = () => dispatch(vote(poll.id, Object.keys(selected)));
return (
);
};
interface IPollFooter {
poll: PollEntity,
showResults: boolean,
selected: Selected,
}
const PollFooter: React.FC = ({ poll, showResults, selected }): JSX.Element => {
const intl = useIntl();
const timeRemaining = poll.expired ? intl.formatMessage(messages.closed) : ;
return (
{!showResults &&
}
{showResults && (
<> · >
)}
{poll.expires_at && · {timeRemaining}}
);
};
type Selected = Record;
interface IPoll {
id: string,
status?: string,
}
const Poll: React.FC = ({ id, status }): JSX.Element | null => {
const dispatch = useDispatch();
const me = useAppSelector((state) => state.me);
const poll = useAppSelector((state) => state.polls.get(id));
const [selected, setSelected] = useState({} as Selected);
const openUnauthorizedModal = () => {
dispatch(openModal('UNAUTHORIZED', {
action: 'POLL_VOTE',
ap_id: status,
}));
};
const toggleOption = (value: number) => {
if (me) {
if (poll?.multiple) {
const tmp = { ...selected };
if (tmp[value]) {
delete tmp[value];
} else {
tmp[value] = true;
}
setSelected(tmp);
} else {
const tmp: Selected = {};
tmp[value] = true;
setSelected(tmp);
}
} else {
openUnauthorizedModal();
}
};
if (!poll) return null;
const showResults = poll.voted || poll.expired;
return (
e.stopPropagation()}>
{poll.options.map((option, i) => (
))}
);
};
export default Poll;