refactor questions store
This commit is contained in:
parent
f3eed84b42
commit
a57de5fe4d
@ -1,32 +1,21 @@
|
|||||||
import { questionApi } from "@api/question";
|
import { questionApi } from "@api/question";
|
||||||
import { devlog } from "@frontend/kitui";
|
import { devlog } from "@frontend/kitui";
|
||||||
|
import { Question } from "@model/question/question";
|
||||||
import { produce } from "immer";
|
import { produce } from "immer";
|
||||||
import { Question } from "model/question/question";
|
|
||||||
import { enqueueSnackbar } from "notistack";
|
import { enqueueSnackbar } from "notistack";
|
||||||
import { isAxiosCanceledError } from "utils/isAxiosCanceledError";
|
import { isAxiosCanceledError } from "../../utils/isAxiosCanceledError";
|
||||||
import { create } from "zustand";
|
import { QuestionsStore, useQuestionsStore } from "./store";
|
||||||
import { devtools } from "zustand/middleware";
|
|
||||||
|
|
||||||
|
|
||||||
type QuestionsStore = {
|
export const setQuestions = (quizes: Question[] | null) => setProducedState(state => {
|
||||||
questionsById: Record<number, Question | undefined>;
|
state.questionsById = {};
|
||||||
};
|
if (quizes === null) return;
|
||||||
|
|
||||||
const initialState: QuestionsStore = {
|
quizes.forEach(question => state.questionsById[question.id] = question);
|
||||||
questionsById: {},
|
}, {
|
||||||
};
|
type: "setQuizes",
|
||||||
|
quizes,
|
||||||
export const useQuestionsStore = create<QuestionsStore>()(
|
});
|
||||||
devtools(
|
|
||||||
() => initialState,
|
|
||||||
{
|
|
||||||
name: "QuestionsStore",
|
|
||||||
enabled: process.env.NODE_ENV === "development",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
export const setQuestions = (questions: QuestionsStore["questionsById"]) => useQuestionsStore.setState({ questionsById: questions });
|
|
||||||
|
|
||||||
export const setQuestion = (question: Question) => setProducedState(state => {
|
export const setQuestion = (question: Question) => setProducedState(state => {
|
||||||
state.questionsById[question.id] = question;
|
state.questionsById[question.id] = question;
|
||||||
@ -72,7 +61,6 @@ export const setQuestionFieldOptimistic = async <T extends keyof Question>(
|
|||||||
setQuestion(currentUpdatedQuestion);
|
setQuestion(currentUpdatedQuestion);
|
||||||
try {
|
try {
|
||||||
const { updated } = await questionApi.edit(currentUpdatedQuestion, controller.signal);
|
const { updated } = await questionApi.edit(currentUpdatedQuestion, controller.signal);
|
||||||
// await new Promise((resolve, reject) => setTimeout(reject, 2000, new Error("Api rejected")));
|
|
||||||
|
|
||||||
setQuestionField(question.id, "version", updated);
|
setQuestionField(question.id, "version", updated);
|
||||||
controller = null;
|
controller = null;
|
||||||
@ -107,9 +95,11 @@ export const updateQuestionWithFn = (
|
|||||||
updateFn: updateFn.toString(),
|
updateFn: updateFn.toString(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const createQuestion = async () => {
|
export const createQuestion = async (quizId: number) => {
|
||||||
try {
|
try {
|
||||||
const question = await questionApi.create();
|
const question = await questionApi.create({
|
||||||
|
quiz_id: quizId,
|
||||||
|
});
|
||||||
|
|
||||||
setQuestion(question);
|
setQuestion(question);
|
||||||
} catch (error) {
|
} catch (error) {
|
8
src/stores/questions/hooks.ts
Normal file
8
src/stores/questions/hooks.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import { useQuestionsStore } from "./store";
|
||||||
|
|
||||||
|
|
||||||
|
export function useQuestionArray() {
|
||||||
|
const questions = useQuestionsStore(state => state.questionsById);
|
||||||
|
|
||||||
|
return Object.values(questions).flatMap(question => question ? [question] : []);
|
||||||
|
}
|
22
src/stores/questions/store.ts
Normal file
22
src/stores/questions/store.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { Question } from "@model/question/question";
|
||||||
|
import { create } from "zustand";
|
||||||
|
import { devtools } from "zustand/middleware";
|
||||||
|
|
||||||
|
|
||||||
|
export type QuestionsStore = {
|
||||||
|
questionsById: Record<number, Question | undefined>;
|
||||||
|
};
|
||||||
|
|
||||||
|
const initialState: QuestionsStore = {
|
||||||
|
questionsById: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useQuestionsStore = create<QuestionsStore>()(
|
||||||
|
devtools(
|
||||||
|
() => initialState,
|
||||||
|
{
|
||||||
|
name: "QuestionsStore",
|
||||||
|
enabled: process.env.NODE_ENV === "development",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
Loading…
Reference in New Issue
Block a user