frontPanel/src/stores/questions.ts

154 lines
3.3 KiB
TypeScript
Raw Normal View History

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 = {
answer: string;
answerLong: string;
hints: string;
};
2023-08-24 11:09:47 +00:00
type Hint = {
text: string;
video: string;
videoUrl: string;
};
export interface Question {
id: number;
2023-08-10 13:45:44 +00:00
title: string;
description: string;
type: string;
required: boolean;
deleted: true;
2023-08-10 13:45:44 +00:00
page: number;
content: {
variants: Variants[];
2023-08-24 11:09:47 +00:00
hint: Hint;
2023-08-24 09:09:43 +00:00
large: boolean;
multi: boolean;
2023-08-24 09:09:43 +00:00
own: boolean;
innerName: string;
};
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;
}
interface QuestionStore {
2023-07-30 15:48:41 +00:00
listQuestions: Question[];
openedModalSettings: string;
}
export const questionStore = create<QuestionStore>()(
2023-07-30 15:48:41 +00:00
persist<QuestionStore>(
() => ({
listQuestions: [],
openedModalSettings: "",
2023-08-10 13:45:44 +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>) => {
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[]
) => {
questionStore.setState({ listQuestions: updatedQuestions });
};
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 });
};
export const createQuestion = (id: number) => {
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-08-24 11:09:47 +00:00
hint: {
text: "",
video: "",
videoUrl: "",
},
2023-08-24 09:09:43 +00:00
large: false,
multi: false,
own: false,
innerName: "",
},
version: 0,
parent_ids: [0],
created_at: "",
updated_at: "",
2023-08-24 09:09:43 +00:00
expanded: false,
});
questionStore.setState({ listQuestions: newData });
};
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 });
};
export const removeQuestion = (index: number) => {
const array = [...questionStore.getState()["listQuestions"]];
array.splice(index, 1);
questionStore.setState({ listQuestions: array });
};
2023-07-30 15:35:40 +00:00
export const resetSomeField = (data: Record<string, string>) => {
questionStore.setState(data);
};
2023-07-30 15:35:40 +00:00
export const findQuestionById = (id_question: number) => {
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;
}
});
return found;
};
function getRandom(min: number, max: number) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}