1
- import { connect , PromiseState } from "react-refetch" ;
2
1
import React , { useState } from "react" ;
3
2
import {
4
3
Alert ,
@@ -16,62 +15,54 @@ import {
16
15
Row ,
17
16
Spinner ,
18
17
} 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 " ;
22
21
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 ;
29
24
}
30
25
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 > ;
61
48
}
62
- if ( myListFetch . pending ) {
49
+ if ( ! myListFetch . data ) {
63
50
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 > ;
66
51
}
67
52
68
- const myList = myListFetch . value ;
53
+ const myList = myListFetch . data ?? [ ] ;
69
54
70
55
return (
71
56
< >
72
57
< Row className = "my-2" >
73
58
< 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
+ >
75
66
Create New List
76
67
</ Button >
77
68
</ Col >
@@ -86,15 +77,18 @@ export const UserProblemListPage = connect<unknown, Props>(() => ({
86
77
internalListId = { internal_list_id }
87
78
internalListName = { internal_list_name }
88
79
listItemCount = { items . length }
89
- deleteList = { props . deleteList }
80
+ deleteList = { async ( internalListId ) => {
81
+ await deleteList ( internalListId ) ;
82
+ await myListFetch . mutate ( ) ;
83
+ } }
90
84
/>
91
85
) ) }
92
86
</ ListGroup >
93
87
</ Col >
94
88
</ Row >
95
89
</ >
96
90
) ;
97
- } ) ;
91
+ } ;
98
92
99
93
interface SingleListEntryProps {
100
94
internalListId : string ;
0 commit comments