1
1
import React , { useState } from "react" ;
2
- import { connect , PromiseState } from "react-refetch" ;
3
2
import { List } from "immutable" ;
4
3
import {
5
4
Alert ,
@@ -15,49 +14,57 @@ import {
15
14
Spinner ,
16
15
} from "reactstrap" ;
17
16
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" ;
26
18
import Problem from "../../../interfaces/Problem" ;
27
19
import { ProblemSearchBox } from "../../../components/ProblemSearchBox" ;
28
20
import { formatProblemUrl } from "../../../utils/Url" ;
29
- import { ProblemList , ProblemListItem } from "../types" ;
21
+ import { ProblemListItem } from "../types" ;
30
22
import { NewTabLink } from "../../../components/NewTabLink" ;
31
23
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 {
34
31
listId : string ;
35
32
}
36
- interface InnerProps extends OuterProps {
37
- problemListFetch : PromiseState < ProblemList > ;
38
- updateList : ( name : string ) => void ;
39
- updateListResponse : PromiseState < Record < string , unknown > | null > ;
40
33
41
- addItem : ( problemId : string ) => void ;
42
- deleteItem : ( problemId : string ) => void ;
43
- updateItem : ( problemId : string , memo : string ) => void ;
44
- }
34
+ const updateList = async ( name : string , listId : string ) => {
35
+ await updateProblemList ( name , listId ) ;
36
+ } ;
37
+
38
+ const addItem = async ( problemId : string , listId : string ) => {
39
+ await addProblemItem ( problemId , listId ) ;
40
+ } ;
41
+
42
+ const deleteItem = async ( problemId : string , listId : string ) => {
43
+ await deleteProblemItem ( problemId , listId ) ;
44
+ } ;
45
45
46
- const InnerSingleProblemList = ( props : InnerProps ) => {
46
+ const updateItem = async ( problemId : string , memo : string , listId : string ) => {
47
+ await updateProblemItem ( problemId , memo , listId ) ;
48
+ } ;
49
+
50
+ export const SingleProblemList = ( props : Props ) => {
51
+ const { listId } = props ;
47
52
const loginState = useLoginState ( ) ;
48
53
const [ adding , setAdding ] = useState ( false ) ;
49
54
const [ creatingContest , setCreatingContest ] = useState ( false ) ;
50
55
const problems = useProblems ( ) ?? [ ] ;
51
56
52
- const { problemListFetch } = props ;
57
+ const problemListFetch = useProblemList ( listId ) ;
58
+
53
59
const internalUserId = loginState . data ?. internal_user_id ;
54
- if ( problemListFetch . pending ) {
60
+
61
+ if ( ! problemListFetch . data && ! problemListFetch . error ) {
55
62
return < Spinner style = { { width : "3rem" , height : "3rem" } } /> ;
56
- } else if ( problemListFetch . rejected || ! problemListFetch . value ) {
63
+ } else if ( ! problemListFetch . data ) {
57
64
return < Alert color = "danger" > Failed to fetch list info.</ Alert > ;
58
65
}
59
- const listInfo = problemListFetch . value ;
60
- const modifiable = listInfo . internal_user_id === internalUserId ;
66
+ const listInfo = problemListFetch . data ;
67
+ const modifiable = listInfo ? .internal_user_id === internalUserId ;
61
68
62
69
return creatingContest ? (
63
70
< >
@@ -87,7 +94,7 @@ const InnerSingleProblemList = (props: InnerProps) => {
87
94
< h2 >
88
95
< DoubleClickEdit
89
96
modifiable = { modifiable }
90
- saveText = { ( name ) : void => props . updateList ( name ) }
97
+ saveText = { ( name ) => updateList ( name , listId ) }
91
98
initialText = { listInfo . internal_list_name }
92
99
/>
93
100
</ h2 >
@@ -98,9 +105,11 @@ const InnerSingleProblemList = (props: InnerProps) => {
98
105
{ adding ? (
99
106
< ProblemSearchBox
100
107
problems = { problems }
101
- selectProblem = { ( problem ) : void => {
102
- props . addItem ( problem . id ) ;
103
- } }
108
+ selectProblem = { ( problem ) =>
109
+ addItem ( problem . id , listId ) . then ( ( ) =>
110
+ problemListFetch . mutate ( )
111
+ )
112
+ }
104
113
/>
105
114
) : modifiable ? (
106
115
< Button color = "success" onClick = { ( ) : void => setAdding ( ! adding ) } >
@@ -130,10 +139,16 @@ const InnerSingleProblemList = (props: InnerProps) => {
130
139
key = { item . problem_id }
131
140
item = { item }
132
141
problem = { problem }
133
- saveText = { ( memo : string ) : void =>
134
- props . updateItem ( item . problem_id , memo )
142
+ saveText = { ( memo : string ) =>
143
+ updateItem ( item . problem_id , memo , listId ) . then ( ( ) =>
144
+ problemListFetch . mutate ( )
145
+ )
146
+ }
147
+ deleteItem = { ( ) =>
148
+ deleteItem ( item . problem_id , listId ) . then ( ( ) =>
149
+ problemListFetch . mutate ( )
150
+ )
135
151
}
136
- deleteItem = { ( ) : void => props . deleteItem ( item . problem_id ) }
137
152
/>
138
153
) ;
139
154
} ) }
@@ -144,53 +159,6 @@ const InnerSingleProblemList = (props: InnerProps) => {
144
159
) ;
145
160
} ;
146
161
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
-
194
162
const ProblemEntry : React . FC < {
195
163
item : ProblemListItem ;
196
164
problem : Problem | undefined ;
0 commit comments