import { create } from "zustand"; import { persist } from "zustand/middleware"; export type Variants = { answer: string; answerLong: string; hints: string; }; type Hint = { text: string; video: string; }; type Rule = { or: boolean; show: boolean; reqs: [ { id: string; vars: number[]; } ]; }; export interface Question { id: number; title: string; description: string; type: string; required: boolean; deleted: true; page: number; content: { variants: Variants[]; hint: Hint; rule: Rule; large: boolean; multi: boolean; own: boolean; innerName: string; back: string; placeholder: string; type: string; autofill: boolean; default: string; }; version: number; parent_ids: number[]; created_at: string; updated_at: string; expanded: boolean; } interface QuestionStore { listQuestions: Question[]; openedModalSettings: string; } export const questionStore = create()( persist( () => ({ listQuestions: [], openedModalSettings: "", }), { name: "question", } ) ); export const updateQuestionsList = (index: number, data: Partial) => { const array = [...questionStore.getState()["listQuestions"]]; array.splice(index, 1, { ...array[index], ...data }); questionStore.setState({ listQuestions: array }); }; export const updateQuestionsListDragAndDrop = ( updatedQuestions: Question[] ) => { questionStore.setState({ listQuestions: updatedQuestions }); }; 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: { large: false, multi: false, own: false, innerName: "", back: "", placeholder: "", type: "single", autofill: true, default: "", variants: [ { answer: "", answerLong: "", hints: "", }, ], hint: { text: "", video: "", }, rule: { or: true, show: true, reqs: [ { id: "", vars: [], }, ], }, }, version: 0, parent_ids: [0], created_at: "", updated_at: "", expanded: false, }); questionStore.setState({ listQuestions: newData }); }; 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 }); }; export const resetSomeField = (data: Record) => { questionStore.setState(data); }; export const findQuestionById = (id_question: number) => { let found = null; 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; }