2024-01-30 16:49:33 +00:00
|
|
|
import { QuestionVariant } from "@model/questionTypes/shared";
|
|
|
|
import { produce } from "immer";
|
|
|
|
import { nanoid } from "nanoid";
|
2023-12-16 14:55:56 +00:00
|
|
|
import { create } from "zustand";
|
|
|
|
import { devtools } from "zustand/middleware";
|
2024-01-22 10:26:11 +00:00
|
|
|
import type { Moment } from "moment";
|
2024-02-08 13:42:31 +00:00
|
|
|
import { QuizStep } from "@model/settingsData";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2024-02-10 16:25:06 +00:00
|
|
|
type QuestionAnswer = {
|
2024-01-30 16:49:33 +00:00
|
|
|
questionId: string;
|
2024-01-31 12:57:07 +00:00
|
|
|
answer: string | string[] | Moment;
|
2023-12-16 14:55:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type OwnVariant = {
|
2024-01-30 16:49:33 +00:00
|
|
|
id: string;
|
|
|
|
variant: QuestionVariant;
|
2023-12-16 14:55:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
interface QuizViewStore {
|
2024-02-10 16:25:06 +00:00
|
|
|
answers: QuestionAnswer[];
|
2024-01-30 16:49:33 +00:00
|
|
|
ownVariants: OwnVariant[];
|
2024-02-11 18:04:30 +00:00
|
|
|
pointsSum: number;
|
|
|
|
points: Record<string, number>;
|
2024-02-08 13:42:31 +00:00
|
|
|
currentQuizStep: QuizStep;
|
2023-12-16 14:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const useQuizViewStore = create<QuizViewStore>()(
|
2024-01-30 16:49:33 +00:00
|
|
|
devtools(
|
|
|
|
(set, get) => ({
|
|
|
|
answers: [],
|
|
|
|
ownVariants: [],
|
2024-02-11 18:04:30 +00:00
|
|
|
points: {},
|
|
|
|
pointsSum: 0,
|
2024-02-08 13:42:31 +00:00
|
|
|
currentQuizStep: "startpage",
|
2024-01-30 16:49:33 +00:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
name: "quizView",
|
|
|
|
enabled: import.meta.env.DEV,
|
|
|
|
trace: import.meta.env.DEV,
|
|
|
|
}
|
|
|
|
)
|
2023-12-16 14:55:56 +00:00
|
|
|
);
|
|
|
|
|
2024-01-30 16:49:33 +00:00
|
|
|
function setProducedState<A extends string | { type: string; }>(
|
|
|
|
recipe: (state: QuizViewStore) => void,
|
2024-02-11 18:04:30 +00:00
|
|
|
action?: A,
|
2024-01-30 16:49:33 +00:00
|
|
|
) {
|
|
|
|
useQuizViewStore.setState(state => produce(state, recipe), false, action);
|
|
|
|
}
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2024-02-11 18:04:30 +00:00
|
|
|
const calcPoints = () => {
|
|
|
|
const storePoints = useQuizViewStore.getState().points;
|
|
|
|
let sum = Object.values(storePoints).reduce((accumulator, currentValue) => accumulator + currentValue)
|
|
|
|
console.log("сумма ", sum)
|
|
|
|
useQuizViewStore.setState({ pointsSum: sum })
|
|
|
|
}
|
|
|
|
|
2024-01-22 10:26:11 +00:00
|
|
|
export const updateAnswer = (
|
2024-02-08 13:42:31 +00:00
|
|
|
questionId: string,
|
2024-02-11 18:04:30 +00:00
|
|
|
answer: string | string[] | Moment,
|
|
|
|
points: number
|
|
|
|
) => {
|
|
|
|
setProducedState(state => {
|
|
|
|
const index = state.answers.findIndex(answer => questionId === answer.questionId);
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2024-02-11 18:04:30 +00:00
|
|
|
if (index < 0) {
|
|
|
|
state.answers.push({ questionId, answer });
|
|
|
|
} else {
|
|
|
|
state.answers[index] = { questionId, answer };
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
type: "updateAnswer",
|
|
|
|
questionId,
|
|
|
|
answer
|
|
|
|
})
|
|
|
|
const storePoints = useQuizViewStore.getState().points;
|
|
|
|
useQuizViewStore.setState({ points: { ...storePoints, ...{ [questionId]: points } } })
|
|
|
|
calcPoints()
|
|
|
|
};
|
2024-01-30 16:49:33 +00:00
|
|
|
|
|
|
|
export const deleteAnswer = (questionId: string) => useQuizViewStore.setState(state => ({
|
|
|
|
answers: state.answers.filter(answer => questionId !== answer.questionId)
|
|
|
|
}), false, {
|
|
|
|
type: "deleteAnswer",
|
|
|
|
questionId
|
|
|
|
});
|
|
|
|
|
|
|
|
export const updateOwnVariant = (id: string, answer: string) => setProducedState(state => {
|
|
|
|
const index = state.ownVariants.findIndex((variant) => variant.id === id);
|
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
state.ownVariants.push({
|
|
|
|
id,
|
|
|
|
variant: {
|
|
|
|
id: nanoid(),
|
|
|
|
answer,
|
|
|
|
extendedText: "",
|
|
|
|
hints: "",
|
|
|
|
originalImageUrl: "",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
state.ownVariants[index].variant.answer = answer;
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
type: "updateOwnVariant",
|
|
|
|
id,
|
|
|
|
answer
|
|
|
|
});
|
|
|
|
|
|
|
|
export const deleteOwnVariant = (id: string) => useQuizViewStore.setState(state => ({
|
|
|
|
ownVariants: state.ownVariants.filter((variant) => variant.id !== id)
|
|
|
|
}), false, {
|
|
|
|
type: "deleteOwnVariant",
|
|
|
|
id
|
2024-02-08 13:42:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export const setCurrentQuizStep = (currentQuizStep: QuizStep) => useQuizViewStore.setState({
|
|
|
|
currentQuizStep
|
|
|
|
}, false, {
|
|
|
|
type: "setCurrentQuizStep",
|
|
|
|
currentQuizStep
|
|
|
|
});
|