fix order of created question

This commit is contained in:
nflnkr 2023-12-12 17:47:47 +03:00
parent e58df2173e
commit dfe85edc67
2 changed files with 17 additions and 6 deletions

@ -387,7 +387,7 @@ console.log(question.type)
}}
>
<Box
onClick={() => createUntypedQuestion(question.quizId)}
onClick={() => createUntypedQuestion(question.quizId, question.id)}
sx={{
display: plusVisible && !isDragging ? "flex" : "none",
width: "100%",

@ -26,8 +26,8 @@ export const setQuestions = (questions: RawQuestion[] | null) => setProducedStat
questions,
});
export const createUntypedQuestion = (quizId: number) => setProducedState(state => {
state.questions.push({
export const createUntypedQuestion = (quizId: number, insertAfterQuestionId?: string) => setProducedState(state => {
const newUntypedQuestion = {
id: nanoid(),
quizId,
type: null,
@ -35,7 +35,16 @@ export const createUntypedQuestion = (quizId: number) => setProducedState(state
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,
@ -335,6 +344,8 @@ export const createTypedQuestion = async (
type: "createTypedQuestion",
question,
});
updateQuestionOrders();
} catch (error) {
devlog("Error creating question", error);
enqueueSnackbar("Не удалось создать вопрос");
@ -355,7 +366,7 @@ export const deleteQuestion = async (questionId: string) => requestQueue.enqueue
try {
await questionApi.delete(question.backendId);
removeQuestion(questionId);
updateQuestionOrders();
@ -521,7 +532,7 @@ export const createBackResult = async (
rawQuestionToQuestion(createdQuestion)
);
}, {
type: "createTypedQuestion",
type: "createBackResult",
question,
});
} catch (error) {