From 3d1777f6b6d07f8d14d27eca5745ea61acf94bf9 Mon Sep 17 00:00:00 2001 From: Snowball_233 Date: Sun, 25 Aug 2024 18:36:22 +0800 Subject: [PATCH] fix: refactor part of Api declaration --- api/AnswerApi.ts | 2 +- api/QuestionApi.ts | 30 ++++--- api/SurveyApi.ts | 15 +++- .../backstage/judge/[answerId]/page.tsx | 9 --- app/(root)/survey/[id]/page.tsx | 78 +++++-------------- 5 files changed, 52 insertions(+), 82 deletions(-) diff --git a/api/AnswerApi.ts b/api/AnswerApi.ts index 494e2d2..87ac9b9 100644 --- a/api/AnswerApi.ts +++ b/api/AnswerApi.ts @@ -43,7 +43,7 @@ export default class AnswerApi { if (!res.ok) { notifications.show({ - title: '获取题目失败, 请将以下信息反馈给管理员', + title: '获取问卷答案失败, 请将以下信息反馈给管理员', message: `${res.statusText}: ${await res.text()}`, color: 'red', }); diff --git a/api/QuestionApi.ts b/api/QuestionApi.ts index 063925a..8597bc3 100644 --- a/api/QuestionApi.ts +++ b/api/QuestionApi.ts @@ -133,18 +133,26 @@ export interface Page { } export interface Value { - content: string - title: string + content: string; + title: string; } export interface QuestionProps { - id: string - content: Value - type: number - values: Value[] - condition: string | undefined - answer: string | undefined - all_points: number | undefined - sub_points: number | undefined - required: boolean | undefined + id: string; + content: Value; + type: number; + values: Value[]; + condition: string | undefined; + answer: string | undefined; + all_points: number | undefined; + sub_points: number | undefined; + required: boolean | undefined; +} + +export interface PageResponse { + id: string; + title: string; + budge: string; + content: string[]; + next: string | null; } diff --git a/api/SurveyApi.ts b/api/SurveyApi.ts index 8197548..37b0183 100644 --- a/api/SurveyApi.ts +++ b/api/SurveyApi.ts @@ -18,7 +18,7 @@ export default class SurveyApi { return result; }; - public static getSurvey = async (id: number): Promise => { + public static getSurvey = async (id: number) => { const myHeaders = new Headers(); myHeaders.append('token', Cookie.getCookie('token')); @@ -29,7 +29,7 @@ export default class SurveyApi { const res = await fetch(`${SERVER_URL}/api/survey/${id}`, requestOptions); - const result: SurveyInfo = JSON.parse(await res.text()); + const result: SurveyResponse = JSON.parse(await res.text()); return result; }; @@ -79,6 +79,17 @@ export default class SurveyApi { export type NewSurveyInfo = Omit; +export interface SurveyResponse { + id: number; + title: string; + budge: string; + description: string; + image: string; + page: string; + start_date: string; + end_date: string; +} + export interface SurveyInfo { id: number; title: string; diff --git a/app/(root)/backstage/judge/[answerId]/page.tsx b/app/(root)/backstage/judge/[answerId]/page.tsx index 4d203fd..3354666 100644 --- a/app/(root)/backstage/judge/[answerId]/page.tsx +++ b/app/(root)/backstage/judge/[answerId]/page.tsx @@ -1,7 +1,6 @@ 'use client'; import React, { useEffect, useRef, useState } from 'react'; -import { notifications } from '@mantine/notifications'; import { Button, Center, Container, Space, Stack, Text, Title } from '@mantine/core'; import AnswerApi, { AnswerInfo } from '@/api/AnswerApi'; import QuestionApi, { Page, QuestionProps } from '@/api/QuestionApi'; @@ -18,7 +17,6 @@ export default function JudgeSinglePage({ params }: { params: { answerId: number const [totalScore, setTotalScore] = useState(null); const [userScore, setUserScore] = useState(null); const [page, setPage] = useState(null); - // const [correctAnswer, setCorrectAnswer] = useState({}); const [currentPage, setCurrentPage] = useState(null); const [nextPage, setNextPage] = useState(null); const questionsProps = useRef(new Map()); @@ -32,13 +30,6 @@ export default function JudgeSinglePage({ params }: { params: { answerId: number .then((res2) => { setCurrentPage(res2.page); }); - }) - .catch((error) => { - notifications.show({ - title: '失败', - message: error.toString(), - color: 'red', - }); }); }, []); diff --git a/app/(root)/survey/[id]/page.tsx b/app/(root)/survey/[id]/page.tsx index fd6f88c..740c971 100644 --- a/app/(root)/survey/[id]/page.tsx +++ b/app/(root)/survey/[id]/page.tsx @@ -5,13 +5,13 @@ import { useEffect, useRef, useState } from 'react'; import { notifications } from '@mantine/notifications'; import { useSearchParams } from 'next/navigation'; import Question from '@/app/(root)/survey/[id]/components/question'; -import { QuestionProps } from '@/api/QuestionApi'; -import { SERVER_URL } from '@/api/BackendApi'; +import QuestionApi, { PageResponse, QuestionProps } from '@/api/QuestionApi'; import AnswerApi, { SaveRequest } from '@/api/AnswerApi'; +import SurveyApi from '@/api/SurveyApi'; export default function SurveyPage({ params }: { params: { id: number } }) { - const [currentPage, setCurrentPage] = useState(null); - const [nextPage, setNextPage] = useState(null); + const [currentPage, setCurrentPage] = useState(null); + const [nextPage, setNextPage] = useState(null); const [questions, setQuestions] = useState(undefined); const [answers, setAnswers] = useState>(new Map()); const searchParams = useSearchParams(); @@ -107,46 +107,25 @@ export default function SurveyPage({ params }: { params: { id: number } }) { } useEffect(() => { - const myHeaders = new Headers(); - myHeaders.append('token', '111'); - - const requestOptions: RequestInit = { - method: 'GET', - headers: myHeaders, - redirect: 'follow', - }; - - fetch(`${SERVER_URL}/api/survey/${params.id}`, requestOptions) - .then(response => response.text()) - .then(result => { - const response: SurveyResponse = JSON.parse(result); - setCurrentPage(response.page); - }) - .catch(error => { - notifications.show({ - title: '获取试题失败,请将以下信息反馈给管理员', - message: error.toString(), - color: 'red', - }); - }); + SurveyApi.getSurvey(params.id) + .then((result) => { + setCurrentPage(result.page); + } + ); }, [params.id]); useEffect(() => { if (currentPage !== null) { - const myHeaders = new Headers(); - myHeaders.append('token', '111'); - - const requestOptions: RequestInit = { - method: 'GET', - headers: myHeaders, - redirect: 'follow', - }; - - fetch(`${SERVER_URL}/api/question?page=${currentPage}`, requestOptions) - .then(response => response.text()) - .then(result => { - const response: PageResponse = JSON.parse(result); - setQuestions(response); + QuestionApi.fetchPage(currentPage) + .then((response) => { + const pageResponse: PageResponse = { + id: response.id, + title: response.title, + budge: '', + content: response.content, + next: response.next, + }; + setQuestions(pageResponse); setNextPage(response.next); }); } @@ -175,25 +154,6 @@ export default function SurveyPage({ params }: { params: { id: number } }) { ); } -interface SurveyResponse { - id: number; - title: string; - budge: string; - description: string; - image: string; - page: number; - start_date: string; - end_date: string; -} - -export interface PageResponse { - id: string; - title: string; - budge: string; - content: string[]; - next: number | null; -} - export type ConditionType = 'and' | 'or' | 'not'; export interface Condition {