// @ts-check import React, { useCallback, useRef, useState } from 'react'; import { HMSPollQuestionCreateParams, HMSPollQuestionOptionCreateParams, HMSPollQuestionType, } from '@100mslive/react-sdk'; import { AddCircleIcon, TrashIcon } from '@100mslive/react-icons'; // import { Button, Dropdown, Flex, IconButton, Input, Switch, Text, TextArea, Tooltip } from '../../../..'; import { Button, Dropdown, Flex, IconButton, Input, Text, TextArea, Tooltip } from '../../../..'; // @ts-ignore import { DialogDropdownTrigger } from '../../../primitives/DropdownTrigger'; // @ts-ignore import { DeleteQuestionModal } from './DeleteQuestionModal'; // @ts-ignore import { useDropdownSelection } from '../../hooks/useDropdownSelection'; // @ts-ignore import { isValidTextInput } from '../../../common/utils'; import { Line } from '../common/Line'; // @ts-ignore import { MultipleChoiceOptionInputs } from '../common/MultipleChoiceOptions'; // @ts-ignore import { SingleChoiceOptionInputs } from '../common/SingleChoiceOptions'; import { QUESTION_TYPE, QUESTION_TYPE_TITLE } from '../../../common/constants'; export const QuestionForm = ({ question, index, length, onSave, removeQuestion, isQuiz, }: { question: HMSPollQuestionCreateParams & { draftID: number }; index: number; length: number; onSave: (optionParams: HMSPollQuestionCreateParams & { draftID: number; saved: boolean }) => void; removeQuestion: () => void; isQuiz: boolean; }) => { const ref = useRef(null); const selectionBg = useDropdownSelection(); const [openDelete, setOpenDelete] = useState(false); const [open, setOpen] = useState(false); const [type, setType] = useState(question.type || QUESTION_TYPE.SINGLE_CHOICE); const [text, setText] = useState(question.text); const [weight, setWeight] = useState(isQuiz ? 10 : 1); const [options, setOptions] = useState( question?.options || [ { text: '', isCorrectAnswer: false }, { text: '', isCorrectAnswer: false }, ], ); // const [skippable, setSkippable] = useState(false); const isValid = isValidQuestion({ text, type, options, weight, isQuiz, }); const handleOptionTextChange = useCallback( (index: number, text: string) => { setOptions(options => [...options.slice(0, index), { ...options[index], text }, ...options.slice(index + 1)]); }, [setOptions], ); const removeOption = useCallback( (index: number) => setOptions(options => { const newOptions = [...options]; newOptions.splice(index, 1); return newOptions; }), [setOptions], ); const selectSingleChoiceAnswer = useCallback( (answerIndex: number) => { if (!isQuiz) { return; } setOptions(options => options.map((option, index) => ({ ...option, isCorrectAnswer: index === answerIndex, })), ); }, [setOptions, isQuiz], ); const selectMultipleChoiceAnswer = useCallback( (checked: boolean, index: number) => { if (!isQuiz) { return; } setOptions(options => [ ...options.slice(0, index), { ...options[index], isCorrectAnswer: checked }, ...options.slice(index + 1), ]); }, [setOptions, isQuiz], ); return ( <> Question {index + 1} of {length} Question Type {/* @ts-ignore */} {Object.keys(QUESTION_TYPE_TITLE).map(value => { return ( setType(value)} css={{ px: '$9', bg: type === value ? selectionBg : undefined, }} > {/* @ts-ignore */} {QUESTION_TYPE_TITLE[value]} ); })}