Skip to content

Commit 4affdbc

Browse files
authored
Merge pull request #1076 from atsuya02/SingleProblemList
SingleProblemListのreact-refetch を置き換える
2 parents 0da2dcc + bca0885 commit 4affdbc

File tree

3 files changed

+91
-83
lines changed

3 files changed

+91
-83
lines changed

atcoder-problems-frontend/src/pages/Internal/ApiUrl.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ export const LIST_UPDATE = `${BASE_URL}/list/update`;
1515
export const LIST_ITEM_UPDATE = `${BASE_URL}/list/item/update`;
1616
export const LIST_ITEM_DELETE = `${BASE_URL}/list/item/delete`;
1717
export const LIST_ITEM_ADD = `${BASE_URL}/list/item/add`;
18-
export const listGetUrl = (listId: string): string =>
19-
`${BASE_URL}/list/get/${listId}`;
2018

2119
export const PROGRESS_RESET_LIST = `${BASE_URL}/progress_reset/list`;
2220
export const PROGRESS_RESET_ADD = `${BASE_URL}/progress_reset/add`;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {
2+
LIST_ITEM_ADD,
3+
LIST_ITEM_DELETE,
4+
LIST_ITEM_UPDATE,
5+
LIST_UPDATE,
6+
} from "../ApiUrl";
7+
8+
export const updateProblemList = (name: string, listId: string) =>
9+
fetch(LIST_UPDATE, {
10+
method: "POST",
11+
headers: {
12+
"Content-Type": "application/json",
13+
},
14+
body: JSON.stringify({ internal_list_id: listId, name }),
15+
});
16+
17+
export const addProblemItem = (problemId: string, listId: string) =>
18+
fetch(LIST_ITEM_ADD, {
19+
method: "POST",
20+
headers: {
21+
"Content-Type": "application/json",
22+
},
23+
body: JSON.stringify({
24+
internal_list_id: listId,
25+
problem_id: problemId,
26+
}),
27+
});
28+
29+
export const deleteProblemItem = (problemId: string, listId: string) =>
30+
fetch(LIST_ITEM_DELETE, {
31+
method: "POST",
32+
headers: {
33+
"Content-Type": "application/json",
34+
},
35+
body: JSON.stringify({
36+
internal_list_id: listId,
37+
problem_id: problemId,
38+
}),
39+
});
40+
41+
export const updateProblemItem = (
42+
problemId: string,
43+
memo: string,
44+
listId: string
45+
) =>
46+
fetch(LIST_ITEM_UPDATE, {
47+
method: "POST",
48+
headers: {
49+
"Content-Type": "application/json",
50+
},
51+
body: JSON.stringify({
52+
internal_list_id: listId,
53+
problem_id: problemId,
54+
memo,
55+
}),
56+
});

atcoder-problems-frontend/src/pages/Internal/ProblemList/SingleProblemList.tsx

Lines changed: 35 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useState } from "react";
2-
import { connect, PromiseState } from "react-refetch";
32
import { List } from "immutable";
43
import {
54
Alert,
@@ -15,49 +14,41 @@ import {
1514
Spinner,
1615
} from "reactstrap";
1716
import { useProblems } from "../../../api/APIClient";
18-
import { useLoginState } from "../../../api/InternalAPIClient";
19-
import {
20-
LIST_ITEM_ADD,
21-
LIST_ITEM_DELETE,
22-
LIST_ITEM_UPDATE,
23-
LIST_UPDATE,
24-
listGetUrl,
25-
} from "../ApiUrl";
17+
import { useLoginState, useProblemList } from "../../../api/InternalAPIClient";
2618
import Problem from "../../../interfaces/Problem";
2719
import { ProblemSearchBox } from "../../../components/ProblemSearchBox";
2820
import { formatProblemUrl } from "../../../utils/Url";
29-
import { ProblemList, ProblemListItem } from "../types";
21+
import { ProblemListItem } from "../types";
3022
import { NewTabLink } from "../../../components/NewTabLink";
3123
import { ContestCreatePage } from "../VirtualContest/ContestCreatePage";
32-
33-
interface OuterProps {
24+
import {
25+
updateProblemList,
26+
addProblemItem,
27+
deleteProblemItem,
28+
updateProblemItem,
29+
} from "./ApiClient";
30+
interface Props {
3431
listId: string;
3532
}
36-
interface InnerProps extends OuterProps {
37-
problemListFetch: PromiseState<ProblemList>;
38-
updateList: (name: string) => void;
39-
updateListResponse: PromiseState<Record<string, unknown> | null>;
4033

41-
addItem: (problemId: string) => void;
42-
deleteItem: (problemId: string) => void;
43-
updateItem: (problemId: string, memo: string) => void;
44-
}
45-
46-
const InnerSingleProblemList = (props: InnerProps) => {
34+
export const SingleProblemList = (props: Props) => {
35+
const { listId } = props;
4736
const loginState = useLoginState();
4837
const [adding, setAdding] = useState(false);
4938
const [creatingContest, setCreatingContest] = useState(false);
5039
const problems = useProblems() ?? [];
5140

52-
const { problemListFetch } = props;
41+
const problemListFetch = useProblemList(listId);
42+
5343
const internalUserId = loginState.data?.internal_user_id;
54-
if (problemListFetch.pending) {
44+
45+
if (!problemListFetch.data && !problemListFetch.error) {
5546
return <Spinner style={{ width: "3rem", height: "3rem" }} />;
56-
} else if (problemListFetch.rejected || !problemListFetch.value) {
47+
} else if (!problemListFetch.data) {
5748
return <Alert color="danger">Failed to fetch list info.</Alert>;
5849
}
59-
const listInfo = problemListFetch.value;
60-
const modifiable = listInfo.internal_user_id === internalUserId;
50+
const listInfo = problemListFetch.data;
51+
const modifiable = listInfo?.internal_user_id === internalUserId;
6152

6253
return creatingContest ? (
6354
<>
@@ -87,7 +78,7 @@ const InnerSingleProblemList = (props: InnerProps) => {
8778
<h2>
8879
<DoubleClickEdit
8980
modifiable={modifiable}
90-
saveText={(name): void => props.updateList(name)}
81+
saveText={async (name) => await updateProblemList(name, listId)}
9182
initialText={listInfo.internal_list_name}
9283
/>
9384
</h2>
@@ -98,9 +89,11 @@ const InnerSingleProblemList = (props: InnerProps) => {
9889
{adding ? (
9990
<ProblemSearchBox
10091
problems={problems}
101-
selectProblem={(problem): void => {
102-
props.addItem(problem.id);
103-
}}
92+
selectProblem={async (problem) =>
93+
await addProblemItem(problem.id, listId).then(() =>
94+
problemListFetch.mutate()
95+
)
96+
}
10497
/>
10598
) : modifiable ? (
10699
<Button color="success" onClick={(): void => setAdding(!adding)}>
@@ -130,10 +123,18 @@ const InnerSingleProblemList = (props: InnerProps) => {
130123
key={item.problem_id}
131124
item={item}
132125
problem={problem}
133-
saveText={(memo: string): void =>
134-
props.updateItem(item.problem_id, memo)
126+
saveText={async (memo: string) =>
127+
await updateProblemItem(
128+
item.problem_id,
129+
memo,
130+
listId
131+
).then(() => problemListFetch.mutate())
132+
}
133+
deleteItem={async () =>
134+
await deleteProblemItem(item.problem_id, listId).then(() =>
135+
problemListFetch.mutate()
136+
)
135137
}
136-
deleteItem={(): void => props.deleteItem(item.problem_id)}
137138
/>
138139
);
139140
})}
@@ -144,53 +145,6 @@ const InnerSingleProblemList = (props: InnerProps) => {
144145
);
145146
};
146147

147-
export const SingleProblemList = connect<OuterProps, InnerProps>((props) => ({
148-
problemListFetch: listGetUrl(props.listId),
149-
updateList: (name: string) => ({
150-
updateListResponse: {
151-
url: LIST_UPDATE,
152-
method: "POST",
153-
body: JSON.stringify({ internal_list_id: props.listId, name }),
154-
force: true,
155-
},
156-
}),
157-
updateListResponse: { value: null },
158-
addItem: (problemId: string) => ({
159-
problemListFetch: {
160-
url: LIST_ITEM_ADD,
161-
method: "POST",
162-
body: JSON.stringify({
163-
internal_list_id: props.listId,
164-
problem_id: problemId,
165-
}),
166-
then: (): string => listGetUrl(props.listId),
167-
},
168-
}),
169-
deleteItem: (problemId: string) => ({
170-
problemListFetch: {
171-
url: LIST_ITEM_DELETE,
172-
method: "POST",
173-
body: JSON.stringify({
174-
internal_list_id: props.listId,
175-
problem_id: problemId,
176-
}),
177-
then: (): string => listGetUrl(props.listId),
178-
},
179-
}),
180-
updateItem: (problemId: string, memo: string) => ({
181-
problemListFetch: {
182-
url: LIST_ITEM_UPDATE,
183-
method: "POST",
184-
body: JSON.stringify({
185-
internal_list_id: props.listId,
186-
problem_id: problemId,
187-
memo,
188-
}),
189-
then: (): string => listGetUrl(props.listId),
190-
},
191-
}),
192-
}))(InnerSingleProblemList);
193-
194148
const ProblemEntry: React.FC<{
195149
item: ProblemListItem;
196150
problem: Problem | undefined;

0 commit comments

Comments
 (0)