Skip to content

Commit 738438a

Browse files
authored
Merge pull request #1065 from yb173/replace_old_api_recent_submissions
RecentSubmissions の react-refetch を swr で置き換える
2 parents 8a454b5 + 352897b commit 738438a

File tree

3 files changed

+13
-25
lines changed

3 files changed

+13
-25
lines changed

atcoder-problems-frontend/src/api/APIClient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,8 @@ export const useVirtualContestSubmissions = (
287287
}
288288
).data;
289289
};
290+
291+
export const useRecentSubmissions = () => {
292+
const url = `${ATCODER_API_URL}/v3/recent`;
293+
return useSWRData(url, (url) => fetchTypedArray(url, isSubmission));
294+
};
Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
11
import React from "react";
22
import { Alert, Row, Spinner } from "reactstrap";
3-
import { connect, PromiseState } from "react-refetch";
4-
import Submission from "../interfaces/Submission";
5-
import { fetchRecentSubmissions } from "../utils/Api";
3+
import { useRecentSubmissions } from "../api/APIClient";
64
import { SubmissionListTable } from "../components/SubmissionListTable";
75

8-
interface Props {
9-
submissions: PromiseState<Submission[]>;
10-
}
6+
export const RecentSubmissions = () => {
7+
const submissionsResponse = useRecentSubmissions();
118

12-
const InnerRecentSubmissions: React.FC<Props> = (props) => {
13-
if (props.submissions.pending) {
9+
if (!submissionsResponse.data && !submissionsResponse.error) {
1410
return <Spinner style={{ width: "3rem", height: "3rem" }} />;
11+
} else if (!submissionsResponse.data) {
12+
return <Alert color="danger">Failed to fetch submission info.</Alert>;
1513
}
16-
if (props.submissions.rejected) {
17-
return <Alert color="danger">Failed to fetch contest info.</Alert>;
18-
}
1914

20-
const submissions = props.submissions.value.sort((a, b) => b.id - a.id);
15+
const submissions = submissionsResponse.data.sort((a, b) => b.id - a.id);
16+
2117
return (
2218
<Row>
2319
<h1>Recent Submissions</h1>
2420
<SubmissionListTable submissions={submissions} />
2521
</Row>
2622
);
2723
};
28-
29-
export const RecentSubmissions = connect<unknown, Props>(() => ({
30-
submissions: {
31-
comparison: null,
32-
value: fetchRecentSubmissions,
33-
},
34-
}))(InnerRecentSubmissions);

atcoder-problems-frontend/src/utils/Api.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import { hasPropertyAsType, isNumber } from "./TypeUtils";
44

55
const ATCODER_API_URL = process.env.REACT_APP_ATCODER_API_URL;
66

7-
export const fetchRecentSubmissions = (): Promise<Submission[]> => {
8-
return fetch(`${ATCODER_API_URL}/v3/recent`)
9-
.then((r) => r.json())
10-
.then((r: unknown[]) => r.filter(isSubmission));
11-
};
12-
137
export const fetchPartialUserSubmissions = async (
148
userId: UserId,
159
fromSecond: number

0 commit comments

Comments
 (0)