frontPanel/src/stores/quizes.ts

98 lines
2.9 KiB
TypeScript
Raw Normal View History

import { create } from "zustand";
import { persist } from "zustand/middleware";
interface QuizStore {
listQuizes: {[key: number]: Quizes};
updateQuizesList: (newQuiz: Quizes) => void;
removeQuiz:(id: number) => void;
}
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,
config: 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
}
export const quizStore = create<QuizStore>()(
persist(
(set, get) => ({
listQuizes: {
// 1: {
// "id": 0,
// "qid": "string",
// "deleted": false,
// "archived": true,
// "fingerprinting": true,
// "repeatable": true,
// "note_prevented": true,
// "mail_notifications": true,
// "unique_answers": true,
// "name": "string",
// "description": "string",
// "config": "string",
// "status": "string",
// "limit": 0,
// "due_to": 0,
// "time_of_passing": 0,
// "pausable": true,
// "version": 0,
// "version_comment": "string",
// "created_at": "string",
// "updated_at": "string",
// "question_cnt": 0,
// "passed_count": 0,
// "average_time": 0,
// "super": true,
// "group_id": 0
// }
},
updateQuizesList: (newQuiz) => {
const state = get()["listQuizes"] || {};
const newState = {
...state,
[newQuiz["id"]]: newQuiz
};
set({listQuizes:newState});
},
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});
}
}),
{
name: "quizes",
}
)
);