Skip to content

Commit

Permalink
fix: refactor part of Api declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
SnowballXueQiu committed Aug 25, 2024
1 parent 2ed727a commit 3d1777f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 82 deletions.
2 changes: 1 addition & 1 deletion api/AnswerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class AnswerApi {

if (!res.ok) {
notifications.show({
title: '获取题目失败, 请将以下信息反馈给管理员',
title: '获取问卷答案失败, 请将以下信息反馈给管理员',
message: `${res.statusText}: ${await res.text()}`,
color: 'red',
});
Expand Down
30 changes: 19 additions & 11 deletions api/QuestionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 13 additions & 2 deletions api/SurveyApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class SurveyApi {
return result;
};

public static getSurvey = async (id: number): Promise<SurveyInfo> => {
public static getSurvey = async (id: number) => {
const myHeaders = new Headers();
myHeaders.append('token', Cookie.getCookie('token'));

Expand All @@ -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;
};
Expand Down Expand Up @@ -79,6 +79,17 @@ export default class SurveyApi {

export type NewSurveyInfo = Omit<SurveyInfo, 'id'>;

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;
Expand Down
9 changes: 0 additions & 9 deletions app/(root)/backstage/judge/[answerId]/page.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -18,7 +17,6 @@ export default function JudgeSinglePage({ params }: { params: { answerId: number
const [totalScore, setTotalScore] = useState<number | null>(null);
const [userScore, setUserScore] = useState<number | null>(null);
const [page, setPage] = useState<Page | null>(null);
// const [correctAnswer, setCorrectAnswer] = useState({});
const [currentPage, setCurrentPage] = useState<string | null>(null);
const [nextPage, setNextPage] = useState<string | null>(null);
const questionsProps = useRef(new Map<string, QuestionProps>());
Expand All @@ -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',
});
});
}, []);

Expand Down
78 changes: 19 additions & 59 deletions app/(root)/survey/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<number | null>(null);
const [nextPage, setNextPage] = useState<number | null>(null);
const [currentPage, setCurrentPage] = useState<string | null>(null);
const [nextPage, setNextPage] = useState<string | null>(null);
const [questions, setQuestions] = useState<PageResponse | undefined>(undefined);
const [answers, setAnswers] = useState<Map<string, string>>(new Map());
const searchParams = useSearchParams();
Expand Down Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3d1777f

Please sign in to comment.