Skip to content

Commit 31f984a

Browse files
committed
refactor: ContestUpdatePageのfetchをreact-refetchを使用しないよう修正
1 parent 1ece6b6 commit 31f984a

File tree

3 files changed

+18
-27
lines changed

3 files changed

+18
-27
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
UserResponse,
55
VirtualContestDetails,
66
VirtualContestInfo,
7-
VirtualContestItem,
87
} from "../pages/Internal/types";
98
import { useSWRData } from "./index";
109

@@ -53,9 +52,3 @@ export const useRecentContests = () => {
5352
typeCastFetcher<VirtualContestInfo[]>(url)
5453
);
5554
};
56-
57-
export const useContestUpdate = () => {
58-
return useSWRData(`${BASE_URL}/contest/update/`, (url) =>
59-
typeCastFetcher<VirtualContestItem[]>(url)
60-
);
61-
};

atcoder-problems-frontend/src/pages/Internal/VirtualContest/ApiClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const updateVirtualContestItems = (
6666
order: i,
6767
})),
6868
}),
69-
}).then(() => ({}));
69+
}).then((response) => response);
7070

7171
export const joinContest = (contestId: string) =>
7272
fetch(CONTEST_JOIN, {

atcoder-problems-frontend/src/pages/Internal/VirtualContest/ContestUpdatePage.tsx

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import React from "react";
22
import { List } from "immutable";
33
import { Redirect } from "react-router-dom";
44
import { Alert, Spinner } from "reactstrap";
5-
import {
6-
useVirtualContest,
7-
useContestUpdate,
8-
} from "../../../api/InternalAPIClient";
5+
import { useVirtualContest } from "../../../api/InternalAPIClient";
96
import * as DateUtil from "../../../utils/DateUtil";
107
import { VirtualContestItem } from "../types";
118
import {
@@ -29,30 +26,30 @@ interface OuterProps {
2926
contestId: string;
3027
}
3128

29+
const createAndUpdateContest = async (
30+
request: Request,
31+
problems: VirtualContestItem[]
32+
) => {
33+
const response = await updateVirtualContestInfo(request).then(() =>
34+
updateVirtualContestItems(request.id, problems)
35+
);
36+
return response.status;
37+
};
38+
3239
export const ContestUpdatePage = (props: OuterProps) => {
3340
const { contestId } = props;
34-
const updateResponse = useContestUpdate();
3541
const contestResponse = useVirtualContest(contestId);
42+
const [updateResponse, setUpdateResponse] = React.useState<number>();
3643
if (!contestResponse.data && !contestResponse.error) {
3744
return <Spinner style={{ width: "3rem", height: "3rem" }} />;
3845
} else if (contestResponse.error || !contestResponse.data) {
3946
return <Alert color="danger">Failed to fetch contest info.</Alert>;
4047
}
4148

42-
const updateContest = (request: Request, problems: VirtualContestItem[]) => ({
43-
updateResponse: {
44-
comparison: null,
45-
value: () =>
46-
updateVirtualContestInfo(request).then(() =>
47-
updateVirtualContestItems(props.contestId, problems)
48-
),
49-
},
50-
});
51-
5249
const contestInfo = contestResponse.data.info;
5350
const contestProblems = contestResponse.data.problems;
5451

55-
if (!updateResponse.data && updateResponse.error) {
52+
if (updateResponse === 200) {
5653
return <Redirect to={`/contest/show/${contestId}`} />;
5754
}
5855

@@ -84,7 +81,7 @@ export const ContestUpdatePage = (props: OuterProps) => {
8481
initialPublicState={contestInfo.is_public}
8582
initialPenaltySecond={contestInfo.penalty_second}
8683
buttonTitle="Update"
87-
buttonPush={({
84+
buttonPush={async ({
8885
title,
8986
memo,
9087
startSecond,
@@ -93,8 +90,8 @@ export const ContestUpdatePage = (props: OuterProps) => {
9390
mode,
9491
publicState,
9592
penaltySecond,
96-
}): void => {
97-
updateContest(
93+
}) => {
94+
const status = await createAndUpdateContest(
9895
{
9996
id: contestId,
10097
title,
@@ -107,6 +104,7 @@ export const ContestUpdatePage = (props: OuterProps) => {
107104
},
108105
ps.toArray()
109106
);
107+
setUpdateResponse(status);
110108
}}
111109
/>
112110
);

0 commit comments

Comments
 (0)