Skip to content

Commit 8567496

Browse files
committed
Add error messages
1 parent 15e35f8 commit 8567496

File tree

5 files changed

+94
-20
lines changed

5 files changed

+94
-20
lines changed

src/hooks/judge.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { useEffect, useState } from "react";
22
import * as JudgeServiceModel from "@/models/service/judge";
33
import * as JudgeService from "@/apis/judge";
4+
import { useDispatch } from "react-redux";
5+
import { AddMessageSagaPattern } from "@/store/sagas/message";
6+
import { useTranslation } from "react-i18next";
47

58
export const useJudge = (uid: string) => {
69
const [judge, setJudge] = useState<JudgeServiceModel.JudgeInfo>();
@@ -22,6 +25,8 @@ export const useJudge = (uid: string) => {
2225
};
2326

2427
export const useRunJudge = (slug: string) => {
28+
const dispatch = useDispatch();
29+
const { t } = useTranslation();
2530
const [src, setSrc] = useState<string>("");
2631
const [src_language, setSrcLanguage] = useState<string>("");
2732

@@ -33,7 +38,16 @@ export const useRunJudge = (slug: string) => {
3338
afterJudgePosted(res);
3439
})
3540
.catch((err) => {
36-
console.log(err);
41+
dispatch({
42+
type: AddMessageSagaPattern,
43+
payload: {
44+
id: "judge-fetch-error",
45+
content: `${t("Failed to fetch judge")}`,
46+
duration: 3000,
47+
level: "error",
48+
err: err.toString(),
49+
},
50+
});
3751
});
3852
}
3953

@@ -45,6 +59,8 @@ export const useRunJudge = (slug: string) => {
4559
};
4660

4761
export const useJudgeList = () => {
62+
const dispatch = useDispatch();
63+
const { t } = useTranslation();
4864
const [total, setTotal] = useState<number>(0);
4965
const [limit, setLimit] = useState<number>(10);
5066
const [offset, setOffset] = useState<number>(0);
@@ -57,11 +73,20 @@ export const useJudgeList = () => {
5773
setTotal(res.total);
5874
})
5975
.catch((err) => {
60-
console.log(err);
76+
dispatch({
77+
type: AddMessageSagaPattern,
78+
payload: {
79+
id: "judge-list-fetch-error",
80+
content: `${t("Failed to fetch judge list")}`,
81+
duration: 3000,
82+
level: "error",
83+
err: err.toString(),
84+
},
85+
});
6186
});
6287
};
6388

64-
useEffect(getJudgeListFromServer, [limit, offset]);
89+
useEffect(getJudgeListFromServer, [dispatch, limit, offset, t]);
6590

6691
function getJudgeList() {
6792
return judgeList;

src/hooks/problem.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import * as ProblemServiceModel from "@/models/service/problem";
33
import * as ProblemService from "@/apis/problem";
44
import { useDispatch } from "react-redux";
55
import { AddMessageSagaPattern } from "@/store/sagas/message";
6+
import { useTranslation } from "react-i18next";
67

78
export const useProblem = (slug: string, fallback?: () => void) => {
9+
const dispatch = useDispatch();
10+
const { t } = useTranslation();
811
const [problem, setProblem] = useState<ProblemServiceModel.Problem | null>(
912
null,
1013
);
1114
const fallbackRef = useRef(fallback);
12-
const dispatch = useDispatch();
1315

1416
useEffect(() => {
1517
fallbackRef.current = fallback;
@@ -25,9 +27,10 @@ export const useProblem = (slug: string, fallback?: () => void) => {
2527
type: AddMessageSagaPattern,
2628
payload: {
2729
id: "problem-fetch-error",
28-
content: "Failed to fetch problem",
30+
content: `${t("Failed to fetch problem")}`,
2931
duration: 3000,
3032
err: err.toString(),
33+
level: "error",
3134
},
3235
});
3336
fallbackRef.current?.();
@@ -43,6 +46,8 @@ export const useProblem = (slug: string, fallback?: () => void) => {
4346
};
4447

4548
export const useProblemInfoList = () => {
49+
const dispatch = useDispatch();
50+
const { t } = useTranslation();
4651
const [problemList, setProblemList] = useState<
4752
ProblemServiceModel.ProblemInfo[]
4853
>([]);
@@ -64,9 +69,18 @@ export const useProblemInfoList = () => {
6469
setTotal(res.total);
6570
})
6671
.catch((err) => {
67-
console.log(err);
72+
dispatch({
73+
type: AddMessageSagaPattern,
74+
payload: {
75+
id: "problem-list-fetch-error",
76+
content: `${t("Failed to fetch problem list")}`,
77+
duration: 3000,
78+
level: "error",
79+
err: err.toString(),
80+
},
81+
});
6882
});
69-
}, [limit, offset, titleQuery, difficultyQuery]);
83+
}, [limit, offset, titleQuery, difficultyQuery, dispatch, t]);
7084

7185
useEffect(() => {
7286
getProblemInfoListFromServer();

src/hooks/rank.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { useEffect, useState } from "react";
22
import * as RankServiceModel from "@/models/service/rank";
33
import * as RankService from "@/apis/rank";
4+
import { useDispatch } from "react-redux";
5+
import { useTranslation } from "react-i18next";
6+
import { AddMessageSagaPattern } from "@/store/sagas/message";
47

58
export const useRankList = () => {
9+
const dispatch = useDispatch();
10+
const { t } = useTranslation();
611
const [rankList, setRankList] = useState<RankServiceModel.RankInfo[]>([]);
712

813
useEffect(() => {
@@ -13,11 +18,18 @@ export const useRankList = () => {
1318
}
1419
})
1520
.catch((err) => {
16-
console.log(err);
21+
dispatch({
22+
type: AddMessageSagaPattern,
23+
payload: {
24+
id: "rank-fetch-error",
25+
content: `${t("Failed to fetch rank list")}`,
26+
duration: 3000,
27+
level: "error",
28+
err: err.toString(),
29+
},
30+
});
1731
});
18-
19-
// eslint-disable-next-line react-hooks/exhaustive-deps
20-
}, []);
32+
}, [dispatch, t]);
2133

2234
function getRankList() {
2335
return rankList;

src/hooks/user.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import * as UserServiceModel from "@/models/service/user";
22
import * as UserService from "@/apis/user";
33
import { useEffect, useState } from "react";
4+
import { AddMessageSagaPattern } from "@/store/sagas/message";
5+
import { useTranslation } from "react-i18next";
6+
import { useDispatch } from "react-redux";
47

58
export const useUserInfoList = () => {
9+
const dispatch = useDispatch();
10+
const { t } = useTranslation();
611
const [userInfoList, setUserInfoList] = useState<UserServiceModel.UserInfo[]>(
712
[],
813
);
@@ -11,11 +16,24 @@ export const useUserInfoList = () => {
1116
const [offset, setOffset] = useState<number>(0);
1217

1318
useEffect(() => {
14-
UserService.getUserInfoList(limit, offset).then((res) => {
15-
setUserInfoList(res.list);
16-
setTotal(res.total);
17-
});
18-
}, [limit, offset]);
19+
UserService.getUserInfoList(limit, offset)
20+
.then((res) => {
21+
setUserInfoList(res.list);
22+
setTotal(res.total);
23+
})
24+
.catch((err) => {
25+
dispatch({
26+
type: AddMessageSagaPattern,
27+
payload: {
28+
id: "user-list-fetch-error",
29+
content: `${t("Failed to fetch user list")}`,
30+
duration: 3000,
31+
level: "error",
32+
err: err.toString(),
33+
},
34+
});
35+
});
36+
}, [dispatch, limit, offset, t]);
1937

2038
function getUserInfoList() {
2139
return userInfoList;

src/i18n/resources/zh_CN.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const ZH_CN_TRANSLATIONS: Resource = {
2020
Logout: "登出",
2121
Submit: "提交",
2222
Copy: "复制",
23+
Theme: "主题",
24+
Create: "创建",
25+
Cancel: "取消",
2326

2427
"or Register": "或注册",
2528
"Welcome!": "欢迎!",
@@ -34,11 +37,13 @@ const ZH_CN_TRANSLATIONS: Resource = {
3437
"Click and confirm submission": "点击并确认提交",
3538
"Please login first": "请先登录",
3639

37-
Theme: "主题",
40+
"Failed to fetch rank list": "获取排名列表失败",
41+
"Failed to fetch user list": "获取用户列表失败",
42+
"Failed to fetch problem list": "获取题目列表失败",
43+
"Failed to fetch problem": "获取题目失败",
44+
"Failed to fetch judge list": "获取评测列表失败",
45+
"Failed to fetch judge": "获取评测失败",
3846

39-
Create: "创建",
40-
Cancel: "取消",
41-
"Sign out": "登出",
4247
"Show actions": "显示操作",
4348

4449
Raw: "原始",

0 commit comments

Comments
 (0)