Skip to content

Commit e9bd69b

Browse files
authored
Merge pull request #1080 from atsuya02/ResetProgress
ResetProgressのreact-refetch を置き換える
2 parents db320c7 + 170f16e commit e9bd69b

File tree

2 files changed

+40
-71
lines changed

2 files changed

+40
-71
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { PROGRESS_RESET_ADD, PROGRESS_RESET_DELETE } from "../ApiUrl";
2+
import { getCurrentUnixtimeInSecond } from "../../../utils/DateUtil";
3+
4+
export const addResetProgress = (problemId: string) =>
5+
fetch(PROGRESS_RESET_ADD, {
6+
method: "POST",
7+
headers: {
8+
"Content-Type": "application/json",
9+
},
10+
body: JSON.stringify({
11+
problem_id: problemId,
12+
reset_epoch_second: getCurrentUnixtimeInSecond(),
13+
}),
14+
});
15+
16+
export const deleteResetProgress = (problemId: string) =>
17+
fetch(PROGRESS_RESET_DELETE, {
18+
method: "POST",
19+
headers: {
20+
"Content-Type": "application/json",
21+
},
22+
body: JSON.stringify({
23+
problem_id: problemId,
24+
}),
25+
});
Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,16 @@
11
import { Button, Col, Row, Table } from "reactstrap";
22
import React from "react";
3-
import { connect, PromiseState } from "react-refetch";
43
import { GoTrashcan } from "react-icons/go";
54
import { useProblems } from "../../../api/APIClient";
65
import { ProblemSearchBox } from "../../../components/ProblemSearchBox";
7-
import { ProgressResetList } from "../types";
8-
import {
9-
PROGRESS_RESET_ADD,
10-
PROGRESS_RESET_DELETE,
11-
PROGRESS_RESET_LIST,
12-
} from "../ApiUrl";
136
import { ProblemLink } from "../../../components/ProblemLink";
14-
import {
15-
formatMomentDateTime,
16-
parseSecond,
17-
getCurrentUnixtimeInSecond,
18-
} from "../../../utils/DateUtil";
7+
import { formatMomentDateTime, parseSecond } from "../../../utils/DateUtil";
8+
import { useProgressResetList } from "../../../api/InternalAPIClient";
9+
import { addResetProgress, deleteResetProgress } from "./ApiClient";
1910

20-
interface Props {
21-
progressResetList: PromiseState<ProgressResetList | null>;
22-
addResetProgress: (problemId: string) => void;
23-
addResetProgressResponse: PromiseState<Record<string, unknown> | null>;
24-
deleteResetProgress: (problemId: string) => void;
25-
deleteResetProgressResponse: PromiseState<Record<string, unknown> | null>;
26-
}
27-
28-
const InnerResetProgress: React.FC<Props> = (props) => {
29-
const progressResetList =
30-
props.progressResetList.fulfilled && props.progressResetList.value
31-
? props.progressResetList.value.items
32-
: [];
11+
export const ResetProgress: React.FC = () => {
12+
const progressResetListFetch = useProgressResetList();
13+
const progressResetList = progressResetListFetch.data?.items || [];
3314
progressResetList.sort((a, b) => a.reset_epoch_second - b.reset_epoch_second);
3415
const problems = useProblems() ?? [];
3516
return (
@@ -43,7 +24,11 @@ const InnerResetProgress: React.FC<Props> = (props) => {
4324
<Col sm="12">
4425
<ProblemSearchBox
4526
problems={problems}
46-
selectProblem={(problm): void => props.addResetProgress(problm.id)}
27+
selectProblem={async (problem) =>
28+
await addResetProgress(problem.id).then(() =>
29+
progressResetListFetch.mutate()
30+
)
31+
}
4732
/>
4833
</Col>
4934
</Row>
@@ -81,8 +66,10 @@ const InnerResetProgress: React.FC<Props> = (props) => {
8166
<td>
8267
<Button
8368
color="danger"
84-
onClick={(): void =>
85-
props.deleteResetProgress(item.problem_id)
69+
onClick={async () =>
70+
await deleteResetProgress(item.problem_id).then(() =>
71+
progressResetListFetch.mutate()
72+
)
8673
}
8774
>
8875
<GoTrashcan />
@@ -98,46 +85,3 @@ const InnerResetProgress: React.FC<Props> = (props) => {
9885
</>
9986
);
10087
};
101-
102-
export const ResetProgress = connect<unknown, Props>(() => ({
103-
progressResetList: {
104-
url: PROGRESS_RESET_LIST,
105-
},
106-
addResetProgressResponse: { value: null },
107-
addResetProgress: (problemId: string) => ({
108-
addResetProgressResponse: {
109-
force: true,
110-
refreshing: true,
111-
url: PROGRESS_RESET_ADD,
112-
method: "POST",
113-
body: JSON.stringify({
114-
problem_id: problemId,
115-
reset_epoch_second: getCurrentUnixtimeInSecond(),
116-
}),
117-
andThen: () => ({
118-
progressResetList: {
119-
force: true,
120-
url: PROGRESS_RESET_LIST,
121-
},
122-
}),
123-
},
124-
}),
125-
deleteResetProgress: (problemId: string) => ({
126-
deleteResetProgressResponse: {
127-
force: true,
128-
refreshing: true,
129-
url: PROGRESS_RESET_DELETE,
130-
method: "POST",
131-
body: JSON.stringify({
132-
problem_id: problemId,
133-
}),
134-
andThen: () => ({
135-
progressResetList: {
136-
force: true,
137-
url: PROGRESS_RESET_LIST,
138-
},
139-
}),
140-
},
141-
}),
142-
deleteResetProgressResponse: { value: null },
143-
}))(InnerResetProgress);

0 commit comments

Comments
 (0)