From 719d7f06552e85cb15f320e1050356c8f32530ef Mon Sep 17 00:00:00 2001 From: Nathan Sparrow <24910097+DismalShadowX@users.noreply.github.com> Date: Mon, 4 Nov 2024 02:42:07 -0400 Subject: [PATCH] Update poll_form.jsx Add 14 days duration option to poll forms and character limit to 100 --- .../features/compose/components/poll_form.jsx | 162 ++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 app/javascript/mastodon/features/compose/components/poll_form.jsx diff --git a/app/javascript/mastodon/features/compose/components/poll_form.jsx b/app/javascript/mastodon/features/compose/components/poll_form.jsx new file mode 100644 index 00000000000000..9cbfcb0ebc0f6a --- /dev/null +++ b/app/javascript/mastodon/features/compose/components/poll_form.jsx @@ -0,0 +1,162 @@ +import PropTypes from 'prop-types'; +import { useCallback } from 'react'; + +import { defineMessages, useIntl } from 'react-intl'; + +import classNames from 'classnames'; + +import { useDispatch, useSelector } from 'react-redux'; + +import { + changePollSettings, + changePollOption, + clearComposeSuggestions, + fetchComposeSuggestions, + selectComposeSuggestion, +} from 'mastodon/actions/compose'; +import AutosuggestInput from 'mastodon/components/autosuggest_input'; + +const messages = defineMessages({ + option_placeholder: { id: 'compose_form.poll.option_placeholder', defaultMessage: 'Option {number}' }, + duration: { id: 'compose_form.poll.duration', defaultMessage: 'Poll length' }, + type: { id: 'compose_form.poll.type', defaultMessage: 'Style' }, + switchToMultiple: { id: 'compose_form.poll.switch_to_multiple', defaultMessage: 'Change poll to allow multiple choices' }, + switchToSingle: { id: 'compose_form.poll.switch_to_single', defaultMessage: 'Change poll to allow for a single choice' }, + minutes: { id: 'intervals.full.minutes', defaultMessage: '{number, plural, one {# minute} other {# minutes}}' }, + hours: { id: 'intervals.full.hours', defaultMessage: '{number, plural, one {# hour} other {# hours}}' }, + days: { id: 'intervals.full.days', defaultMessage: '{number, plural, one {# day} other {# days}}' }, + singleChoice: { id: 'compose_form.poll.single', defaultMessage: 'Single choice' }, + multipleChoice: { id: 'compose_form.poll.multiple', defaultMessage: 'Multiple choice' }, +}); + +const Select = ({ label, options, value, onChange }) => { + return ( + + ); +}; + +Select.propTypes = { + label: PropTypes.node, + value: PropTypes.any, + onChange: PropTypes.func, + options: PropTypes.arrayOf(PropTypes.shape({ + label: PropTypes.node, + value: PropTypes.any, + })), +}; + +const Option = ({ multipleChoice, index, title, autoFocus }) => { + const intl = useIntl(); + const dispatch = useDispatch(); + const suggestions = useSelector(state => state.getIn(['compose', 'suggestions'])); + const lang = useSelector(state => state.getIn(['compose', 'language'])); + const maxOptions = useSelector(state => state.getIn(['server', 'server', 'configuration', 'polls', 'max_options'])); + + const handleChange = useCallback(({ target: { value } }) => { + dispatch(changePollOption(index, value, maxOptions)); + }, [dispatch, index, maxOptions]); + + const handleSuggestionsFetchRequested = useCallback(token => { + dispatch(fetchComposeSuggestions(token)); + }, [dispatch]); + + const handleSuggestionsClearRequested = useCallback(() => { + dispatch(clearComposeSuggestions()); + }, [dispatch]); + + const handleSuggestionSelected = useCallback((tokenStart, token, value) => { + dispatch(selectComposeSuggestion(tokenStart, token, value, ['poll', 'options', index])); + }, [dispatch, index]); + + return ( + + ); +}; + +Option.propTypes = { + title: PropTypes.string.isRequired, + index: PropTypes.number.isRequired, + multipleChoice: PropTypes.bool, + autoFocus: PropTypes.bool, +}; + +export const PollForm = () => { + const intl = useIntl(); + const dispatch = useDispatch(); + const poll = useSelector(state => state.getIn(['compose', 'poll'])); + const options = poll?.get('options'); + const expiresIn = poll?.get('expires_in'); + const isMultiple = poll?.get('multiple'); + + const handleDurationChange = useCallback(({ target: { value } }) => { + dispatch(changePollSettings(value, isMultiple)); + }, [dispatch, isMultiple]); + + const handleTypeChange = useCallback(({ target: { value } }) => { + dispatch(changePollSettings(expiresIn, value === 'true')); + }, [dispatch, expiresIn]); + + if (poll === null) { + return null; + } + + return ( +