sforkowany z mirror/soapbox
Poll: convert to React.FC, remove PollContainer
rodzic
cc1df1bd09
commit
1a1464f5ba
|
@ -1,7 +1,6 @@
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import React from 'react';
|
import React, { useState } from 'react';
|
||||||
import ImmutablePureComponent from 'react-immutable-pure-component';
|
import { defineMessages, useIntl, FormattedMessage } from 'react-intl';
|
||||||
import { defineMessages, injectIntl, useIntl, FormattedMessage, IntlShape } from 'react-intl';
|
|
||||||
import { spring } from 'react-motion';
|
import { spring } from 'react-motion';
|
||||||
import { useDispatch } from 'react-redux';
|
import { useDispatch } from 'react-redux';
|
||||||
|
|
||||||
|
@ -10,6 +9,7 @@ import { vote, fetchPoll } from 'soapbox/actions/polls';
|
||||||
import Icon from 'soapbox/components/icon';
|
import Icon from 'soapbox/components/icon';
|
||||||
import { Text, Button, Stack, HStack } from 'soapbox/components/ui';
|
import { Text, Button, Stack, HStack } from 'soapbox/components/ui';
|
||||||
import Motion from 'soapbox/features/ui/util/optional_motion';
|
import Motion from 'soapbox/features/ui/util/optional_motion';
|
||||||
|
import { useAppSelector } from 'soapbox/hooks';
|
||||||
|
|
||||||
import RelativeTimestamp from './relative_timestamp';
|
import RelativeTimestamp from './relative_timestamp';
|
||||||
|
|
||||||
|
@ -130,18 +130,6 @@ const PollOption: React.FC<IPollOption> = (props): JSX.Element | null => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
interface IPoll {
|
|
||||||
poll?: PollEntity,
|
|
||||||
intl: IntlShape,
|
|
||||||
dispatch?: Function,
|
|
||||||
me?: string | null | false | undefined,
|
|
||||||
status?: string,
|
|
||||||
}
|
|
||||||
|
|
||||||
interface IPollState {
|
|
||||||
selected: Selected,
|
|
||||||
}
|
|
||||||
|
|
||||||
const RefreshButton: React.FC<{poll: PollEntity}> = ({ poll }): JSX.Element => {
|
const RefreshButton: React.FC<{poll: PollEntity}> = ({ poll }): JSX.Element => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
|
@ -197,77 +185,75 @@ const PollFooter: React.FC<IPollFooter> = ({ poll, showResults, selected }): JSX
|
||||||
|
|
||||||
type Selected = Record<number, boolean>;
|
type Selected = Record<number, boolean>;
|
||||||
|
|
||||||
class Poll extends ImmutablePureComponent<IPoll, IPollState> {
|
interface IPoll {
|
||||||
|
id: string,
|
||||||
|
status?: string,
|
||||||
|
}
|
||||||
|
|
||||||
state = {
|
const Poll: React.FC<IPoll> = ({ id, status }): JSX.Element | null => {
|
||||||
selected: {} as Selected,
|
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,
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
toggleOption = (value: number) => {
|
const toggleOption = (value: number) => {
|
||||||
const { me, poll } = this.props;
|
|
||||||
|
|
||||||
if (me) {
|
if (me) {
|
||||||
if (poll?.multiple) {
|
if (poll?.multiple) {
|
||||||
const tmp = { ...this.state.selected };
|
const tmp = { ...selected };
|
||||||
if (tmp[value]) {
|
if (tmp[value]) {
|
||||||
delete tmp[value];
|
delete tmp[value];
|
||||||
} else {
|
} else {
|
||||||
tmp[value] = true;
|
tmp[value] = true;
|
||||||
}
|
}
|
||||||
this.setState({ selected: tmp });
|
setSelected(tmp);
|
||||||
} else {
|
} else {
|
||||||
const tmp: Selected = {};
|
const tmp: Selected = {};
|
||||||
tmp[value] = true;
|
tmp[value] = true;
|
||||||
this.setState({ selected: tmp });
|
setSelected(tmp);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.openUnauthorizedModal();
|
openUnauthorizedModal();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
openUnauthorizedModal = () => {
|
if (!poll) return null;
|
||||||
const { dispatch, status } = this.props;
|
|
||||||
if (!dispatch) return;
|
|
||||||
dispatch(openModal('UNAUTHORIZED', {
|
|
||||||
action: 'POLL_VOTE',
|
|
||||||
ap_id: status,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
const showResults = poll.voted || poll.expired;
|
||||||
const { poll } = this.props;
|
|
||||||
if (!poll) return null;
|
|
||||||
|
|
||||||
const { selected } = this.state;
|
return (
|
||||||
const showResults = poll.voted || poll.expired;
|
<div onClick={e => e.stopPropagation()}>
|
||||||
|
<Stack className={classNames('my-2', { voted: poll.voted })}>
|
||||||
return (
|
<Stack>
|
||||||
<div onClick={e => e.stopPropagation()}>
|
{poll.options.map((option, i) => (
|
||||||
<Stack className={classNames('my-2', { voted: poll.voted })}>
|
<PollOption
|
||||||
<Stack>
|
key={i}
|
||||||
{poll.options.map((option, i) => (
|
poll={poll}
|
||||||
<PollOption
|
option={option}
|
||||||
key={i}
|
index={i}
|
||||||
poll={poll}
|
showResults={showResults}
|
||||||
option={option}
|
active={!!selected[i]}
|
||||||
index={i}
|
onToggle={toggleOption}
|
||||||
showResults={showResults}
|
/>
|
||||||
active={!!this.state.selected[i]}
|
))}
|
||||||
onToggle={this.toggleOption}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
</Stack>
|
|
||||||
|
|
||||||
<PollFooter
|
|
||||||
poll={poll}
|
|
||||||
showResults={showResults}
|
|
||||||
selected={selected}
|
|
||||||
/>
|
|
||||||
</Stack>
|
</Stack>
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
<PollFooter
|
||||||
|
poll={poll}
|
||||||
|
showResults={showResults}
|
||||||
|
selected={selected}
|
||||||
|
/>
|
||||||
|
</Stack>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default injectIntl(Poll);
|
export default Poll;
|
||||||
|
|
|
@ -8,7 +8,7 @@ import { withRouter } from 'react-router-dom';
|
||||||
|
|
||||||
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
|
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
|
||||||
import Icon from 'soapbox/components/icon';
|
import Icon from 'soapbox/components/icon';
|
||||||
import PollContainer from 'soapbox/containers/poll_container';
|
import Poll from 'soapbox/components/poll';
|
||||||
import { addGreentext } from 'soapbox/utils/greentext';
|
import { addGreentext } from 'soapbox/utils/greentext';
|
||||||
import { onlyEmoji } from 'soapbox/utils/rich_content';
|
import { onlyEmoji } from 'soapbox/utils/rich_content';
|
||||||
|
|
||||||
|
@ -244,7 +244,7 @@ class StatusContent extends React.PureComponent {
|
||||||
|
|
||||||
<div tabIndex={!hidden ? 0 : null} className={`status__content__text ${!hidden ? 'status__content__text--visible' : ''}`} style={directionStyle} dangerouslySetInnerHTML={content} lang={status.get('language')} />
|
<div tabIndex={!hidden ? 0 : null} className={`status__content__text ${!hidden ? 'status__content__text--visible' : ''}`} style={directionStyle} dangerouslySetInnerHTML={content} lang={status.get('language')} />
|
||||||
|
|
||||||
{!hidden && !!status.get('poll') && <PollContainer pollId={status.get('poll')} status={status.get('url')} />}
|
{!hidden && !!status.get('poll') && <Poll id={status.get('poll')} status={status.get('url')} />}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
} else if (this.props.onClick) {
|
} else if (this.props.onClick) {
|
||||||
|
@ -267,7 +267,7 @@ class StatusContent extends React.PureComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status.get('poll')) {
|
if (status.get('poll')) {
|
||||||
output.push(<PollContainer pollId={status.get('poll')} key='poll' status={status.get('url')} />);
|
output.push(<Poll id={status.get('poll')} key='poll' status={status.get('url')} />);
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
|
@ -287,7 +287,7 @@ class StatusContent extends React.PureComponent {
|
||||||
];
|
];
|
||||||
|
|
||||||
if (status.get('poll')) {
|
if (status.get('poll')) {
|
||||||
output.push(<PollContainer pollId={status.get('poll')} key='poll' status={status.get('url')} />);
|
output.push(<Poll id={status.get('poll')} key='poll' status={status.get('url')} />);
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
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);
|
|
Ładowanie…
Reference in New Issue