2023-06-28 23:32:43 +00:00
|
|
|
import {create} from "zustand";
|
|
|
|
import {persist} from "zustand/middleware";
|
|
|
|
|
|
|
|
interface QuestionStore {
|
2023-06-30 14:39:07 +00:00
|
|
|
listQuestions: any;
|
|
|
|
updateQuestionsList: (id: number, index: number, values: unknown) => void;
|
|
|
|
removeQuestion: any;
|
|
|
|
createQuestion: (id: number) => void;
|
2023-07-14 11:43:28 +00:00
|
|
|
openedModalSettings: any
|
2023-06-28 23:32:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const questionStore = create<QuestionStore>()(
|
|
|
|
|
|
|
|
persist(
|
|
|
|
(set, get) => ({
|
|
|
|
listQuestions: {
|
|
|
|
},
|
2023-06-30 14:39:07 +00:00
|
|
|
updateQuestionsList: (id: number, index: number, values: any) => {
|
|
|
|
const array = get()["listQuestions"][id] || [];
|
|
|
|
array[index] = {
|
|
|
|
...array[index],
|
2023-06-28 23:32:43 +00:00
|
|
|
...values
|
|
|
|
};
|
2023-06-30 14:39:07 +00:00
|
|
|
const state = get()["listQuestions"] || {};
|
|
|
|
state[id] = array
|
2023-06-28 23:32:43 +00:00
|
|
|
set({listQuestions: state});
|
|
|
|
},
|
|
|
|
|
2023-06-30 14:39:07 +00:00
|
|
|
createQuestion:(id: number) => {
|
2023-07-11 10:43:04 +00:00
|
|
|
const idQ = getRandom(1000000, 10000000)
|
2023-06-30 14:39:07 +00:00
|
|
|
const array = get()["listQuestions"][id] || [];
|
|
|
|
array.push(
|
|
|
|
{
|
2023-07-11 10:43:04 +00:00
|
|
|
"id": idQ,
|
2023-06-30 14:39:07 +00:00
|
|
|
"title": "",
|
|
|
|
"description": "",
|
|
|
|
"type": "",
|
2023-06-28 23:32:43 +00:00
|
|
|
"required": true,
|
|
|
|
"deleted": true,
|
|
|
|
"page": 0,
|
2023-07-11 10:43:04 +00:00
|
|
|
"content": {
|
|
|
|
variants: [
|
2023-07-14 11:43:28 +00:00
|
|
|
{
|
|
|
|
answer: "",
|
|
|
|
answerLong: "",
|
|
|
|
hints: ""
|
|
|
|
}
|
2023-07-11 10:43:04 +00:00
|
|
|
],
|
|
|
|
own: true,
|
|
|
|
multi: true
|
|
|
|
},
|
2023-06-28 23:32:43 +00:00
|
|
|
"version": 0,
|
|
|
|
"parent_ids": [
|
|
|
|
0
|
|
|
|
],
|
|
|
|
"created_at": "",
|
|
|
|
"updated_at": ""
|
2023-06-30 14:39:07 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
const state = get()["listQuestions"] || {};
|
|
|
|
state[id] = array
|
|
|
|
set({listQuestions: state});
|
|
|
|
},
|
|
|
|
|
|
|
|
removeQuestion: (id:number, index: number) => {
|
|
|
|
const array = get()["listQuestions"][id] || [];
|
|
|
|
array.splice(index, 1)
|
|
|
|
const state = get()["listQuestions"] || {};
|
|
|
|
state[id] = array
|
|
|
|
set({listQuestions: state});
|
|
|
|
},
|
2023-07-14 11:43:28 +00:00
|
|
|
|
|
|
|
openedModalSettings: [],
|
|
|
|
|
|
|
|
createOpenedModalSettings: (id: number) => {
|
|
|
|
const state = get()["openedModalSettings"] || [];
|
|
|
|
state.push(String(id))
|
|
|
|
set({openedModalSettings: state})
|
|
|
|
}
|
2023-06-28 23:32:43 +00:00
|
|
|
}),
|
2023-06-30 14:39:07 +00:00
|
|
|
|
2023-06-28 23:32:43 +00:00
|
|
|
{
|
|
|
|
name: "question",
|
|
|
|
}
|
|
|
|
)
|
2023-07-11 10:43:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
function getRandom(min: number, max: number) {
|
|
|
|
min = Math.ceil(min);
|
|
|
|
max = Math.floor(max);
|
|
|
|
return Math.floor(Math.random() * (max - min)) + min;
|
|
|
|
}
|