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

227 lines
6.9 KiB
TypeScript
Raw Normal View History

2022-06-06 10:19:45 -07:00
'use strict';
import React from 'react';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
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';
2022-06-06 10:19:45 -07:00
import { useAppSelector } from 'soapbox/hooks';
2022-06-07 08:11:28 -07:00
import DurationSelector from './polls/duration-selector';
2022-06-06 11:38:48 -07:00
import type { AutoSuggestion } from 'soapbox/components/autosuggest_input';
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' },
remove_option: { id: 'compose_form.poll.remove_option', defaultMessage: 'Remove this 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 {
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<HTMLInputElement>) => onChange(index, event.target.value);
const handleOptionRemove = () => {
if (numOptions > 2) {
onRemove(index);
} else {
onRemovePoll();
}
};
const onSuggestionsClearRequested = () => onClearSuggestions();
const onSuggestionsFetchRequested = (token: string) => onFetchSuggestions(token);
2022-06-06 11:38:48 -07:00
const onSuggestionSelected = (tokenStart: number, token: string | null, value: AutoSuggestion) => {
if (token && typeof value === 'string') {
props.onSuggestionSelected(tokenStart, token, value, ['poll', 'options', index]);
}
};
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
2022-06-07 09:45:36 -07:00
className='rounded-md'
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-07 09:45:36 -07:00
autoFocus={index === 0}
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}>Delete</Button>
</div>
)}
</HStack>
2022-06-06 10:19:45 -07:00
);
};
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;
2022-06-07 08:38:54 -07:00
const intl = useIntl();
2022-06-06 10:19:45 -07:00
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');
2022-06-08 04:44:49 -07:00
const handleAddOption = () => {
// autofocus on new input
// use streamfield
onAddOption('');
};
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);
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
title={title}
key={i}
index={i}
onChange={onChangeOption}
onRemove={onRemoveOption}
maxChars={maxOptionChars}
numOptions={options.size}
{...filteredProps}
/>
))}
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'
>
<FormattedMessage {...messages.add_option} />
</Button>
)}
</HStack>
</Stack>
2022-06-07 08:24:40 -07:00
<Divider />
2022-06-07 08:11:28 -07:00
2022-06-09 06:58:19 -07:00
<button onClick={handleToggleMultiple} className='text-left'>
<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'>
2022-06-09 07:06:58 -07:00
<Button theme='danger' size='sm' onClick={props.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;