Skip to content

Commit f07a582

Browse files
authored
Merge pull request #1315 from a-yossy/delete-my-account-page-react-refetch
[MyAccountPage] react-refetchをfetchに置き換える
2 parents 008364e + 7bdc0f2 commit f07a582

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { PROGRESS_RESET_ADD, PROGRESS_RESET_DELETE } from "../ApiUrl";
1+
import {
2+
PROGRESS_RESET_ADD,
3+
PROGRESS_RESET_DELETE,
4+
USER_UPDATE,
5+
} from "../ApiUrl";
26
import { getCurrentUnixtimeInSecond } from "../../../utils/DateUtil";
37

48
export const addResetProgress = (problemId: string) =>
@@ -23,3 +27,14 @@ export const deleteResetProgress = (problemId: string) =>
2327
problem_id: problemId,
2428
}),
2529
});
30+
31+
export const updateUserInfo = (userId: string) =>
32+
fetch(USER_UPDATE, {
33+
method: "POST",
34+
headers: {
35+
"Content-Type": "application/json",
36+
},
37+
body: JSON.stringify({
38+
atcoder_user_id: userId,
39+
}),
40+
});

atcoder-problems-frontend/src/pages/Internal/MyAccountPage/index.tsx

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useEffect, useState } from "react";
2-
import { connect, PromiseState, PropsMapInner } from "react-refetch";
32
import { Nav, NavItem, NavLink, Spinner } from "reactstrap";
43
import {
54
Redirect,
@@ -10,25 +9,29 @@ import {
109
useLocation,
1110
} from "react-router-dom";
1211
import { useLoginState } from "../../../api/InternalAPIClient";
13-
import { USER_UPDATE } from "../ApiUrl";
1412
import { UserProblemListPage } from "../UserProblemListPage";
1513
import { MyContestList } from "./MyContestList";
1614
import { ResetProgress } from "./ResetProgress";
1715
import { UserIdUpdate } from "./UserIdUpdate";
16+
import { updateUserInfo } from "./ApiClient";
1817

19-
interface InnerProps {
20-
updateUserInfo: (atcoderUser: string) => void;
21-
updateUserInfoResponse: PromiseState<Record<string, unknown> | null>;
22-
}
23-
24-
const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
25-
const { updateUserInfoResponse } = props;
18+
export const MyAccountPage = (): JSX.Element => {
2619
const loginState = useLoginState();
2720

2821
const [userId, setUserId] = useState("");
22+
const [isUpdating, setIsUpdating] = useState(false);
23+
const [isValidResponse, setIsValidResponse] = useState<boolean>();
2924
const { path } = useRouteMatch();
3025
const { pathname } = useLocation();
3126

27+
const handleSubmit = async (userId: string) => {
28+
setIsUpdating(true);
29+
await updateUserInfo(userId).then((response) => {
30+
setIsValidResponse(response.status === 200);
31+
});
32+
setIsUpdating(false);
33+
};
34+
3235
useEffect(() => {
3336
if (loginState.data) {
3437
setUserId(loginState.data.atcoder_user_id ?? "");
@@ -37,16 +40,12 @@ const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
3740
// eslint-disable-next-line react-hooks/exhaustive-deps
3841
}, [!!loginState.data]);
3942

40-
if (loginState.error || updateUserInfoResponse.rejected) {
43+
if (loginState.error || isValidResponse === false) {
4144
return <Redirect to="/" />;
4245
} else if (!loginState.data) {
4346
return <Spinner style={{ width: "3rem", height: "3rem" }} />;
4447
} else {
45-
const updating = updateUserInfoResponse.refreshing;
46-
const updated =
47-
!updating &&
48-
updateUserInfoResponse.fulfilled &&
49-
updateUserInfoResponse.value !== null;
48+
const updated = !isUpdating && isValidResponse;
5049

5150
return (
5251
<>
@@ -90,8 +89,8 @@ const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
9089
<UserIdUpdate
9190
userId={userId}
9291
setUserId={setUserId}
93-
onSubmit={() => props.updateUserInfo(userId)}
94-
status={updating ? "updating" : updated ? "updated" : "open"}
92+
onSubmit={() => handleSubmit(userId)}
93+
status={isUpdating ? "updating" : updated ? "updated" : "open"}
9594
/>
9695
</Route>
9796
<Route exact path={`${path}/contests`}>
@@ -108,20 +107,3 @@ const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
108107
);
109108
}
110109
};
111-
112-
export const MyAccountPage = connect<unknown, InnerProps>(() => ({
113-
updateUserInfo: (
114-
atcoderUser: string
115-
): PropsMapInner<Pick<InnerProps, "updateUserInfoResponse">> => ({
116-
updateUserInfoResponse: {
117-
force: true,
118-
refreshing: true,
119-
url: USER_UPDATE,
120-
method: "POST",
121-
body: JSON.stringify({ atcoder_user_id: atcoderUser }),
122-
},
123-
}),
124-
updateUserInfoResponse: {
125-
value: null,
126-
},
127-
}))(InnerMyAccountPage);

0 commit comments

Comments
 (0)