diff --git a/src/stores/questions/actions.ts b/src/stores/questions/actions.ts index 9bb34e2c..80f4abac 100644 --- a/src/stores/questions/actions.ts +++ b/src/stores/questions/actions.ts @@ -41,7 +41,17 @@ export const setQuestions = (questions: RawQuestion[] | null | undefined) => export const createUntypedQuestion = ( quizId: number, insertAfterQuestionId?: string, -) => +) => { + const { questions } = useQuestionsStore.getState(); + + const questionsAmount = questions.filter( + ({ type }) => type !== "result", + ).length; + + if (questionsAmount >= 100) { + return; + } + setProducedState( (state) => { const newUntypedQuestion = { @@ -70,6 +80,7 @@ export const createUntypedQuestion = ( quizId, }, ); +}; const removeQuestion = (questionId: string) => setProducedState( @@ -530,66 +541,80 @@ export const deleteQuestion = async (questionId: string) => } }); -export const copyQuestion = async (questionId: string, quizId: number) => - requestQueue.enqueue(`copyQuestion-${quizId}-${questionId}`, async () => { - const question = useQuestionsStore - .getState() - .questions.find((q) => q.id === questionId); - if (!question) return; +export const copyQuestion = async (questionId: string, quizId: number) => { + const { questions } = useQuestionsStore.getState(); - const frontId = nanoid(); - if (question.type === null) { - const copiedQuestion = structuredClone(question); - copiedQuestion.id = frontId; + const questionsAmount = questions.filter( + ({ type }) => type !== "result", + ).length; - setProducedState( - (state) => { - state.questions.push(copiedQuestion); - }, - { - type: "copyQuestion", - questionId, + if (questionsAmount >= 100) { + return; + } + + return requestQueue.enqueue( + `copyQuestion-${quizId}-${questionId}`, + async () => { + const question = useQuestionsStore + .getState() + .questions.find((q) => q.id === questionId); + if (!question) return; + + const frontId = nanoid(); + if (question.type === null) { + const copiedQuestion = structuredClone(question); + copiedQuestion.id = frontId; + + setProducedState( + (state) => { + state.questions.push(copiedQuestion); + }, + { + type: "copyQuestion", + questionId, + quizId, + }, + ); + + return; + } + + try { + const { updated: newQuestionId } = await questionApi.copy( + question.backendId, quizId, - }, - ); + ); - return; - } + const copiedQuestion = structuredClone(question); + copiedQuestion.backendId = newQuestionId; + copiedQuestion.id = frontId; + copiedQuestion.content.id = frontId; + copiedQuestion.content.rule = { + main: [], + parentId: "", + default: "", + children: [], + }; - try { - const { updated: newQuestionId } = await questionApi.copy( - question.backendId, - quizId, - ); + setProducedState( + (state) => { + state.questions.push(copiedQuestion); + }, + { + type: "copyQuestion", + questionId, + quizId, + }, + ); - const copiedQuestion = structuredClone(question); - copiedQuestion.backendId = newQuestionId; - copiedQuestion.id = frontId; - copiedQuestion.content.id = frontId; - copiedQuestion.content.rule = { - main: [], - parentId: "", - default: "", - children: [], - }; - - setProducedState( - (state) => { - state.questions.push(copiedQuestion); - }, - { - type: "copyQuestion", - questionId, - quizId, - }, - ); - - updateQuestionOrders(); - } catch (error) { - devlog("Error copying question", error); - enqueueSnackbar("Не удалось скопировать вопрос"); - } - }); + updateQuestionOrders(); + } catch (error) { + devlog("Error copying question", error); + enqueueSnackbar("Не удалось скопировать вопрос"); + } + }, + ); +}; function setProducedState( recipe: (state: QuestionsStore) => void,