2023-07-27 17:30:55 +00:00
|
|
|
import { create } from "zustand";
|
|
|
|
import { persist } from "zustand/middleware";
|
2023-07-20 22:02:20 +00:00
|
|
|
|
|
|
|
type Variants = {
|
2023-07-27 17:30:55 +00:00
|
|
|
answer: string;
|
|
|
|
answerLong: string;
|
|
|
|
hints: string;
|
|
|
|
};
|
2023-07-20 22:02:20 +00:00
|
|
|
|
|
|
|
interface Question {
|
2023-07-27 17:30:55 +00:00
|
|
|
id: number;
|
|
|
|
title: string;
|
|
|
|
description: string;
|
|
|
|
type: string;
|
2023-07-30 15:35:40 +00:00
|
|
|
required: boolean;
|
2023-07-27 17:30:55 +00:00
|
|
|
deleted: true;
|
|
|
|
page: number;
|
|
|
|
content: {
|
|
|
|
variants: Variants[];
|
|
|
|
own: boolean;
|
|
|
|
multi: boolean;
|
|
|
|
};
|
|
|
|
version: number;
|
|
|
|
parent_ids: number[];
|
|
|
|
created_at: string;
|
|
|
|
updated_at: string;
|
2023-07-20 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 23:32:43 +00:00
|
|
|
interface QuestionStore {
|
2023-07-30 15:48:41 +00:00
|
|
|
listQuestions: Question[];
|
2023-07-27 17:30:55 +00:00
|
|
|
openedModalSettings: string;
|
2023-06-28 23:32:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const questionStore = create<QuestionStore>()(
|
2023-07-30 15:48:41 +00:00
|
|
|
persist<QuestionStore>(
|
2023-07-27 17:30:55 +00:00
|
|
|
() => ({
|
|
|
|
listQuestions: [],
|
|
|
|
openedModalSettings: "",
|
|
|
|
}),
|
2023-06-28 23:32:43 +00:00
|
|
|
|
2023-07-27 17:30:55 +00:00
|
|
|
{
|
|
|
|
name: "question",
|
|
|
|
}
|
|
|
|
)
|
2023-07-11 10:43:04 +00:00
|
|
|
);
|
2023-07-30 15:35:40 +00:00
|
|
|
export const updateQuestionsList = (index: number, data: Partial<Question>) => {
|
2023-07-27 17:30:55 +00:00
|
|
|
const array = [...questionStore.getState()["listQuestions"]];
|
|
|
|
array.splice(index, 1, { ...array[index], ...data });
|
|
|
|
questionStore.setState({ listQuestions: array });
|
|
|
|
};
|
|
|
|
|
2023-07-30 15:35:40 +00:00
|
|
|
export const updateQuestionsListDragAndDrop = (updatedQuestions: Question[]) => {
|
2023-07-27 17:30:55 +00:00
|
|
|
questionStore.setState({ listQuestions: updatedQuestions });
|
|
|
|
};
|
2023-07-20 22:02:20 +00:00
|
|
|
|
|
|
|
export const createQuestion = (id: number) => {
|
2023-07-27 17:30:55 +00:00
|
|
|
const idQ = getRandom(1000000, 10000000);
|
|
|
|
const newData = [...questionStore.getState()["listQuestions"]]; //пересоздание массива
|
|
|
|
newData.push({
|
|
|
|
id: idQ,
|
|
|
|
title: "",
|
|
|
|
description: "",
|
|
|
|
type: "",
|
|
|
|
required: true,
|
|
|
|
deleted: true,
|
|
|
|
page: 0,
|
|
|
|
content: {
|
|
|
|
variants: [
|
|
|
|
{
|
|
|
|
answer: "",
|
|
|
|
answerLong: "",
|
|
|
|
hints: "",
|
2023-07-20 22:02:20 +00:00
|
|
|
},
|
2023-07-27 17:30:55 +00:00
|
|
|
],
|
|
|
|
own: true,
|
|
|
|
multi: true,
|
|
|
|
},
|
|
|
|
version: 0,
|
|
|
|
parent_ids: [0],
|
|
|
|
created_at: "",
|
|
|
|
updated_at: "",
|
|
|
|
});
|
|
|
|
questionStore.setState({ listQuestions: newData });
|
|
|
|
};
|
2023-07-20 22:02:20 +00:00
|
|
|
|
|
|
|
export const removeQuestion = (index: number) => {
|
2023-07-27 17:30:55 +00:00
|
|
|
const array = [...questionStore.getState()["listQuestions"]];
|
|
|
|
array.splice(index, 1);
|
|
|
|
questionStore.setState({ listQuestions: array });
|
|
|
|
};
|
2023-07-20 22:02:20 +00:00
|
|
|
|
2023-07-30 15:35:40 +00:00
|
|
|
export const resetSomeField = (data: Record<string, string>) => {
|
2023-07-27 17:30:55 +00:00
|
|
|
questionStore.setState(data);
|
|
|
|
};
|
2023-07-20 22:02:20 +00:00
|
|
|
|
2023-07-30 15:35:40 +00:00
|
|
|
export const findQuestionById = (id_question: number) => {
|
2023-07-27 17:30:55 +00:00
|
|
|
let found = null;
|
2023-07-30 15:35:40 +00:00
|
|
|
questionStore.getState()["listQuestions"].some((quiz: Question, index: number) => {
|
|
|
|
if (quiz.id === id_question) {
|
2023-07-27 17:30:55 +00:00
|
|
|
found = { quiz, index };
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return found;
|
|
|
|
};
|
2023-07-11 10:43:04 +00:00
|
|
|
|
|
|
|
function getRandom(min: number, max: number) {
|
2023-07-27 17:30:55 +00:00
|
|
|
min = Math.ceil(min);
|
|
|
|
max = Math.floor(max);
|
|
|
|
return Math.floor(Math.random() * (max - min)) + min;
|
|
|
|
}
|