diff --git a/src/pages/Questions/Form/FormQuestionsPage.tsx b/src/pages/Questions/Form/FormQuestionsPage.tsx index dd695573..6e61344a 100644 --- a/src/pages/Questions/Form/FormQuestionsPage.tsx +++ b/src/pages/Questions/Form/FormQuestionsPage.tsx @@ -11,6 +11,7 @@ import { FormDraggableList } from "./FormDraggableList"; import { collapseAllQuestions, createUntypedQuestion, + createUntypedQuestionForm, } from "@root/questions/actions"; import { useCurrentQuiz } from "@root/quizes/hooks"; @@ -76,7 +77,7 @@ export default function FormQuestionsPage() { }, }} onClick={() => { - createUntypedQuestion(quiz.backendId); + createUntypedQuestionForm(quiz.backendId); }} data-cy="create-question" > diff --git a/src/stores/questions/actions.ts b/src/stores/questions/actions.ts index 0d064a04..023a8ac5 100644 --- a/src/stores/questions/actions.ts +++ b/src/stores/questions/actions.ts @@ -29,6 +29,7 @@ export const setQuestions = (questions: RawQuestion[] | null | undefined) => export const createUntypedQuestion = (quizId: number, insertAfterQuestionId?: string) => { const { questions } = useQuestionsStore.getState(); + console.log("insertAfterQuestionId ", insertAfterQuestionId) const questionsAmount = questions.filter(({ type }) => type !== "result").length; @@ -65,6 +66,44 @@ export const createUntypedQuestion = (quizId: number, insertAfterQuestionId?: st ); }; +export const createUntypedQuestionForm = (quizId: number, insertAfterQuestionId?: string) => { + const { questions } = useQuestionsStore.getState(); + console.log("insertAfterQuestionId ", insertAfterQuestionId) + + const questionsAmount = questions.filter(({ type }) => type !== "result").length; + + if (questionsAmount >= 100) { + return; + } + + setProducedState( + (state) => { + const newUntypedQuestion = { + id: nanoid(), + quizId, + type: null, + title: "", + description: "", + deleted: false, + expanded: true, + }; + + if (insertAfterQuestionId) { + const index = state.questions.findIndex((q) => q.id === insertAfterQuestionId); + if (index === -1) return; + state.questions.splice(index + 1, 0, newUntypedQuestion); + return; + } + + state.questions.push(newUntypedQuestion); + }, + { + type: "createUntypedQuestion", + quizId, + } + ); +}; + const removeQuestion = (questionId: string) => setProducedState( (state) => {