2023-11-02 16:45:28 +00:00
|
|
|
|
import { questionApi } from "@api/question";
|
|
|
|
|
import { devlog } from "@frontend/kitui";
|
2023-11-27 23:07:24 +00:00
|
|
|
|
import { questionToEditQuestionRequest } from "@model/question/edit";
|
2023-11-16 16:41:25 +00:00
|
|
|
|
import { QuestionType, RawQuestion, rawQuestionToQuestion } from "@model/question/question";
|
2023-11-15 18:38:02 +00:00
|
|
|
|
import { AnyQuizQuestion, ImageQuestionVariant, QuestionVariant, createQuestionImageVariant, createQuestionVariant } from "@model/questionTypes/shared";
|
2023-11-02 16:45:28 +00:00
|
|
|
|
import { produce } from "immer";
|
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
2023-11-14 16:44:27 +00:00
|
|
|
|
import { isAxiosCanceledError } from "../../utils/isAxiosCanceledError";
|
2023-11-27 23:07:24 +00:00
|
|
|
|
import { notReachable } from "../../utils/notReachable";
|
2023-11-23 19:07:57 +00:00
|
|
|
|
import { RequestQueue } from "../../utils/requestQueue";
|
2023-11-27 23:07:24 +00:00
|
|
|
|
import { QuestionsStore, useQuestionsStore } from "./store";
|
|
|
|
|
import { nanoid } from "nanoid";
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
|
|
|
|
|
2023-11-14 20:15:52 +00:00
|
|
|
|
export const setQuestions = (questions: RawQuestion[] | null) => setProducedState(state => {
|
2023-11-16 16:41:25 +00:00
|
|
|
|
state.questions = questions?.map(rawQuestionToQuestion) ?? [];
|
2023-11-14 16:44:27 +00:00
|
|
|
|
}, {
|
2023-11-14 20:15:52 +00:00
|
|
|
|
type: "setQuestions",
|
|
|
|
|
questions,
|
2023-11-14 16:44:27 +00:00
|
|
|
|
});
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
2023-11-17 15:42:49 +00:00
|
|
|
|
const addQuestion = (question: AnyQuizQuestion) => setProducedState(state => {
|
|
|
|
|
state.questions.push(question);
|
|
|
|
|
}, {
|
|
|
|
|
type: "addQuestion",
|
|
|
|
|
question,
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
const removeQuestion = (questionId: string) => setProducedState(state => {
|
2023-11-16 16:41:25 +00:00
|
|
|
|
const index = state.questions.findIndex(q => q.id === questionId);
|
2023-11-27 23:07:24 +00:00
|
|
|
|
if (index === -1) return;
|
|
|
|
|
|
2023-11-16 16:41:25 +00:00
|
|
|
|
state.questions.splice(index, 1);
|
2023-11-15 18:38:02 +00:00
|
|
|
|
}, {
|
|
|
|
|
type: "removeQuestion",
|
|
|
|
|
questionId,
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
const setQuestionBackendId = (questionId: string, backendId: number) => setProducedState(state => {
|
2023-11-16 16:41:25 +00:00
|
|
|
|
const question = state.questions.find(q => q.id === questionId);
|
2023-11-02 16:45:28 +00:00
|
|
|
|
if (!question) return;
|
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
question.backendId = backendId;
|
2023-11-02 16:45:28 +00:00
|
|
|
|
}, {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
type: "setQuestionBackendId",
|
|
|
|
|
questionId: questionId,
|
|
|
|
|
backendId,
|
2023-11-02 16:45:28 +00:00
|
|
|
|
});
|
|
|
|
|
|
2023-11-16 16:41:25 +00:00
|
|
|
|
export const reorderQuestions = (
|
|
|
|
|
sourceIndex: number,
|
|
|
|
|
destinationIndex: number,
|
|
|
|
|
) => {
|
|
|
|
|
if (sourceIndex === destinationIndex) return;
|
|
|
|
|
|
|
|
|
|
setProducedState(state => {
|
|
|
|
|
const [removed] = state.questions.splice(sourceIndex, 1);
|
|
|
|
|
state.questions.splice(destinationIndex, 0, removed);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
export const toggleExpandQuestion = (questionId: string) => setProducedState(state => {
|
2023-11-16 16:41:25 +00:00
|
|
|
|
const question = state.questions.find(q => q.id === questionId);
|
2023-11-15 18:38:02 +00:00
|
|
|
|
if (!question) return;
|
|
|
|
|
|
|
|
|
|
question.expanded = !question.expanded;
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-16 16:41:25 +00:00
|
|
|
|
export const collapseAllQuestions = () => setProducedState(state => {
|
|
|
|
|
state.questions.forEach(question => question.expanded = false);
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
export const addQuestionVariant = (questionId: string) => {
|
|
|
|
|
updateQuestion(questionId, question => {
|
2023-11-15 18:38:02 +00:00
|
|
|
|
switch (question.type) {
|
|
|
|
|
case "variant":
|
|
|
|
|
case "emoji":
|
|
|
|
|
case "select":
|
|
|
|
|
question.content.variants.push(createQuestionVariant());
|
|
|
|
|
break;
|
|
|
|
|
case "images":
|
|
|
|
|
case "varimg":
|
|
|
|
|
question.content.variants.push(createQuestionImageVariant());
|
|
|
|
|
break;
|
|
|
|
|
case "text":
|
|
|
|
|
case "date":
|
|
|
|
|
case "number":
|
|
|
|
|
case "file":
|
|
|
|
|
case "page":
|
|
|
|
|
case "rating":
|
|
|
|
|
throw new Error(`Cannot add variant to question of type "${question.type}"`);
|
|
|
|
|
default: notReachable(question);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
export const deleteQuestionVariant = (questionId: string, variantId: string) => {
|
|
|
|
|
updateQuestion(questionId, question => {
|
2023-11-15 18:38:02 +00:00
|
|
|
|
if (!("variants" in question.content)) return;
|
|
|
|
|
|
|
|
|
|
const variantIndex = question.content.variants.findIndex(variant => variant.id === variantId);
|
|
|
|
|
if (variantIndex === -1) return;
|
|
|
|
|
|
|
|
|
|
question.content.variants.splice(variantIndex, 1);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const setQuestionVariantField = (
|
2023-11-27 23:07:24 +00:00
|
|
|
|
questionId: string,
|
2023-11-15 18:38:02 +00:00
|
|
|
|
variantId: string,
|
|
|
|
|
field: keyof QuestionVariant,
|
|
|
|
|
value: QuestionVariant[keyof QuestionVariant],
|
|
|
|
|
) => {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
updateQuestion(questionId, question => {
|
2023-11-15 18:38:02 +00:00
|
|
|
|
if (!("variants" in question.content)) return;
|
|
|
|
|
|
|
|
|
|
const variantIndex = question.content.variants.findIndex(variant => variant.id === variantId);
|
|
|
|
|
if (variantIndex === -1) return;
|
|
|
|
|
|
|
|
|
|
const variant = question.content.variants[variantIndex];
|
|
|
|
|
variant[field] = value;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const setQuestionImageVariantField = (
|
2023-11-27 23:07:24 +00:00
|
|
|
|
questionId: string,
|
2023-11-15 18:38:02 +00:00
|
|
|
|
variantId: string,
|
|
|
|
|
field: keyof ImageQuestionVariant,
|
|
|
|
|
value: ImageQuestionVariant[keyof ImageQuestionVariant],
|
|
|
|
|
) => {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
updateQuestion(questionId, question => {
|
2023-11-15 18:38:02 +00:00
|
|
|
|
if (!("variants" in question.content)) return;
|
|
|
|
|
|
|
|
|
|
const variantIndex = question.content.variants.findIndex(variant => variant.id === variantId);
|
|
|
|
|
if (variantIndex === -1) return;
|
|
|
|
|
|
|
|
|
|
const variant = question.content.variants[variantIndex];
|
|
|
|
|
if (!("originalImageUrl" in variant)) return;
|
|
|
|
|
|
|
|
|
|
variant[field] = value;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const reorderQuestionVariants = (
|
2023-11-27 23:07:24 +00:00
|
|
|
|
questionId: string,
|
2023-11-15 18:38:02 +00:00
|
|
|
|
sourceIndex: number,
|
|
|
|
|
destinationIndex: number,
|
|
|
|
|
) => {
|
|
|
|
|
if (sourceIndex === destinationIndex) return;
|
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
updateQuestion(questionId, question => {
|
2023-11-15 18:38:02 +00:00
|
|
|
|
if (!("variants" in question.content)) return;
|
|
|
|
|
|
|
|
|
|
const [removed] = question.content.variants.splice(sourceIndex, 1);
|
|
|
|
|
question.content.variants.splice(destinationIndex, 0, removed);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const setQuestionBackgroundImage = (
|
2023-11-27 23:07:24 +00:00
|
|
|
|
questionId: string,
|
2023-11-15 18:38:02 +00:00
|
|
|
|
url: string,
|
|
|
|
|
) => {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
updateQuestion(questionId, question => {
|
2023-11-15 18:38:02 +00:00
|
|
|
|
if (question.content.back === url) return;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
question.content.back !== question.content.originalBack
|
|
|
|
|
) URL.revokeObjectURL(question.content.back);
|
|
|
|
|
question.content.back = url;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const setQuestionOriginalBackgroundImage = (
|
2023-11-27 23:07:24 +00:00
|
|
|
|
questionId: string,
|
2023-11-15 18:38:02 +00:00
|
|
|
|
url: string,
|
|
|
|
|
) => {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
updateQuestion(questionId, question => {
|
2023-11-15 18:38:02 +00:00
|
|
|
|
if (question.content.originalBack === url) return;
|
|
|
|
|
|
|
|
|
|
URL.revokeObjectURL(question.content.originalBack);
|
|
|
|
|
question.content.originalBack = url;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-16 16:41:25 +00:00
|
|
|
|
export const setVariantImageUrl = (
|
2023-11-27 23:07:24 +00:00
|
|
|
|
questionId: string,
|
2023-11-16 16:41:25 +00:00
|
|
|
|
variantId: string,
|
|
|
|
|
url: string,
|
|
|
|
|
) => {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
updateQuestion(questionId, question => {
|
2023-11-16 16:41:25 +00:00
|
|
|
|
if (!("variants" in question.content)) return;
|
|
|
|
|
|
|
|
|
|
const variant = question.content.variants.find(variant => variant.id === variantId);
|
|
|
|
|
if (!variant || !("originalImageUrl" in variant)) return;
|
|
|
|
|
|
|
|
|
|
if (variant.extendedText === url) return;
|
|
|
|
|
|
|
|
|
|
if (variant.extendedText !== variant.originalImageUrl) URL.revokeObjectURL(variant.extendedText);
|
|
|
|
|
variant.extendedText = url;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const setVariantOriginalImageUrl = (
|
2023-11-27 23:07:24 +00:00
|
|
|
|
questionId: string,
|
2023-11-16 16:41:25 +00:00
|
|
|
|
variantId: string,
|
|
|
|
|
url: string,
|
|
|
|
|
) => {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
updateQuestion(questionId, question => {
|
2023-11-16 16:41:25 +00:00
|
|
|
|
if (!("variants" in question.content)) return;
|
|
|
|
|
|
|
|
|
|
const variant = question.content.variants.find(
|
|
|
|
|
variant => variant.id === variantId
|
|
|
|
|
) as ImageQuestionVariant | undefined;
|
|
|
|
|
if (!variant || !("originalImageUrl" in variant)) return;
|
|
|
|
|
|
|
|
|
|
if (variant.originalImageUrl === url) return;
|
|
|
|
|
|
|
|
|
|
URL.revokeObjectURL(variant.originalImageUrl);
|
|
|
|
|
variant.originalImageUrl = url;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const setPageQuestionPicture = (
|
2023-11-27 23:07:24 +00:00
|
|
|
|
questionId: string,
|
2023-11-16 16:41:25 +00:00
|
|
|
|
url: string,
|
|
|
|
|
) => {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
updateQuestion(questionId, question => {
|
2023-11-16 16:41:25 +00:00
|
|
|
|
if (question.type !== "page") return;
|
|
|
|
|
|
|
|
|
|
if (question.content.picture === url) return;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
question.content.picture !== question.content.originalPicture
|
|
|
|
|
) URL.revokeObjectURL(question.content.picture);
|
|
|
|
|
question.content.picture = url;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const setPageQuestionOriginalPicture = (
|
2023-11-27 23:07:24 +00:00
|
|
|
|
questionId: string,
|
2023-11-16 16:41:25 +00:00
|
|
|
|
url: string,
|
|
|
|
|
) => {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
updateQuestion(questionId, question => {
|
2023-11-16 16:41:25 +00:00
|
|
|
|
if (question.type !== "page") return;
|
|
|
|
|
|
|
|
|
|
if (question.content.originalPicture === url) return;
|
|
|
|
|
|
|
|
|
|
URL.revokeObjectURL(question.content.originalPicture);
|
|
|
|
|
question.content.originalPicture = url;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const setQuestionInnerName = (
|
2023-11-27 23:07:24 +00:00
|
|
|
|
questionId: string,
|
2023-11-16 16:41:25 +00:00
|
|
|
|
name: string,
|
|
|
|
|
) => {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
updateQuestion(questionId, question => {
|
2023-11-16 16:41:25 +00:00
|
|
|
|
question.content.innerName = name;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
const REQUEST_DEBOUNCE = 200;
|
|
|
|
|
const requestQueue = new RequestQueue();
|
2023-11-23 19:07:57 +00:00
|
|
|
|
let requestTimeoutId: ReturnType<typeof setTimeout>;
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
export const updateQuestion = (
|
|
|
|
|
questionId: string,
|
2023-11-15 18:38:02 +00:00
|
|
|
|
updateFn: (question: AnyQuizQuestion) => void,
|
2023-11-02 16:45:28 +00:00
|
|
|
|
) => {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
setProducedState(state => {
|
|
|
|
|
const question = state.questions.find(q => q.id === questionId);
|
|
|
|
|
if (!question) return;
|
|
|
|
|
|
|
|
|
|
updateFn(question);
|
|
|
|
|
}, {
|
|
|
|
|
type: "updateQuestion",
|
|
|
|
|
questionId,
|
|
|
|
|
updateFn: updateFn.toString(),
|
|
|
|
|
});
|
2023-11-15 18:38:02 +00:00
|
|
|
|
|
2023-11-23 19:07:57 +00:00
|
|
|
|
clearTimeout(requestTimeoutId);
|
2023-11-27 23:07:24 +00:00
|
|
|
|
requestTimeoutId = setTimeout(() => {
|
|
|
|
|
requestQueue.enqueue(async () => {
|
|
|
|
|
const question = useQuestionsStore.getState().questions.find(q => q.id === questionId);
|
|
|
|
|
if (!question) return;
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
const response = await questionApi.edit(questionToEditQuestionRequest(question));
|
2023-11-15 18:38:02 +00:00
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
setQuestionBackendId(questionId, response.updated);
|
2023-11-23 19:07:57 +00:00
|
|
|
|
}).catch(error => {
|
|
|
|
|
if (isAxiosCanceledError(error)) return;
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
devlog("Error editing question", { error, questionId });
|
2023-11-23 19:07:57 +00:00
|
|
|
|
enqueueSnackbar("Не удалось сохранить вопрос");
|
|
|
|
|
});
|
|
|
|
|
}, REQUEST_DEBOUNCE);
|
2023-11-02 16:45:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
export const createQuestion = async (quizId: number, type: QuestionType = "variant") => requestQueue.enqueue(async () => {
|
2023-11-02 16:45:28 +00:00
|
|
|
|
try {
|
2023-11-14 16:44:27 +00:00
|
|
|
|
const question = await questionApi.create({
|
|
|
|
|
quiz_id: quizId,
|
2023-11-16 16:41:25 +00:00
|
|
|
|
type,
|
2023-11-14 16:44:27 +00:00
|
|
|
|
});
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
2023-11-17 15:42:49 +00:00
|
|
|
|
addQuestion(rawQuestionToQuestion(question));
|
2023-11-02 16:45:28 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
devlog("Error creating question", error);
|
|
|
|
|
enqueueSnackbar("Не удалось создать вопрос");
|
|
|
|
|
}
|
2023-11-27 23:07:24 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const deleteQuestion = async (questionId: string) => requestQueue.enqueue(async () => {
|
|
|
|
|
const question = useQuestionsStore.getState().questions.find(q => q.id === questionId);
|
|
|
|
|
if (!question) return;
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
|
|
|
|
try {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
await questionApi.delete(question.backendId);
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
|
|
|
|
removeQuestion(questionId);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
devlog("Error deleting question", error);
|
|
|
|
|
enqueueSnackbar("Не удалось удалить вопрос");
|
|
|
|
|
}
|
2023-11-27 23:07:24 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const copyQuestion = async (questionId: string, quizId: number) => requestQueue.enqueue(async () => {
|
|
|
|
|
const question = useQuestionsStore.getState().questions.find(q => q.id === questionId);
|
|
|
|
|
if (!question) return;
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
2023-11-15 18:38:02 +00:00
|
|
|
|
try {
|
2023-11-27 23:07:24 +00:00
|
|
|
|
const { updated: newQuestionId } = await questionApi.copy(question.backendId, quizId);
|
2023-11-15 18:38:02 +00:00
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
const copiedQuestion = structuredClone(question);
|
|
|
|
|
copiedQuestion.backendId = newQuestionId;
|
|
|
|
|
copiedQuestion.id = nanoid();
|
2023-11-15 18:38:02 +00:00
|
|
|
|
|
2023-11-27 23:07:24 +00:00
|
|
|
|
setProducedState(state => {
|
2023-11-16 16:41:25 +00:00
|
|
|
|
state.questions.push(copiedQuestion);
|
2023-11-15 18:38:02 +00:00
|
|
|
|
}, {
|
|
|
|
|
type: "copyQuestion",
|
2023-11-27 23:07:24 +00:00
|
|
|
|
questionId: questionId,
|
2023-11-15 18:38:02 +00:00
|
|
|
|
quizId,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
devlog("Error copying question", error);
|
|
|
|
|
enqueueSnackbar("Не удалось скопировать вопрос");
|
|
|
|
|
}
|
2023-11-27 23:07:24 +00:00
|
|
|
|
});
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
|
|
|
|
function setProducedState<A extends string | { type: unknown; }>(
|
|
|
|
|
recipe: (state: QuestionsStore) => void,
|
|
|
|
|
action?: A,
|
|
|
|
|
) {
|
|
|
|
|
useQuestionsStore.setState(state => produce(state, recipe), false, action);
|
|
|
|
|
}
|