From 65c4015dccda23c4b802e8645f4f7cca20c81401 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 17 Oct 2022 17:34:56 +0200 Subject: [PATCH] Poll: Quiz creation fixes (#2073) --- src/components/middle/composer/PollModal.scss | 2 +- src/components/middle/composer/PollModal.tsx | 28 +++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/components/middle/composer/PollModal.scss b/src/components/middle/composer/PollModal.scss index b1ced1573..a92a8580d 100644 --- a/src/components/middle/composer/PollModal.scss +++ b/src/components/middle/composer/PollModal.scss @@ -73,7 +73,7 @@ color: var(--color-text-secondary); } - .error { + .poll-error { font-size: smaller; color: var(--color-error); margin: -1rem 0 1rem 0.25rem; diff --git a/src/components/middle/composer/PollModal.tsx b/src/components/middle/composer/PollModal.tsx index 77726c949..ee0530f94 100644 --- a/src/components/middle/composer/PollModal.tsx +++ b/src/components/middle/composer/PollModal.tsx @@ -48,7 +48,7 @@ const PollModal: FC = ({ const [isMultipleAnswers, setIsMultipleAnswers] = useState(false); const [isQuizMode, setIsQuizMode] = useState(isQuiz || false); const [solution, setSolution] = useState(''); - const [correctOption, setCorrectOption] = useState(); + const [correctOption, setCorrectOption] = useState(); const [hasErrors, setHasErrors] = useState(false); const lang = useLang(); @@ -68,7 +68,7 @@ const PollModal: FC = ({ setIsMultipleAnswers(false); setIsQuizMode(isQuiz || false); setSolution(''); - setCorrectOption(''); + setCorrectOption(undefined); setHasErrors(false); } }, [isQuiz, isOpen]); @@ -120,7 +120,7 @@ const PollModal: FC = ({ return; } - if (isQuizMode && (!correctOption || !optionsTrimmed[Number(correctOption)])) { + if (isQuizMode && (correctOption === undefined || !optionsTrimmed[correctOption])) { setHasErrors(true); return; } @@ -129,7 +129,7 @@ const PollModal: FC = ({ .map((text, index) => ({ text: text.trim(), option: String(index), - ...(String(index) === correctOption && { correct: true }), + ...(index === correctOption && { correct: true }), })); const payload: ApiNewPoll = { @@ -146,7 +146,7 @@ const PollModal: FC = ({ const { text, entities } = (solution && parseMessageInput(solution.substring(0, MAX_SOLUTION_LENGTH))) || {}; payload.quiz = { - correctAnswers: [correctOption], + correctAnswers: [String(correctOption)], ...(text && { solution: text }), ...(entities && { solutionEntities: entities }), }; @@ -180,6 +180,15 @@ const PollModal: FC = ({ const newOptions = [...options]; newOptions.splice(index, 1); setOptions(newOptions); + + if (correctOption !== undefined) { + if (correctOption === index) { + setCorrectOption(undefined); + } else if (index < correctOption) { + setCorrectOption(correctOption - 1); + } + } + requestAnimationFrame(() => { if (!optionsListRef.current) { return; @@ -187,10 +196,10 @@ const PollModal: FC = ({ optionsListRef.current.classList.toggle('overflown', optionsListRef.current.scrollHeight > MAX_LIST_HEIGHT); }); - }, [options]); + }, [correctOption, options]); const handleCorrectOptionChange = useCallback((newValue: string) => { - setCorrectOption(newValue); + setCorrectOption(Number(newValue)); }, [setCorrectOption]); const handleIsAnonymousChange = useCallback((e: ChangeEvent) => { @@ -288,8 +297,8 @@ const PollModal: FC = ({ function renderQuizNoOptionError() { const optionsTrimmed = options.map((o) => o.trim()).filter((o) => o.length); - return isQuizMode && (!correctOption || !optionsTrimmed[Number(correctOption)]) && ( -

{lang('lng_polls_choose_correct')}

+ return isQuizMode && (correctOption === undefined || !optionsTrimmed[correctOption]) && ( +

{lang('lng_polls_choose_correct')}

); } @@ -313,6 +322,7 @@ const PollModal: FC = ({ ) : (