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