'use strict'; import React from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import AutosuggestInput from 'soapbox/components/autosuggest_input'; import { Button, Divider, HStack, Stack, Text } from 'soapbox/components/ui'; import { useAppSelector } from 'soapbox/hooks'; import DurationSelector from './polls/duration-selector'; import type { AutoSuggestion } from 'soapbox/components/autosuggest_input'; const messages = defineMessages({ option_placeholder: { id: 'compose_form.poll.option_placeholder', defaultMessage: 'Answer #{number}' }, add_option: { id: 'compose_form.poll.add_option', defaultMessage: 'Add an answer' }, remove_option: { id: 'compose_form.poll.remove_option', defaultMessage: 'Remove this answer' }, poll_duration: { id: 'compose_form.poll.duration', defaultMessage: 'Poll duration' }, switchToMultiple: { id: 'compose_form.poll.switch_to_multiple', defaultMessage: 'Change poll to allow multiple answers' }, switchToSingle: { id: 'compose_form.poll.switch_to_single', defaultMessage: 'Change poll to allow for a single answer' }, 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}}' }, }); interface IOption { index: number maxChars: number numOptions: number onChange(index: number, value: string): void onClearSuggestions(): void onFetchSuggestions(token: string): void onRemove(index: number): void onRemovePoll(): void onSuggestionSelected(tokenStart: number, token: string, value: string, key: (string | number)[]): void suggestions?: any // list title: string } const Option = (props: IOption) => { const { index, maxChars, numOptions, onChange, onClearSuggestions, onFetchSuggestions, onRemove, onRemovePoll, suggestions, title, } = props; const intl = useIntl(); const handleOptionTitleChange = (event: React.ChangeEvent) => onChange(index, event.target.value); const handleOptionRemove = () => { if (numOptions > 2) { onRemove(index); } else { onRemovePoll(); } }; // const handleToggleMultiple = (event: React.MouseEvent | React.KeyboardEvent) => { // event.preventDefault(); // event.stopPropagation(); // onToggleMultiple(); // }; const onSuggestionsClearRequested = () => onClearSuggestions(); // const handleCheckboxKeypress = (event: React.KeyboardEvent) => { // if (event.key === 'Enter' || event.key === ' ') { // handleToggleMultiple(event); // } // }; const onSuggestionsFetchRequested = (token: string) => onFetchSuggestions(token); const onSuggestionSelected = (tokenStart: number, token: string | null, value: AutoSuggestion) => { if (token && typeof value === 'string') { props.onSuggestionSelected(tokenStart, token, value, ['poll', 'options', index]); } }; return (
{index + 1}
{index > 1 && (
)}
); }; interface IPollForm { expiresIn?: number isMultiple?: boolean onAddOption(value: string): void onChangeOption(): void onChangeSettings(value: string | number | undefined, isMultiple?: boolean): void onClearSuggestions(): void onFetchSuggestions(token: string): void onRemoveOption(): void onRemovePoll(): void onSuggestionSelected(tokenStart: number, token: string, value: string, key: (string | number)[]): void options?: any suggestions?: any // list } const PollForm = (props: IPollForm) => { const { expiresIn, isMultiple, onAddOption, onChangeOption, onChangeSettings, onRemoveOption, options, ...filteredProps } = props; const pollLimits = useAppSelector((state) => state.instance.getIn(['configuration', 'polls']) as any); const maxOptions = pollLimits.get('max_options'); const maxOptionChars = pollLimits.get('max_characters_per_option'); const handleAddOption = () => onAddOption(''); const handleSelectDuration = (value: number) => onChangeSettings(value, isMultiple); // const handleToggleMultiple = () => onChangeSettings(expiresIn, !isMultiple); if (!options) { return null; } return ( {options.map((title: string, i: number) => (