Skip to content

Commit dd95540

Browse files
authored
Merge pull request #1360 from hotate29/swr
`UserProblemListPage`の`react-refetch`を置き換える
2 parents 7eaec11 + 0e606b0 commit dd95540

File tree

4 files changed

+47
-64
lines changed

4 files changed

+47
-64
lines changed

atcoder-problems-frontend/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
"react-helmet": "6.1.0",
5959
"react-icons": "4.2.0",
6060
"react-measure": "2.5.2",
61-
"react-refetch": "4.0.0-0",
6261
"react-router-dom": "5.1.2",
6362
"react-scripts": "4.0.1",
6463
"reactstrap": "8.2.0",

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,9 @@ export const useRecentContests = () => {
5252
typeCastFetcher<VirtualContestInfo[]>(url)
5353
);
5454
};
55+
56+
export const useMyList = () => {
57+
return useSWRData(`${BASE_URL}/list/my`, (url) =>
58+
typeCastFetcher<ProblemList[]>(url)
59+
);
60+
};

atcoder-problems-frontend/src/pages/Internal/UserProblemListPage.tsx

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { connect, PromiseState } from "react-refetch";
21
import React, { useState } from "react";
32
import {
43
Alert,
@@ -16,62 +15,54 @@ import {
1615
Row,
1716
Spinner,
1817
} from "reactstrap";
19-
import { Link, Redirect } from "react-router-dom";
20-
import { LIST_CREATE, LIST_DELETE, LIST_MY } from "./ApiUrl";
21-
import { ProblemList } from "./types";
18+
import { Link, useHistory } from "react-router-dom";
19+
import { useMyList } from "../../api/InternalAPIClient";
20+
import { LIST_CREATE, LIST_DELETE } from "./ApiUrl";
2221

23-
interface Props {
24-
myListFetch: PromiseState<ProblemList[] | null>;
25-
createListFetch: PromiseState<{ internal_list_id: string } | null>;
26-
createNewList: () => void;
27-
deleteList: (internalListId: string) => void;
28-
deleteResponse: PromiseState<unknown | null>;
22+
interface CreateListResponse {
23+
internal_list_id: string;
2924
}
3025

31-
export const UserProblemListPage = connect<unknown, Props>(() => ({
32-
myListFetch: LIST_MY,
33-
createListFetch: { value: null },
34-
createNewList: () => ({
35-
createListFetch: {
36-
url: LIST_CREATE,
37-
method: "POST",
38-
body: JSON.stringify({ list_name: "New List" }),
39-
},
40-
}),
41-
deleteList: (internalListId: string) => ({
42-
deleteResponse: {
43-
url: LIST_DELETE,
44-
method: "POST",
45-
body: JSON.stringify({ internal_list_id: internalListId }),
46-
andThen: () => ({
47-
myListFetch: {
48-
url: LIST_MY,
49-
refreshing: true,
50-
force: true,
51-
},
52-
}),
53-
},
54-
}),
55-
deleteResponse: { value: null },
56-
}))((props) => {
57-
const { createListFetch, myListFetch } = props;
58-
if (createListFetch.fulfilled && createListFetch.value !== null) {
59-
const listId = createListFetch.value.internal_list_id;
60-
return <Redirect to={`/problemlist/${listId}`} />;
26+
const createNewList = () =>
27+
fetch(LIST_CREATE, {
28+
method: "POST",
29+
headers: { "Content-Type": "application/json" },
30+
body: JSON.stringify({ list_name: "New List" }),
31+
})
32+
.then((response) => response.json())
33+
.then((response) => response as CreateListResponse);
34+
35+
const deleteList = (internalListId: string) =>
36+
fetch(LIST_DELETE, {
37+
method: "POST",
38+
headers: { "Content-Type": "application/json" },
39+
body: JSON.stringify({ internal_list_id: internalListId }),
40+
});
41+
42+
export const UserProblemListPage: React.FC = () => {
43+
const history = useHistory();
44+
const myListFetch = useMyList();
45+
46+
if (myListFetch.error) {
47+
return <Alert color="danger">Failed to fetch list info.</Alert>;
6148
}
62-
if (myListFetch.pending) {
49+
if (!myListFetch.data) {
6350
return <Spinner style={{ width: "3rem", height: "3rem" }} />;
64-
} else if (myListFetch.rejected || !myListFetch.value) {
65-
return <Alert color="danger">Failed to fetch list info.</Alert>;
6651
}
6752

68-
const myList = myListFetch.value;
53+
const myList = myListFetch.data ?? [];
6954

7055
return (
7156
<>
7257
<Row className="my-2">
7358
<Col sm="12">
74-
<Button color="success" onClick={(): void => props.createNewList()}>
59+
<Button
60+
color="success"
61+
onClick={async () => {
62+
const internalListId = (await createNewList()).internal_list_id;
63+
history.push({ pathname: `/problemlist/${internalListId}` });
64+
}}
65+
>
7566
Create New List
7667
</Button>
7768
</Col>
@@ -86,15 +77,18 @@ export const UserProblemListPage = connect<unknown, Props>(() => ({
8677
internalListId={internal_list_id}
8778
internalListName={internal_list_name}
8879
listItemCount={items.length}
89-
deleteList={props.deleteList}
80+
deleteList={async (internalListId) => {
81+
await deleteList(internalListId);
82+
await myListFetch.mutate();
83+
}}
9084
/>
9185
))}
9286
</ListGroup>
9387
</Col>
9488
</Row>
9589
</>
9690
);
97-
});
91+
};
9892

9993
interface SingleListEntryProps {
10094
internalListId: string;

atcoder-problems-frontend/yarn.lock

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6818,13 +6818,6 @@ internmap@^1.0.0:
68186818
resolved "https://registry.yarnpkg.com/internmap/-/internmap-1.0.1.tgz#0017cc8a3b99605f0302f2b198d272e015e5df95"
68196819
integrity sha512-lDB5YccMydFBtasVtxnZ3MRBHuaoE8GKsppq+EchKL2U4nK/DmEpPHNH8MZe5HkMtpSiTSOZwfN0tzYjO/lJEw==
68206820

6821-
invariant@^2.2.4:
6822-
version "2.2.4"
6823-
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6"
6824-
integrity sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA==
6825-
dependencies:
6826-
loose-envify "^1.0.0"
6827-
68286821
ip-regex@^2.1.0:
68296822
version "2.1.0"
68306823
resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-2.1.0.tgz#fa78bf5d2e6913c911ce9f819ee5146bb6d844e9"
@@ -10651,15 +10644,6 @@ react-redux@^7.2.0:
1065110644
prop-types "^15.7.2"
1065210645
react-is "^16.13.1"
1065310646

10654-
10655-
version "4.0.0-0"
10656-
resolved "https://registry.yarnpkg.com/react-refetch/-/react-refetch-4.0.0-0.tgz#4bdc6c69f36f84d49be7010deb54bc13c0455aff"
10657-
integrity sha512-xTzCklbbNURYgDiqK5xF1BeFQT54JCKYl8MP5q+cHqLSLAXjNTfy8P1+XTK4pbAneu+gmFhJ7IM9SZiZ27zPZw==
10658-
dependencies:
10659-
hoist-non-react-statics "^3.3.0"
10660-
invariant "^2.2.4"
10661-
warning "^4.0.3"
10662-
1066310647
react-refresh@^0.8.3:
1066410648
version "0.8.3"
1066510649
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.8.3.tgz#721d4657672d400c5e3c75d063c4a85fb2d5d68f"

0 commit comments

Comments
 (0)