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