fix questions store type errors

This commit is contained in:
nflnkr 2023-12-03 16:09:10 +03:00
parent 525977609d
commit 30e9dec88b
2 changed files with 28 additions and 25 deletions

@ -123,7 +123,7 @@ export const updateQuestion = (
updateFn: (question: AnyTypedQuizQuestion) => void, updateFn: (question: AnyTypedQuizQuestion) => void,
) => { ) => {
setProducedState(state => { setProducedState(state => {
const question = state.questions.find(q => q.id === questionId) || state.questions.find(q => q.content.id === questionId); const question = state.questions.find(q => q.id === questionId) || state.questions.find(q => q.type !== null && q.content.id === questionId);
if (!question) return; if (!question) return;
if (question.type === null) throw new Error("Cannot update untyped question, use 'updateUntypedQuestion' instead"); if (question.type === null) throw new Error("Cannot update untyped question, use 'updateUntypedQuestion' instead");
@ -136,22 +136,22 @@ export const updateQuestion = (
// clearTimeout(requestTimeoutId); // clearTimeout(requestTimeoutId);
// requestTimeoutId = setTimeout(() => { // requestTimeoutId = setTimeout(() => {
requestQueue.enqueue(async () => { requestQueue.enqueue(async () => {
const q = useQuestionsStore.getState().questions.find(q => q.id === questionId) || useQuestionsStore.getState().questions.find(q => q.content.id === questionId); const q = useQuestionsStore.getState().questions.find(q => q.id === questionId) || useQuestionsStore.getState().questions.find(q => q.type !== null && q.content.id === questionId);
console.log("мы пытаемся найти вопрос ") console.log("мы пытаемся найти вопрос ");
if (!q) return; if (!q) return;
if (q.type === null) throw new Error("Cannot send update request for untyped question"); if (q.type === null) throw new Error("Cannot send update request for untyped question");
console.log(q.title) console.log(q.title);
const response = await questionApi.edit(questionToEditQuestionRequest(q)); const response = await questionApi.edit(questionToEditQuestionRequest(q));
setQuestionBackendId(questionId, response.updated); setQuestionBackendId(questionId, response.updated);
}).catch(error => { }).catch(error => {
if (isAxiosCanceledError(error)) return; if (isAxiosCanceledError(error)) return;
devlog("Error editing question", { error, questionId }); devlog("Error editing question", { error, questionId });
enqueueSnackbar("Не удалось сохранить вопрос"); enqueueSnackbar("Не удалось сохранить вопрос");
}); });
// }, REQUEST_DEBOUNCE); // }, REQUEST_DEBOUNCE);
}; };
@ -273,7 +273,7 @@ export const createTypedQuestion = async (
const question = useQuestionsStore.getState().questions.find(q => q.id === questionId); const question = useQuestionsStore.getState().questions.find(q => q.id === questionId);
if (!question) return; if (!question) return;
if (question.type !== null) throw new Error("Cannot upgrade already typed question"); if (question.type !== null) throw new Error("Cannot upgrade already typed question");
try { try {
const createdQuestion = await questionApi.create({ const createdQuestion = await questionApi.create({
quiz_id: question.quizId, quiz_id: question.quizId,
@ -369,7 +369,7 @@ function setProducedState<A extends string | { type: unknown; }>(
export const cleardragQuestionContentId = () => { export const cleardragQuestionContentId = () => {
useQuestionsStore.setState({dragQuestionContentId: null}); useQuestionsStore.setState({ dragQuestionContentId: null });
}; };
export const getQuestionById = (questionId: string | null) => { export const getQuestionById = (questionId: string | null) => {
@ -377,16 +377,19 @@ export const getQuestionById = (questionId: string | null) => {
return useQuestionsStore.getState().questions.find(q => q.id === questionId) || null; return useQuestionsStore.getState().questions.find(q => q.id === questionId) || null;
}; };
export const getQuestionByContentId = (questionContentId: string | null) => { export const getQuestionByContentId = (questionContentId: string | null) => {
console.log("questionContentId " + questionContentId) console.log("questionContentId " + questionContentId);
if (questionContentId === null) return null; if (questionContentId === null) return null;
return useQuestionsStore.getState().questions.find(q => { return useQuestionsStore.getState().questions.find(q => {
console.log(q.content.id) if (q.type === null) return false;
console.log(q.content.id === questionContentId)
return ( q.content.id === questionContentId)}) || null; console.log(q.content.id);
console.log(q.content.id === questionContentId);
return (q.content.id === questionContentId);
}) || null;
}; };
export const updateOpenedModalSettingsId = (id?: string) => useQuestionsStore.setState({openedModalSettingsId: id ? id : null}); export const updateOpenedModalSettingsId = (id?: string) => useQuestionsStore.setState({ openedModalSettingsId: id ? id : null });
export const updateDragQuestionContentId = (contentId?: string) => { export const updateDragQuestionContentId = (contentId?: string) => {
console.log("contentId " + contentId) console.log("contentId " + contentId);
useQuestionsStore.setState({dragQuestionContentId: contentId ? contentId : null}); useQuestionsStore.setState({ dragQuestionContentId: contentId ? contentId : null });
} };

@ -4,7 +4,7 @@ import { devtools } from "zustand/middleware";
export type QuestionsStore = { export type QuestionsStore = {
questions: (AnyTypedQuizQuestion | UntypedQuizQuestion); questions: (AnyTypedQuizQuestion | UntypedQuizQuestion)[];
openedModalSettingsId: string | null; openedModalSettingsId: string | null;
dragQuestionContentId: string | null; dragQuestionContentId: string | null;
}; };