2023-08-10 13:45:44 +00:00
|
|
|
import { create } from "zustand";
|
|
|
|
import { persist } from "zustand/middleware";
|
|
|
|
|
2023-07-30 16:02:41 +00:00
|
|
|
export type Variants = {
|
2023-07-27 17:30:55 +00:00
|
|
|
answer: string;
|
|
|
|
answerLong: string;
|
|
|
|
hints: string;
|
|
|
|
};
|
2023-07-20 22:02:20 +00:00
|
|
|
|
2023-08-24 11:09:47 +00:00
|
|
|
type Hint = {
|
|
|
|
text: string;
|
|
|
|
video: string;
|
|
|
|
};
|
|
|
|
|
2023-08-24 20:53:27 +00:00
|
|
|
type Rule = {
|
|
|
|
or: boolean;
|
|
|
|
show: boolean;
|
|
|
|
reqs: [
|
|
|
|
{
|
|
|
|
id: string;
|
|
|
|
vars: number[];
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2023-07-30 21:25:47 +00:00
|
|
|
export interface Question {
|
2023-07-27 17:30:55 +00:00
|
|
|
id: number;
|
2023-08-10 13:45:44 +00:00
|
|
|
title: string;
|
|
|
|
description: string;
|
|
|
|
type: string;
|
|
|
|
required: boolean;
|
2023-07-27 17:30:55 +00:00
|
|
|
deleted: true;
|
2023-08-10 13:45:44 +00:00
|
|
|
page: number;
|
2023-07-27 17:30:55 +00:00
|
|
|
content: {
|
|
|
|
variants: Variants[];
|
2023-08-24 11:09:47 +00:00
|
|
|
hint: Hint;
|
2023-08-24 20:53:27 +00:00
|
|
|
rule: Rule;
|
2023-08-24 09:09:43 +00:00
|
|
|
large: boolean;
|
2023-07-27 17:30:55 +00:00
|
|
|
multi: boolean;
|
2023-08-24 09:09:43 +00:00
|
|
|
own: boolean;
|
|
|
|
innerName: string;
|
2023-08-24 11:57:42 +00:00
|
|
|
back: string;
|
2023-07-27 17:30:55 +00:00
|
|
|
};
|
2023-08-10 13:45:44 +00:00
|
|
|
version: number;
|
|
|
|
parent_ids: number[];
|
|
|
|
created_at: string;
|
|
|
|
updated_at: string;
|
2023-08-24 09:09:43 +00:00
|
|
|
expanded: boolean;
|
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-08-10 13:45:44 +00:00
|
|
|
}),
|
2023-06-30 14:39:07 +00:00
|
|
|
|
2023-08-10 13:45:44 +00:00
|
|
|
{
|
|
|
|
name: "question",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
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-08-11 06:15:04 +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
|
|
|
|
2023-08-18 11:16:56 +00:00
|
|
|
export const updateVariants = (index: number, variants: Variants[]) => {
|
|
|
|
const listQuestions = [...questionStore.getState()["listQuestions"]];
|
|
|
|
|
|
|
|
listQuestions[index].content.variants = variants;
|
|
|
|
|
|
|
|
questionStore.setState({ listQuestions });
|
|
|
|
};
|
|
|
|
|
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: {
|
2023-08-24 20:53:27 +00:00
|
|
|
large: false,
|
|
|
|
multi: false,
|
|
|
|
own: false,
|
|
|
|
innerName: "",
|
|
|
|
back: "",
|
2023-07-27 17:30:55 +00:00
|
|
|
variants: [
|
|
|
|
{
|
|
|
|
answer: "",
|
|
|
|
answerLong: "",
|
|
|
|
hints: "",
|
2023-07-20 22:02:20 +00:00
|
|
|
},
|
2023-07-27 17:30:55 +00:00
|
|
|
],
|
2023-08-24 11:09:47 +00:00
|
|
|
hint: {
|
|
|
|
text: "",
|
|
|
|
video: "",
|
|
|
|
},
|
2023-08-24 20:53:27 +00:00
|
|
|
rule: {
|
|
|
|
or: true,
|
|
|
|
show: true,
|
|
|
|
reqs: [
|
|
|
|
{
|
|
|
|
id: "",
|
|
|
|
vars: [],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2023-07-27 17:30:55 +00:00
|
|
|
},
|
|
|
|
version: 0,
|
|
|
|
parent_ids: [0],
|
|
|
|
created_at: "",
|
|
|
|
updated_at: "",
|
2023-08-24 09:09:43 +00:00
|
|
|
expanded: false,
|
2023-07-27 17:30:55 +00:00
|
|
|
});
|
|
|
|
questionStore.setState({ listQuestions: newData });
|
|
|
|
};
|
2023-07-20 22:02:20 +00:00
|
|
|
|
2023-08-11 07:25:28 +00:00
|
|
|
export const copyQuestion = (copiedQuestionIndex: number) => {
|
|
|
|
const listQuestions = [...questionStore.getState()["listQuestions"]];
|
|
|
|
|
|
|
|
listQuestions.splice(
|
|
|
|
copiedQuestionIndex,
|
|
|
|
0,
|
|
|
|
listQuestions[copiedQuestionIndex]
|
|
|
|
);
|
|
|
|
|
|
|
|
questionStore.setState({ listQuestions });
|
|
|
|
};
|
|
|
|
|
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-08-11 06:15:04 +00:00
|
|
|
questionStore
|
|
|
|
.getState()
|
|
|
|
["listQuestions"].some((quiz: Question, index: number) => {
|
|
|
|
if (quiz.id === id_question) {
|
|
|
|
found = { quiz, index };
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2023-07-27 17:30:55 +00:00
|
|
|
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;
|
|
|
|
}
|