sforkowany z mirror/soapbox
Convert Polls to TSX
rodzic
4322712bfb
commit
0cdf898b37
|
@ -1,45 +1,49 @@
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import PropTypes from 'prop-types';
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
||||||
import ImmutablePureComponent from 'react-immutable-pure-component';
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
||||||
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
|
import { defineMessages, injectIntl, FormattedMessage, IntlShape } from 'react-intl';
|
||||||
import spring from 'react-motion/lib/spring';
|
import { spring } from 'react-motion';
|
||||||
|
|
||||||
import { openModal } from 'soapbox/actions/modals';
|
import { openModal } from 'soapbox/actions/modals';
|
||||||
import { vote, fetchPoll } from 'soapbox/actions/polls';
|
import { vote, fetchPoll } from 'soapbox/actions/polls';
|
||||||
import Icon from 'soapbox/components/icon';
|
import Icon from 'soapbox/components/icon';
|
||||||
import { Text } from 'soapbox/components/ui';
|
import { Text } from 'soapbox/components/ui';
|
||||||
import Motion from 'soapbox/features/ui/util/optional_motion';
|
import Motion from 'soapbox/features/ui/util/optional_motion';
|
||||||
import SoapboxPropTypes from 'soapbox/utils/soapbox_prop_types';
|
|
||||||
|
|
||||||
import RelativeTimestamp from './relative_timestamp';
|
import RelativeTimestamp from './relative_timestamp';
|
||||||
|
|
||||||
|
import type { Poll as PollEntity, PollOption } from 'soapbox/types/entities';
|
||||||
|
|
||||||
const messages = defineMessages({
|
const messages = defineMessages({
|
||||||
closed: { id: 'poll.closed', defaultMessage: 'Closed' },
|
closed: { id: 'poll.closed', defaultMessage: 'Closed' },
|
||||||
voted: { id: 'poll.voted', defaultMessage: 'You voted for this answer' },
|
voted: { id: 'poll.voted', defaultMessage: 'You voted for this answer' },
|
||||||
votes: { id: 'poll.votes', defaultMessage: '{votes, plural, one {# vote} other {# votes}}' },
|
votes: { id: 'poll.votes', defaultMessage: '{votes, plural, one {# vote} other {# votes}}' },
|
||||||
});
|
});
|
||||||
|
|
||||||
export default @injectIntl
|
interface IPoll {
|
||||||
class Poll extends ImmutablePureComponent {
|
poll?: PollEntity,
|
||||||
|
intl: IntlShape,
|
||||||
|
dispatch?: Function,
|
||||||
|
disabled?: boolean,
|
||||||
|
me?: string | null | false | undefined,
|
||||||
|
status?: string,
|
||||||
|
}
|
||||||
|
|
||||||
static propTypes = {
|
interface IPollState {
|
||||||
poll: ImmutablePropTypes.map,
|
selected: Record<string, boolean>,
|
||||||
intl: PropTypes.object.isRequired,
|
}
|
||||||
dispatch: PropTypes.func,
|
|
||||||
disabled: PropTypes.bool,
|
class Poll extends ImmutablePureComponent<IPoll, IPollState> {
|
||||||
me: SoapboxPropTypes.me,
|
|
||||||
status: PropTypes.string,
|
|
||||||
};
|
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
selected: {},
|
selected: {} as Record<string, boolean>,
|
||||||
};
|
};
|
||||||
|
|
||||||
_toggleOption = value => {
|
_toggleOption = (value: string) => {
|
||||||
if (this.props.me) {
|
const { me, poll } = this.props;
|
||||||
if (this.props.poll.get('multiple')) {
|
|
||||||
|
if (me) {
|
||||||
|
if (poll?.multiple) {
|
||||||
const tmp = { ...this.state.selected };
|
const tmp = { ...this.state.selected };
|
||||||
if (tmp[value]) {
|
if (tmp[value]) {
|
||||||
delete tmp[value];
|
delete tmp[value];
|
||||||
|
@ -48,7 +52,7 @@ class Poll extends ImmutablePureComponent {
|
||||||
}
|
}
|
||||||
this.setState({ selected: tmp });
|
this.setState({ selected: tmp });
|
||||||
} else {
|
} else {
|
||||||
const tmp = {};
|
const tmp: Record<string, boolean> = {};
|
||||||
tmp[value] = true;
|
tmp[value] = true;
|
||||||
this.setState({ selected: tmp });
|
this.setState({ selected: tmp });
|
||||||
}
|
}
|
||||||
|
@ -57,28 +61,32 @@ class Poll extends ImmutablePureComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleOptionChange = ({ target: { value } }) => {
|
handleOptionChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
|
||||||
this._toggleOption(value);
|
this._toggleOption(e.currentTarget.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
handleOptionKeyPress = (e) => {
|
handleOptionKeyPress = (e: React.KeyboardEvent): void => {
|
||||||
if (e.key === 'Enter' || e.key === ' ') {
|
if (e.key === 'Enter' || e.key === ' ') {
|
||||||
this._toggleOption(e.target.getAttribute('data-index'));
|
const dataIndex = e.currentTarget.getAttribute('data-index');
|
||||||
|
|
||||||
|
if (dataIndex) {
|
||||||
|
this._toggleOption(dataIndex);
|
||||||
|
}
|
||||||
|
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleVote = () => {
|
handleVote = () => {
|
||||||
if (this.props.disabled) {
|
const { disabled, dispatch, poll } = this.props;
|
||||||
return;
|
if (disabled || !dispatch || !poll) return;
|
||||||
}
|
dispatch(vote(poll.id, Object.keys(this.state.selected)));
|
||||||
|
|
||||||
this.props.dispatch(vote(this.props.poll.get('id'), Object.keys(this.state.selected)));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
openUnauthorizedModal = () => {
|
openUnauthorizedModal = () => {
|
||||||
const { dispatch, status } = this.props;
|
const { dispatch, status } = this.props;
|
||||||
|
if (!dispatch) return;
|
||||||
dispatch(openModal('UNAUTHORIZED', {
|
dispatch(openModal('UNAUTHORIZED', {
|
||||||
action: 'POLL_VOTE',
|
action: 'POLL_VOTE',
|
||||||
ap_id: status,
|
ap_id: status,
|
||||||
|
@ -86,21 +94,20 @@ class Poll extends ImmutablePureComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
handleRefresh = () => {
|
handleRefresh = () => {
|
||||||
if (this.props.disabled) {
|
const { disabled, dispatch, poll } = this.props;
|
||||||
return;
|
if (disabled || !poll || !dispatch) return;
|
||||||
}
|
dispatch(fetchPoll(poll.id));
|
||||||
|
|
||||||
this.props.dispatch(fetchPoll(this.props.poll.get('id')));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
renderOption(option, optionIndex, showResults) {
|
renderOption(option: PollOption, optionIndex: number, showResults: boolean): JSX.Element | null {
|
||||||
const { poll, disabled, intl } = this.props;
|
const { poll, disabled, intl } = this.props;
|
||||||
|
if (!poll) return null;
|
||||||
|
|
||||||
const percent = poll.get('votes_count') === 0 ? 0 : (option.get('votes_count') / poll.get('votes_count')) * 100;
|
const percent = poll.votes_count === 0 ? 0 : (option.votes_count / poll.votes_count) * 100;
|
||||||
const leading = poll.get('options').filterNot(other => other.get('title') === option.get('title')).every(other => option.get('votes_count') >= other.get('votes_count'));
|
const leading = poll.options.filterNot(other => other.title === option.title).every(other => option.votes_count >= other.votes_count);
|
||||||
const active = !!this.state.selected[`${optionIndex}`];
|
const active = !!this.state.selected[`${optionIndex}`];
|
||||||
const voted = poll.own_votes?.includes(optionIndex);
|
const voted = poll.own_votes?.includes(optionIndex);
|
||||||
const titleEmojified = option.get('title_emojified');
|
const titleEmojified = option.title_emojified;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li key={option.get('title')}>
|
<li key={option.get('title')}>
|
||||||
|
@ -115,7 +122,7 @@ class Poll extends ImmutablePureComponent {
|
||||||
<label className={classNames('poll__text', { selectable: !showResults })}>
|
<label className={classNames('poll__text', { selectable: !showResults })}>
|
||||||
<input
|
<input
|
||||||
name='vote-options'
|
name='vote-options'
|
||||||
type={poll.get('multiple') ? 'checkbox' : 'radio'}
|
type={poll.multiple ? 'checkbox' : 'radio'}
|
||||||
value={optionIndex}
|
value={optionIndex}
|
||||||
checked={active}
|
checked={active}
|
||||||
onChange={this.handleOptionChange}
|
onChange={this.handleOptionChange}
|
||||||
|
@ -124,19 +131,21 @@ class Poll extends ImmutablePureComponent {
|
||||||
|
|
||||||
{!showResults && (
|
{!showResults && (
|
||||||
<span
|
<span
|
||||||
className={classNames('poll__input', { checkbox: poll.get('multiple'), active })}
|
className={classNames('poll__input', { checkbox: poll.multiple, active })}
|
||||||
tabIndex='0'
|
tabIndex={0}
|
||||||
role={poll.get('multiple') ? 'checkbox' : 'radio'}
|
role={poll.multiple ? 'checkbox' : 'radio'}
|
||||||
onKeyPress={this.handleOptionKeyPress}
|
onKeyPress={this.handleOptionKeyPress}
|
||||||
aria-checked={active}
|
aria-checked={active}
|
||||||
aria-label={option.get('title')}
|
aria-label={option.title}
|
||||||
data-index={optionIndex}
|
data-index={optionIndex}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{showResults && <span className='poll__number' title={intl.formatMessage(messages.votes, { votes: option.get('votes_count') })}>
|
{showResults && (
|
||||||
{!!voted && <Icon src={require('@tabler/icons/icons/check.svg')} className='poll__vote__mark' title={intl.formatMessage(messages.voted)} />}
|
<span className='poll__number' title={intl.formatMessage(messages.votes, { votes: option.votes_count })}>
|
||||||
{Math.round(percent)}%
|
{!!voted && <Icon src={require('@tabler/icons/icons/check.svg')} className='poll__vote__mark' title={intl.formatMessage(messages.voted)} />}
|
||||||
</span>}
|
{Math.round(percent)}%
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
|
||||||
<span dangerouslySetInnerHTML={{ __html: titleEmojified }} />
|
<span dangerouslySetInnerHTML={{ __html: titleEmojified }} />
|
||||||
</label>
|
</label>
|
||||||
|
@ -151,15 +160,14 @@ class Poll extends ImmutablePureComponent {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const timeRemaining = poll.get('expired') ? intl.formatMessage(messages.closed) : <RelativeTimestamp timestamp={poll.get('expires_at')} futureDate />;
|
const timeRemaining = poll.expired ? intl.formatMessage(messages.closed) : <RelativeTimestamp timestamp={poll.expires_at} futureDate />;
|
||||||
const showResults = poll.get('voted') || poll.get('expired');
|
const showResults = poll.voted || poll.expired;
|
||||||
const disabled = this.props.disabled || Object.entries(this.state.selected).every(item => !item);
|
const disabled = this.props.disabled || Object.entries(this.state.selected).every(item => !item);
|
||||||
const voted = poll.voted;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classNames('poll', { voted })}>
|
<div className={classNames('poll', { voted: poll.voted })}>
|
||||||
<ul>
|
<ul>
|
||||||
{poll.get('options').map((option, i) => this.renderOption(option, i, showResults))}
|
{poll.options.map((option, i) => this.renderOption(option, i, showResults))}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<div className='poll__footer'>
|
<div className='poll__footer'>
|
||||||
|
@ -170,8 +178,8 @@ class Poll extends ImmutablePureComponent {
|
||||||
<Text><FormattedMessage id='poll.refresh' defaultMessage='Refresh' /></Text>
|
<Text><FormattedMessage id='poll.refresh' defaultMessage='Refresh' /></Text>
|
||||||
</button> · </span>
|
</button> · </span>
|
||||||
)}
|
)}
|
||||||
<FormattedMessage id='poll.total_votes' defaultMessage='{count, plural, one {# vote} other {# votes}}' values={{ count: poll.get('votes_count') }} />
|
<FormattedMessage id='poll.total_votes' defaultMessage='{count, plural, one {# vote} other {# votes}}' values={{ count: poll.votes_count }} />
|
||||||
{poll.get('expires_at') && <span> · {timeRemaining}</span>}
|
{poll.expires_at && <span> · {timeRemaining}</span>}
|
||||||
</Text>
|
</Text>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -179,3 +187,5 @@ class Poll extends ImmutablePureComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default injectIntl(Poll);
|
|
@ -1,11 +0,0 @@
|
||||||
import { connect } from 'react-redux';
|
|
||||||
|
|
||||||
import Poll from 'soapbox/components/poll';
|
|
||||||
|
|
||||||
const mapStateToProps = (state, { pollId }) => ({
|
|
||||||
poll: state.getIn(['polls', pollId]),
|
|
||||||
me: state.get('me'),
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
export default connect(mapStateToProps)(Poll);
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
import { connect } from 'react-redux';
|
||||||
|
|
||||||
|
import Poll from 'soapbox/components/poll';
|
||||||
|
|
||||||
|
import type { RootState } from 'soapbox/store';
|
||||||
|
|
||||||
|
interface IPollContainer {
|
||||||
|
pollId: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
const mapStateToProps = (state: RootState, { pollId }: IPollContainer) => ({
|
||||||
|
poll: state.polls.get(pollId),
|
||||||
|
me: state.me,
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
export default connect(mapStateToProps)(Poll);
|
|
@ -72,6 +72,7 @@
|
||||||
"@types/lodash": "^4.14.180",
|
"@types/lodash": "^4.14.180",
|
||||||
"@types/qrcode.react": "^1.0.2",
|
"@types/qrcode.react": "^1.0.2",
|
||||||
"@types/react-helmet": "^6.1.5",
|
"@types/react-helmet": "^6.1.5",
|
||||||
|
"@types/react-motion": "^0.0.32",
|
||||||
"@types/react-router-dom": "^5.3.3",
|
"@types/react-router-dom": "^5.3.3",
|
||||||
"@types/react-toggle": "^4.0.3",
|
"@types/react-toggle": "^4.0.3",
|
||||||
"@types/semver": "^7.3.9",
|
"@types/semver": "^7.3.9",
|
||||||
|
|
|
@ -2109,6 +2109,13 @@
|
||||||
dependencies:
|
dependencies:
|
||||||
"@types/react" "*"
|
"@types/react" "*"
|
||||||
|
|
||||||
|
"@types/react-motion@^0.0.32":
|
||||||
|
version "0.0.32"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/react-motion/-/react-motion-0.0.32.tgz#c7355cca054664f1aeadd7388f6890e9355e1783"
|
||||||
|
integrity sha512-xePjDdhy6/6AX3CUQCeQ2GSF0RwF+lXSpUSrm8tmdUXRf5Ps/dULwouTJ8YHhDvX7WlwYRKZjHXatadz/x3HXA==
|
||||||
|
dependencies:
|
||||||
|
"@types/react" "*"
|
||||||
|
|
||||||
"@types/react-redux@^7.1.16":
|
"@types/react-redux@^7.1.16":
|
||||||
version "7.1.18"
|
version "7.1.18"
|
||||||
resolved "https://registry.yarnpkg.com/@types/react-redux/-/react-redux-7.1.18.tgz#2bf8fd56ebaae679a90ebffe48ff73717c438e04"
|
resolved "https://registry.yarnpkg.com/@types/react-redux/-/react-redux-7.1.18.tgz#2bf8fd56ebaae679a90ebffe48ff73717c438e04"
|
||||||
|
|
Ładowanie…
Reference in New Issue