стор квизов и его HC функции

This commit is contained in:
krokodilka 2023-05-14 17:25:07 +03:00
parent e2863bfee8
commit d6570ccae9
2 changed files with 67 additions and 21 deletions

@ -10,8 +10,8 @@ interface Props {
} }
export default function MyQuizzesFull({outerContainerSx: sx, children}: Props) { export default function MyQuizzesFull({outerContainerSx: sx, children}: Props) {
const store = quizStore.getState() const {listQuizes, updateQuizesList, removeQuiz} = quizStore()
console.log(Object.values(store))
return ( return (
<SectionWrapper <SectionWrapper
maxWidth="lg" maxWidth="lg"
@ -20,6 +20,43 @@ export default function MyQuizzesFull({outerContainerSx: sx, children}: Props) {
mb: "70px", mb: "70px",
}} }}
> >
<Button onClick={() => {
removeQuiz(2)
console.log(Object.values(listQuizes))
}}>УДАЛИ МЕНЯ</Button>
<Button onClick={() => {
console.log(Object.values(listQuizes))
}}>Узнай МЕНЯ</Button>
<Button onClick={() => {
updateQuizesList({
"id": 2,
"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
})
}}>добавь МЕНЯ</Button>
<ComplexNavText text1="Кабинет квизов"/> <ComplexNavText text1="Кабинет квизов"/>
<Box <Box
sx={{ sx={{
@ -44,7 +81,7 @@ export default function MyQuizzesFull({outerContainerSx: sx, children}: Props) {
mb: "60px", mb: "60px",
}} }}
> >
{Object.values(store).map((e, i) =>( {Object.values(listQuizes).map((e, i) =>(
<QuizCard name={e.name} openCount={0} applicationCount={0} conversionPercent={0}/> <QuizCard name={e.name} openCount={0} applicationCount={0} conversionPercent={0}/>
) )
)} )}

@ -2,8 +2,9 @@ import { create } from "zustand";
import { persist } from "zustand/middleware"; import { persist } from "zustand/middleware";
interface QuizStore { interface QuizStore {
[key: number]: Quizes; listQuizes: {[key: number]: Quizes};
updateQuizesList: (newQuiz: Quizes) => void;
removeQuiz:(id: number) => void;
} }
interface Quizes { interface Quizes {
@ -15,7 +16,7 @@ interface Quizes {
repeatable: boolean, repeatable: boolean,
note_prevented: boolean, note_prevented: boolean,
mail_notifications: boolean, mail_notifications: boolean,
"unique_answers": boolean, unique_answers: boolean,
name: string, name: string,
description: string, description: string,
config: string, config: string,
@ -38,7 +39,8 @@ interface Quizes {
export const quizStore = create<QuizStore>()( export const quizStore = create<QuizStore>()(
persist( persist(
(set, get) => ({ (set, get) => ({
1: { listQuizes: {
1: {
"id": 0, "id": 0,
"qid": "string", "qid": "string",
"deleted": false, "deleted": false,
@ -66,22 +68,29 @@ export const quizStore = create<QuizStore>()(
"super": true, "super": true,
"group_id": 0 "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", name: "quizes",
} }
) )
); );
// export const Update = (newQuiz: Quizes) => {
//
// const state = quizStore.getState();
// if (typeof state[newQuiz.id] !== "undefined") {
// //ключ есть
// }else{
// quizStore.setState(state => {newQuiz.id: newQuiz})
// }
// quizStore.setState({
//
// });
// }