From f3eed84b42000d6705431e4870c016274bd4e536 Mon Sep 17 00:00:00 2001 From: nflnkr Date: Tue, 14 Nov 2023 19:43:21 +0300 Subject: [PATCH] minor fixes --- src/model/question/create.ts | 22 +++++--- src/model/question/getList.ts | 32 +++++++---- src/model/quiz/quiz.ts | 1 + src/pages/createQuize/MyQuizzesFull.tsx | 2 +- src/pages/startPage/StartPage.tsx | 2 +- src/stores/quizes/actions.ts | 70 ++++++++++++++----------- src/ui_kit/switchStepPages.tsx | 2 +- 7 files changed, 80 insertions(+), 51 deletions(-) diff --git a/src/model/question/create.ts b/src/model/question/create.ts index c96ca483..d7beef2a 100644 --- a/src/model/question/create.ts +++ b/src/model/question/create.ts @@ -1,9 +1,19 @@ +import { DefiniteQuestionType } from "@model/questionTypes/shared"; + + export interface CreateQuestionRequest { + /** id of quiz for what question is creating */ quiz_id: number; - title: string; - description: string; - type: string; - required: boolean; - page: number; - content: string; + /** title of question. max length 512 */ + title?: string; + /** description of question. html/text */ + description?: string; + /** type of question. allow only text, select, file, variant, images, varimg, emoji, date, number, page, rating */ + type?: DefiniteQuestionType; + /** set true if user MUST answer this question */ + required?: boolean; + /** page of question */ + page?: number; + /** json serialized of question content settings */ + content?: string; } diff --git a/src/model/question/getList.ts b/src/model/question/getList.ts index 38536c01..3e346106 100644 --- a/src/model/question/getList.ts +++ b/src/model/question/getList.ts @@ -1,16 +1,28 @@ +import { Question } from "./question"; + + export interface GetQuestionListRequest { - limit: number; - offset: number; - from: number; - to: number; - search: string; - type: string; - deleted: boolean; - required: boolean; - quiz_id: number; + /** max items on page */ + limit?: number; + /** page number */ + offset?: number; + /** start time of time period. timestamp in seconds */ + from?: number; + /** end time of time period. timestamp in seconds */ + to?: number; + /** string for fulltext search in titles of questions */ + search?: string; + /** allow only - text, select, file, variant, images, varimg, emoji, date, number, page, rating or empty string */ + type?: string; + /** get deleted quizes */ + deleted?: boolean; + /** get only require questions */ + required?: boolean; + /** relation to quiz */ + quiz_id?: number; } export interface GetQuestionListResponse { count: number; - items: unknown[]; // TODO + items: Question[]; } diff --git a/src/model/quiz/quiz.ts b/src/model/quiz/quiz.ts index 49655b15..35aeb3e9 100644 --- a/src/model/quiz/quiz.ts +++ b/src/model/quiz/quiz.ts @@ -56,6 +56,7 @@ export interface Quiz { group_id: number; } +/** Type that comes from server */ export interface RawQuiz { /** Id of created quiz */ id: number; diff --git a/src/pages/createQuize/MyQuizzesFull.tsx b/src/pages/createQuize/MyQuizzesFull.tsx index d2676fe0..466a728f 100644 --- a/src/pages/createQuize/MyQuizzesFull.tsx +++ b/src/pages/createQuize/MyQuizzesFull.tsx @@ -36,7 +36,7 @@ export default function MyQuizzesFull({ onError: error => { const message = isAxiosError(error) ? (error.response?.data ?? "") : ""; - devlog("Error creating quiz", error); + devlog("Error getting quiz list", error); enqueueSnackbar(`Не удалось получить квизы. ${message}`); }, }); diff --git a/src/pages/startPage/StartPage.tsx b/src/pages/startPage/StartPage.tsx index 38360531..080c36e8 100755 --- a/src/pages/startPage/StartPage.tsx +++ b/src/pages/startPage/StartPage.tsx @@ -37,7 +37,7 @@ export default function StartPage() { onError: error => { const message = isAxiosError(error) ? (error.response?.data ?? "") : ""; - devlog("Error creating quiz", error); + devlog("Error getting quiz list", error); enqueueSnackbar(`Не удалось получить квизы. ${message}`); }, }); diff --git a/src/stores/quizes/actions.ts b/src/stores/quizes/actions.ts index e58f9aa8..387dcefd 100644 --- a/src/stores/quizes/actions.ts +++ b/src/stores/quizes/actions.ts @@ -8,6 +8,7 @@ import { enqueueSnackbar } from "notistack"; import { NavigateFunction } from "react-router-dom"; import { QuizStore, useQuizStore } from "./store"; import { isAxiosCanceledError } from "../../utils/isAxiosCanceledError"; +import { createQuestion } from "@root/questions/actions"; export const setEditQuizId = (quizId: number | null) => setProducedState(state => { @@ -99,37 +100,6 @@ export const setCurrentStep = (step: number) => setProducedState(state => { state.currentStep = Math.max(0, Math.min(maxQuizSetupSteps, step)) as QuizSetupStep; }); -export const createQuiz = async (navigate: NavigateFunction) => { - try { - const quiz = await quizApi.create({ - name: "Quiz name", - description: "Quiz description", - }); - - setQuiz(rawQuizToQuiz(quiz)); - setEditQuizId(quiz.id); - navigate("/edit"); - } catch (error) { - devlog("Error creating quiz", error); - - const message = getMessageFromFetchError(error) ?? ""; - enqueueSnackbar(`Не удалось создать квиз. ${message}`); - } -}; - -export const deleteQuiz = async (quizId: number) => { - try { - await quizApi.delete(quizId); - - removeQuiz(quizId); - } catch (error) { - devlog("Error deleting quiz", error); - - const message = getMessageFromFetchError(error) ?? ""; - enqueueSnackbar(`Не удалось удалить квиз. ${message}`); - } -}; - export const setQuizType = ( quizId: number, quizType: QuizConfig["type"], @@ -177,7 +147,10 @@ export const updateQuizWithFnOptimistic = async ( setQuiz(currentUpdatedQuiz); try { - const { updated: newId } = await quizApi.edit(quizToEditQuizRequest(currentUpdatedQuiz), controller.signal); + const { updated: newId } = await quizApi.edit( + quizToEditQuizRequest(currentUpdatedQuiz), + controller.signal, + ); setQuizField(quiz.id, "id", newId); setEditQuizId(newId); @@ -203,6 +176,39 @@ export const updateQuizWithFnOptimistic = async ( } }; +export const createQuiz = async (navigate: NavigateFunction) => { + try { + const quiz = await quizApi.create({ + name: "Quiz name", + description: "Quiz description", + }); + + setQuiz(rawQuizToQuiz(quiz)); + setEditQuizId(quiz.id); + navigate("/edit"); + + await createQuestion(quiz.id); + } catch (error) { + devlog("Error creating quiz", error); + + const message = getMessageFromFetchError(error) ?? ""; + enqueueSnackbar(`Не удалось создать квиз. ${message}`); + } +}; + +export const deleteQuiz = async (quizId: number) => { + try { + await quizApi.delete(quizId); + + removeQuiz(quizId); + } catch (error) { + devlog("Error deleting quiz", error); + + const message = getMessageFromFetchError(error) ?? ""; + enqueueSnackbar(`Не удалось удалить квиз. ${message}`); + } +}; + function setProducedState( recipe: (state: QuizStore) => void, action?: A, diff --git a/src/ui_kit/switchStepPages.tsx b/src/ui_kit/switchStepPages.tsx index e36e42a1..f8415a24 100755 --- a/src/ui_kit/switchStepPages.tsx +++ b/src/ui_kit/switchStepPages.tsx @@ -25,7 +25,7 @@ export default function SwitchStepPages({ case 1: return ; case 2: return ; case 3: return ; - case 4: return quizType === "form" ? : ; + case 4: return quizType === "form" ? : ; case 5: return ; case 6: return ; case 7: return ;