fix: questions store
This commit is contained in:
parent
ddfc4d6e04
commit
015717cc0c
@ -12,20 +12,20 @@ import { DraggableList } from "./DraggableList";
|
||||
|
||||
export default function QuestionsPage() {
|
||||
const { listQuizes, updateQuizesList } = quizStore();
|
||||
const params = Number(useParams().quizId);
|
||||
const quizId = Number(useParams().quizId);
|
||||
const { listQuestions } = questionStore();
|
||||
const handleNext = () => {
|
||||
updateQuizesList(params, { step: listQuizes[params].step + 1 });
|
||||
updateQuizesList(quizId, { step: listQuizes[quizId].step + 1 });
|
||||
};
|
||||
|
||||
const handleBack = () => {
|
||||
let result = listQuizes[params].step - 1;
|
||||
updateQuizesList(params, { step: result ? result : 1 });
|
||||
let result = listQuizes[quizId].step - 1;
|
||||
updateQuizesList(quizId, { step: result ? result : 1 });
|
||||
};
|
||||
|
||||
const collapseEverything = () => {
|
||||
listQuestions.forEach((item, index) => {
|
||||
updateQuestionsList(index, { ...item, expanded: false });
|
||||
listQuestions[quizId].forEach((item, index) => {
|
||||
updateQuestionsList(quizId, { ...item, expanded: false });
|
||||
});
|
||||
};
|
||||
|
||||
@ -67,7 +67,7 @@ export default function QuestionsPage() {
|
||||
>
|
||||
<IconButton
|
||||
onClick={() => {
|
||||
createQuestion(params);
|
||||
createQuestion(quizId);
|
||||
console.log(listQuestions);
|
||||
}}
|
||||
>
|
||||
|
||||
@ -72,14 +72,14 @@ export interface Question {
|
||||
}
|
||||
|
||||
interface QuestionStore {
|
||||
listQuestions: Question[];
|
||||
listQuestions: Record<string, Question[]>;
|
||||
openedModalSettings: string;
|
||||
}
|
||||
|
||||
export const questionStore = create<QuestionStore>()(
|
||||
persist<QuestionStore>(
|
||||
() => ({
|
||||
listQuestions: [],
|
||||
listQuestions: {},
|
||||
openedModalSettings: "",
|
||||
}),
|
||||
|
||||
@ -88,31 +88,42 @@ export const questionStore = create<QuestionStore>()(
|
||||
}
|
||||
)
|
||||
);
|
||||
export const updateQuestionsList = (index: number, data: Partial<Question>) => {
|
||||
const array = [...questionStore.getState()["listQuestions"]];
|
||||
array.splice(index, 1, { ...array[index], ...data });
|
||||
questionStore.setState({ listQuestions: array });
|
||||
export const updateQuestionsList = (
|
||||
questionId: number,
|
||||
data: Partial<Question>
|
||||
) => {
|
||||
const questionListClone = { ...questionStore.getState()["listQuestions"] };
|
||||
questionListClone[questionId] = { ...questionListClone[questionId], ...data };
|
||||
questionStore.setState({ listQuestions: questionListClone });
|
||||
};
|
||||
|
||||
export const updateQuestionsListDragAndDrop = (
|
||||
questionId: number,
|
||||
updatedQuestions: Question[]
|
||||
) => {
|
||||
questionStore.setState({ listQuestions: updatedQuestions });
|
||||
const questionListClone = { ...questionStore.getState()["listQuestions"] };
|
||||
questionStore.setState({
|
||||
listQuestions: { ...questionListClone, [questionId]: updatedQuestions },
|
||||
});
|
||||
};
|
||||
|
||||
export const updateVariants = (index: number, variants: Variants[]) => {
|
||||
const listQuestions = [...questionStore.getState()["listQuestions"]];
|
||||
|
||||
listQuestions[index].content.variants = variants;
|
||||
export const updateVariants = (
|
||||
questionId: number,
|
||||
index: number,
|
||||
variants: Variants[]
|
||||
) => {
|
||||
const listQuestions = { ...questionStore.getState()["listQuestions"] };
|
||||
listQuestions[questionId][index].content.variants = variants;
|
||||
|
||||
questionStore.setState({ listQuestions });
|
||||
};
|
||||
|
||||
export const createQuestion = (id: number) => {
|
||||
const idQ = getRandom(1000000, 10000000);
|
||||
const newData = [...questionStore.getState()["listQuestions"]]; //пересоздание массива
|
||||
newData.push({
|
||||
id: idQ,
|
||||
export const createQuestion = (questionId: number) => {
|
||||
const id = getRandom(1000000, 10000000);
|
||||
const newData = { ...questionStore.getState()["listQuestions"] };
|
||||
|
||||
newData[questionId].push({
|
||||
id,
|
||||
title: "",
|
||||
description: "",
|
||||
type: "",
|
||||
@ -176,37 +187,41 @@ export const createQuestion = (id: number) => {
|
||||
updated_at: "",
|
||||
expanded: false,
|
||||
});
|
||||
|
||||
questionStore.setState({ listQuestions: newData });
|
||||
};
|
||||
|
||||
export const copyQuestion = (copiedQuestionIndex: number) => {
|
||||
const listQuestions = [...questionStore.getState()["listQuestions"]];
|
||||
export const copyQuestion = (
|
||||
questionId: number,
|
||||
copiedQuestionIndex: number
|
||||
) => {
|
||||
const listQuestions = { ...questionStore.getState()["listQuestions"] };
|
||||
|
||||
listQuestions.splice(
|
||||
copiedQuestionIndex,
|
||||
0,
|
||||
listQuestions[copiedQuestionIndex]
|
||||
);
|
||||
listQuestions[questionId].push({
|
||||
...listQuestions[questionId][copiedQuestionIndex],
|
||||
});
|
||||
|
||||
questionStore.setState({ listQuestions });
|
||||
};
|
||||
|
||||
export const removeQuestion = (index: number) => {
|
||||
const array = [...questionStore.getState()["listQuestions"]];
|
||||
array.splice(index, 1);
|
||||
questionStore.setState({ listQuestions: array });
|
||||
export const removeQuestion = (questionId: number, index: number) => {
|
||||
const questionListClone = { ...questionStore.getState()["listQuestions"] };
|
||||
|
||||
questionListClone[questionId].splice(index, 1);
|
||||
|
||||
questionStore.setState({ listQuestions: questionListClone });
|
||||
};
|
||||
|
||||
export const resetSomeField = (data: Record<string, string>) => {
|
||||
questionStore.setState(data);
|
||||
};
|
||||
|
||||
export const findQuestionById = (id_question: number) => {
|
||||
export const findQuestionById = (questionId: number) => {
|
||||
let found = null;
|
||||
questionStore
|
||||
.getState()
|
||||
["listQuestions"].some((quiz: Question, index: number) => {
|
||||
if (quiz.id === id_question) {
|
||||
["listQuestions"][questionId].some((quiz: Question, index: number) => {
|
||||
if (quiz.id === questionId) {
|
||||
found = { quiz, index };
|
||||
return true;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user