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,41 @@ 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
- }
45
-
46
- const InnerSingleProblemList = ( props : InnerProps ) => {
34
+ export const SingleProblemList = ( props : Props ) => {
35
+ const { listId } = props ;
47
36
const loginState = useLoginState ( ) ;
48
37
const [ adding , setAdding ] = useState ( false ) ;
49
38
const [ creatingContest , setCreatingContest ] = useState ( false ) ;
50
39
const problems = useProblems ( ) ?? [ ] ;
51
40
52
- const { problemListFetch } = props ;
41
+ const problemListFetch = useProblemList ( listId ) ;
42
+
53
43
const internalUserId = loginState . data ?. internal_user_id ;
54
- if ( problemListFetch . pending ) {
44
+
45
+ if ( ! problemListFetch . data && ! problemListFetch . error ) {
55
46
return < Spinner style = { { width : "3rem" , height : "3rem" } } /> ;
56
- } else if ( problemListFetch . rejected || ! problemListFetch . value ) {
47
+ } else if ( ! problemListFetch . data ) {
57
48
return < Alert color = "danger" > Failed to fetch list info.</ Alert > ;
58
49
}
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 ;
61
52
62
53
return creatingContest ? (
63
54
< >
@@ -87,7 +78,7 @@ const InnerSingleProblemList = (props: InnerProps) => {
87
78
< h2 >
88
79
< DoubleClickEdit
89
80
modifiable = { modifiable }
90
- saveText = { ( name ) : void => props . updateList ( name ) }
81
+ saveText = { async ( name ) => await updateProblemList ( name , listId ) }
91
82
initialText = { listInfo . internal_list_name }
92
83
/>
93
84
</ h2 >
@@ -98,9 +89,11 @@ const InnerSingleProblemList = (props: InnerProps) => {
98
89
{ adding ? (
99
90
< ProblemSearchBox
100
91
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
+ }
104
97
/>
105
98
) : modifiable ? (
106
99
< Button color = "success" onClick = { ( ) : void => setAdding ( ! adding ) } >
@@ -130,10 +123,18 @@ const InnerSingleProblemList = (props: InnerProps) => {
130
123
key = { item . problem_id }
131
124
item = { item }
132
125
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
+ )
135
137
}
136
- deleteItem = { ( ) : void => props . deleteItem ( item . problem_id ) }
137
138
/>
138
139
) ;
139
140
} ) }
@@ -144,53 +145,6 @@ const InnerSingleProblemList = (props: InnerProps) => {
144
145
) ;
145
146
} ;
146
147
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
148
const ProblemEntry : React . FC < {
195
149
item : ProblemListItem ;
196
150
problem : Problem | undefined ;
0 commit comments