161 lines
5.3 KiB
TypeScript
161 lines
5.3 KiB
TypeScript
import {create} from "zustand";
|
||
import {persist} from "zustand/middleware";
|
||
|
||
interface QuizStore {
|
||
listQuizes: { [key: number]: Quizes };
|
||
updateQuizesList: (id: number, values: unknown) => void;
|
||
removeQuiz: (id: number) => void;
|
||
createBlank: () => void;
|
||
}
|
||
|
||
export interface Quizes {
|
||
id: number,
|
||
qid: string,
|
||
deleted: boolean,
|
||
archived: boolean,
|
||
fingerprinting: boolean,
|
||
repeatable: boolean,
|
||
note_prevented: boolean,
|
||
mail_notifications: boolean,
|
||
unique_answers: boolean,
|
||
name: string,
|
||
description: string,
|
||
status: string,
|
||
limit: number,
|
||
due_to: number,
|
||
time_of_passing: number,
|
||
pausable: boolean,
|
||
version: number,
|
||
version_comment: string,
|
||
created_at: string,
|
||
updated_at: string,
|
||
question_cnt: number,
|
||
passed_count: number,
|
||
average_time: number,
|
||
super: true,
|
||
group_id: number,
|
||
step: number,
|
||
startpage: string,
|
||
config: {
|
||
type: string,
|
||
logo: string,
|
||
noStartPage: boolean,
|
||
startpage: {
|
||
description: string,
|
||
button: string,
|
||
position: string,
|
||
background: {
|
||
type: string,
|
||
desktop: string,
|
||
mobile: string,
|
||
video: string,
|
||
cycle: boolean
|
||
},
|
||
},
|
||
info: {
|
||
phonenumber: string,
|
||
clickable: boolean,
|
||
orgname: string,
|
||
site: string,
|
||
law?: string
|
||
},
|
||
meta: string,
|
||
}
|
||
}
|
||
|
||
export const quizStore = create<QuizStore>()(
|
||
persist(
|
||
(set, get) => ({
|
||
listQuizes: {
|
||
},
|
||
updateQuizesList: (id: number, values: any) => {
|
||
const state = get()["listQuizes"] || {};
|
||
state[id] = {
|
||
...state[id],
|
||
...values
|
||
};
|
||
set({listQuizes: state});
|
||
},
|
||
removeQuiz: (id) => {
|
||
const state = get()["listQuizes"] || {};
|
||
|
||
const newState = Object.entries(state).reduce<any>((accumulator, [key, value], index, array) => {
|
||
if (key !== id.toString()) {
|
||
accumulator[key] = value;
|
||
}
|
||
return accumulator;
|
||
}, {});
|
||
set({listQuizes: newState});
|
||
},
|
||
createBlank: () => {
|
||
const id = getRandom(1000000, 10000000)
|
||
const newListQuizes = get()["listQuizes"] || {};
|
||
newListQuizes[id] = {
|
||
"id": id,
|
||
"qid": "",
|
||
"deleted": false,
|
||
"archived": true,
|
||
"fingerprinting": true,
|
||
"repeatable": true,
|
||
"note_prevented": true,
|
||
"mail_notifications": true,
|
||
"unique_answers": true,
|
||
"name": "",
|
||
"description": "",
|
||
"status": "",
|
||
"limit": 0,
|
||
"due_to": 0,
|
||
"time_of_passing": 0,
|
||
"pausable": true,
|
||
"version": 0,
|
||
"version_comment": "",
|
||
"created_at": "",
|
||
"updated_at": "",
|
||
"question_cnt": 0,
|
||
"passed_count": 0,
|
||
"average_time": 0,
|
||
"super": true,
|
||
"group_id": 0,
|
||
"step": 1,
|
||
"startpage": "",
|
||
"config": {
|
||
"noStartPage": false,
|
||
"type": "", // quiz или form
|
||
"logo": "hub.pena.digital/img/logo",
|
||
"startpage": {
|
||
"description": "",// приветственный текст опроса
|
||
"button": "Начать", // текст на кнопке начала опроса
|
||
"position": "ltr", // ltr или rtl. отображение элементов поверх фона
|
||
"background": {
|
||
"type": "image", //image или video
|
||
"desktop": "hub.pena.digital/img/back/full.png",
|
||
"mobile": "hub.pena.digital/img/back/mobile.png",
|
||
"video":"hub.pena.digital/vid/back/vi1.mp4",
|
||
"cycle": true //зацикливать видео или нет
|
||
},
|
||
},
|
||
"info": {
|
||
"phonenumber": "+79885895677",
|
||
"clickable": true,
|
||
"orgname": "ООО \"Пена\"",
|
||
"site": "hub.pena.digital",
|
||
"law": "юридическая информация"
|
||
},
|
||
"meta": "что-то"
|
||
}
|
||
};
|
||
set({listQuizes: newListQuizes});
|
||
return id;
|
||
},
|
||
}),
|
||
{
|
||
name: "quizes",
|
||
}
|
||
)
|
||
);
|
||
|
||
function getRandom(min: number, max: number) {
|
||
min = Math.ceil(min);
|
||
max = Math.floor(max);
|
||
return Math.floor(Math.random() * (max - min)) + min;
|
||
} |