pleroma/app/soapbox/features/compose/components/polls/poll-form.tsx

210 lines
7.2 KiB
TypeScript
Raw Normal View History

2022-06-06 10:19:45 -07:00
import React from 'react';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
2022-06-14 11:07:43 -07:00
import { addPollOption, changePollOption, changePollSettings, clearComposeSuggestions, fetchComposeSuggestions, removePoll, removePollOption, selectComposeSuggestion } from 'soapbox/actions/compose';
2022-11-15 08:11:42 -08:00
import AutosuggestInput from 'soapbox/components/autosuggest-input';
2022-06-07 08:38:54 -07:00
import { Button, Divider, HStack, Stack, Text, Toggle } from 'soapbox/components/ui';
import { useAppDispatch, useAppSelector, useCompose } from 'soapbox/hooks';
2022-06-06 10:19:45 -07:00
2022-06-14 10:52:08 -07:00
import DurationSelector from './duration-selector';
2022-06-07 08:11:28 -07:00
2022-11-15 08:11:42 -08:00
import type { AutoSuggestion } from 'soapbox/components/autosuggest-input';
2022-06-06 11:38:48 -07:00
2022-06-06 10:19:45 -07:00
const messages = defineMessages({
2022-06-07 08:11:28 -07:00
option_placeholder: { id: 'compose_form.poll.option_placeholder', defaultMessage: 'Answer #{number}' },
add_option: { id: 'compose_form.poll.add_option', defaultMessage: 'Add an answer' },
2022-06-07 08:38:54 -07:00
pollDuration: { id: 'compose_form.poll.duration', defaultMessage: 'Duration' },
removePoll: { id: 'compose_form.poll.remove', defaultMessage: 'Remove poll' },
2022-06-07 08:11:28 -07:00
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' },
2022-06-06 10:19:45 -07:00
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}}' },
2022-06-07 08:38:54 -07:00
multiSelect: { id: 'compose_form.poll.multiselect', defaultMessage: 'Multi-Select' },
multiSelectDetail: { id: 'compose_form.poll.multiselect_detail', defaultMessage: 'Allow users to select multiple answers' },
2022-06-06 10:19:45 -07:00
});
interface IOption {
composeId: string
2022-06-06 10:19:45 -07:00
index: number
maxChars: number
numOptions: number
onChange(index: number, value: string): void
onRemove(index: number): void
onRemovePoll(): void
title: string
}
const Option: React.FC<IOption> = ({
composeId,
index,
maxChars,
numOptions,
onChange,
onRemove,
onRemovePoll,
title,
}) => {
2022-06-14 11:07:43 -07:00
const dispatch = useAppDispatch();
2022-06-06 10:19:45 -07:00
const intl = useIntl();
const suggestions = useCompose(composeId).suggestions;
2022-06-14 11:07:43 -07:00
2022-06-06 10:19:45 -07:00
const handleOptionTitleChange = (event: React.ChangeEvent<HTMLInputElement>) => onChange(index, event.target.value);
const handleOptionRemove = () => {
if (numOptions > 2) {
onRemove(index);
} else {
onRemovePoll();
}
};
const onSuggestionsClearRequested = () => dispatch(clearComposeSuggestions(composeId));
2022-06-06 10:19:45 -07:00
const onSuggestionsFetchRequested = (token: string) => dispatch(fetchComposeSuggestions(composeId, token));
2022-06-06 10:19:45 -07:00
2022-06-06 11:38:48 -07:00
const onSuggestionSelected = (tokenStart: number, token: string | null, value: AutoSuggestion) => {
2022-06-14 11:19:46 -07:00
if (token && typeof token === 'string') {
dispatch(selectComposeSuggestion(composeId, tokenStart, token, value, ['poll', 'options', index]));
2022-06-06 11:38:48 -07:00
}
};
2022-06-06 10:19:45 -07:00
return (
2022-06-07 08:11:28 -07:00
<HStack alignItems='center' justifyContent='between' space={4}>
<HStack alignItems='center' space={2} grow>
<div className='w-6'>
2022-06-07 08:25:24 -07:00
<Text weight='bold'>{index + 1}.</Text>
2022-06-07 08:11:28 -07:00
</div>
2022-06-06 10:19:45 -07:00
<AutosuggestInput
className='rounded-md dark:!bg-transparent !bg-transparent'
2022-06-06 10:19:45 -07:00
placeholder={intl.formatMessage(messages.option_placeholder, { number: index + 1 })}
maxLength={maxChars}
value={title}
onChange={handleOptionTitleChange}
suggestions={suggestions}
onSuggestionsFetchRequested={onSuggestionsFetchRequested}
onSuggestionsClearRequested={onSuggestionsClearRequested}
onSuggestionSelected={onSuggestionSelected}
searchTokens={[':']}
2022-06-14 10:52:08 -07:00
autoFocus={index === 0 || index >= 2}
2022-06-06 10:19:45 -07:00
/>
2022-06-07 08:11:28 -07:00
</HStack>
2022-06-06 10:19:45 -07:00
2022-06-07 08:11:28 -07:00
{index > 1 && (
<div>
<Button theme='danger' size='sm' onClick={handleOptionRemove}>
2022-11-18 15:28:56 -08:00
<FormattedMessage id='compose_form.poll.remove_option' defaultMessage='Delete' />
</Button>
2022-06-07 08:11:28 -07:00
</div>
)}
</HStack>
2022-06-06 10:19:45 -07:00
);
};
interface IPollForm {
composeId: string,
}
const PollForm: React.FC<IPollForm> = ({ composeId }) => {
2022-06-14 11:07:43 -07:00
const dispatch = useAppDispatch();
2022-06-07 08:38:54 -07:00
const intl = useIntl();
const compose = useCompose(composeId);
2022-06-06 10:19:45 -07:00
const pollLimits = useAppSelector((state) => state.instance.getIn(['configuration', 'polls']) as any);
const options = compose.poll?.options;
const expiresIn = compose.poll?.expires_in;
const isMultiple = compose.poll?.multiple;
2022-06-14 11:07:43 -07:00
2022-06-06 10:19:45 -07:00
const maxOptions = pollLimits.get('max_options');
const maxOptionChars = pollLimits.get('max_characters_per_option');
const onRemoveOption = (index: number) => dispatch(removePollOption(composeId, index));
const onChangeOption = (index: number, title: string) => dispatch(changePollOption(composeId, index, title));
const handleAddOption = () => dispatch(addPollOption(composeId, ''));
2022-06-14 11:07:43 -07:00
const onChangeSettings = (expiresIn: string | number | undefined, isMultiple?: boolean) =>
dispatch(changePollSettings(composeId, expiresIn, isMultiple));
2022-06-07 08:11:28 -07:00
const handleSelectDuration = (value: number) => onChangeSettings(value, isMultiple);
2022-06-07 08:38:54 -07:00
const handleToggleMultiple = () => onChangeSettings(expiresIn, !isMultiple);
const onRemovePoll = () => dispatch(removePoll(composeId));
2022-06-06 10:19:45 -07:00
if (!options) {
return null;
}
return (
2022-06-07 08:11:28 -07:00
<Stack space={4}>
<Stack space={2}>
2022-06-06 10:19:45 -07:00
{options.map((title: string, i: number) => (
<Option
composeId={composeId}
2022-06-06 10:19:45 -07:00
title={title}
key={i}
index={i}
onChange={onChangeOption}
onRemove={onRemoveOption}
maxChars={maxOptionChars}
numOptions={options.size}
2022-06-14 11:07:43 -07:00
onRemovePoll={onRemovePoll}
2022-06-06 10:19:45 -07:00
/>
))}
2022-06-07 08:11:28 -07:00
<HStack space={2}>
<div className='w-6' />
{options.size < maxOptions && (
<Button
theme='secondary'
onClick={handleAddOption}
size='sm'
2022-06-09 07:30:27 -07:00
block
2022-06-07 08:11:28 -07:00
>
<FormattedMessage {...messages.add_option} />
</Button>
)}
</HStack>
</Stack>
2022-06-07 08:24:40 -07:00
<Divider />
2022-06-07 08:11:28 -07:00
<button type='button' onClick={handleToggleMultiple} className='text-left'>
2022-06-09 06:58:19 -07:00
<HStack alignItems='center' justifyContent='between'>
<Stack>
<Text weight='medium'>
{intl.formatMessage(messages.multiSelect)}
</Text>
<Text theme='muted' size='sm'>
{intl.formatMessage(messages.multiSelectDetail)}
</Text>
</Stack>
<Toggle checked={isMultiple} onChange={handleToggleMultiple} />
</HStack>
</button>
2022-06-07 08:38:54 -07:00
<Divider />
2022-06-07 08:11:28 -07:00
{/* Duration */}
<Stack space={2}>
2022-06-09 06:58:19 -07:00
<Text weight='medium'>
2022-06-07 08:38:54 -07:00
{intl.formatMessage(messages.pollDuration)}
</Text>
2022-06-07 08:11:28 -07:00
<DurationSelector onDurationChange={handleSelectDuration} />
</Stack>
{/* Remove Poll */}
<div className='text-center'>
<button type='button' className='text-danger-500' onClick={onRemovePoll}>
2022-06-07 08:38:54 -07:00
{intl.formatMessage(messages.removePoll)}
</button>
2022-06-07 08:11:28 -07:00
</div>
</Stack>
2022-06-06 10:19:45 -07:00
);
};
export default PollForm;