UNPKG

stream-chat-react

Version:

React components to create chat conversations or livestream style chat

38 lines (37 loc) 1.76 kB
import React from 'react'; import { FormDialog } from '../../Dialog/FormDialog'; import { useChatContext, usePollContext, useTranslationContext } from '../../../context'; import { useStateStore } from '../../../store'; const pollStateSelector = (nextValue) => ({ options: nextValue.options }); export const SuggestPollOptionForm = ({ close, messageId, }) => { const { client } = useChatContext('SuggestPollOptionForm'); const { t } = useTranslationContext('SuggestPollOptionForm'); const { poll } = usePollContext(); const { options } = useStateStore(poll.state, pollStateSelector); return (React.createElement(FormDialog, { className: 'str-chat__prompt-dialog str-chat__modal__suggest-poll-option', close: close, fields: { optionText: { element: 'input', props: { id: 'optionText', name: 'optionText', required: true, type: 'text', value: '', }, validator: (value) => { if (!value) return; const existingOption = options.find((option) => option.text === value.trim()); if (existingOption) { return new Error(t('Option already exists')); } return; }, }, }, onSubmit: async (value) => { const { poll_option } = await client.createPollOption(poll.id, { text: value.optionText, }); poll.castVote(poll_option.id, messageId); }, shouldDisableSubmitButton: (value) => !value.optionText, title: t('Suggest an option') })); };