From 1a1464f5bae69cdd2bf11a975b73712b19d4014e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 26 Mar 2022 22:24:52 -0500 Subject: [PATCH] Poll: convert to React.FC, remove PollContainer --- app/soapbox/components/poll.tsx | 120 ++++++++++------------- app/soapbox/components/status_content.js | 8 +- app/soapbox/containers/poll_container.ts | 17 ---- 3 files changed, 57 insertions(+), 88 deletions(-) delete mode 100644 app/soapbox/containers/poll_container.ts diff --git a/app/soapbox/components/poll.tsx b/app/soapbox/components/poll.tsx index 52518172e..577426a0c 100644 --- a/app/soapbox/components/poll.tsx +++ b/app/soapbox/components/poll.tsx @@ -1,7 +1,6 @@ import classNames from 'classnames'; -import React from 'react'; -import ImmutablePureComponent from 'react-immutable-pure-component'; -import { defineMessages, injectIntl, useIntl, FormattedMessage, IntlShape } from 'react-intl'; +import React, { useState } from 'react'; +import { defineMessages, useIntl, FormattedMessage } from 'react-intl'; import { spring } from 'react-motion'; import { useDispatch } from 'react-redux'; @@ -10,6 +9,7 @@ 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'; @@ -130,18 +130,6 @@ const PollOption: React.FC = (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 dispatch = useDispatch(); @@ -197,77 +185,75 @@ const PollFooter: React.FC = ({ poll, showResults, selected }): JSX type Selected = Record; -class Poll extends ImmutablePureComponent { +interface IPoll { + id: string, + status?: string, +} - state = { - selected: {} as Selected, +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, + })); }; - toggleOption = (value: number) => { - const { me, poll } = this.props; - + const toggleOption = (value: number) => { if (me) { if (poll?.multiple) { - const tmp = { ...this.state.selected }; + const tmp = { ...selected }; if (tmp[value]) { delete tmp[value]; } else { tmp[value] = true; } - this.setState({ selected: tmp }); + setSelected(tmp); } else { const tmp: Selected = {}; tmp[value] = true; - this.setState({ selected: tmp }); + setSelected(tmp); } } else { - this.openUnauthorizedModal(); + openUnauthorizedModal(); } - } + }; - openUnauthorizedModal = () => { - const { dispatch, status } = this.props; - if (!dispatch) return; - dispatch(openModal('UNAUTHORIZED', { - action: 'POLL_VOTE', - ap_id: status, - })); - } + if (!poll) return null; - render() { - const { poll } = this.props; - if (!poll) return null; + const showResults = poll.voted || poll.expired; - const { selected } = this.state; - const showResults = poll.voted || poll.expired; - - return ( -
e.stopPropagation()}> - - - {poll.options.map((option, i) => ( - - ))} - - - + return ( +
e.stopPropagation()}> + + + {poll.options.map((option, i) => ( + + ))} -
- ); - } -} + +
+
+ ); +}; -export default injectIntl(Poll); +export default Poll; diff --git a/app/soapbox/components/status_content.js b/app/soapbox/components/status_content.js index 2e6105a52..2bd5c0712 100644 --- a/app/soapbox/components/status_content.js +++ b/app/soapbox/components/status_content.js @@ -8,7 +8,7 @@ import { withRouter } from 'react-router-dom'; import { getSoapboxConfig } from 'soapbox/actions/soapbox'; 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 { onlyEmoji } from 'soapbox/utils/rich_content'; @@ -244,7 +244,7 @@ class StatusContent extends React.PureComponent {
- {!hidden && !!status.get('poll') && } + {!hidden && !!status.get('poll') && }
); } else if (this.props.onClick) { @@ -267,7 +267,7 @@ class StatusContent extends React.PureComponent { } if (status.get('poll')) { - output.push(); + output.push(); } return output; @@ -287,7 +287,7 @@ class StatusContent extends React.PureComponent { ]; if (status.get('poll')) { - output.push(); + output.push(); } return output; diff --git a/app/soapbox/containers/poll_container.ts b/app/soapbox/containers/poll_container.ts deleted file mode 100644 index 8d83977af..000000000 --- a/app/soapbox/containers/poll_container.ts +++ /dev/null @@ -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);