1- import { connect , PromiseState } from "react-refetch" ;
21import React , { useState } from "react" ;
32import {
43 Alert ,
@@ -16,62 +15,54 @@ import {
1615 Row ,
1716 Spinner ,
1817} 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 " ;
2221
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 ;
2924}
3025
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 > ;
6148 }
62- if ( myListFetch . pending ) {
49+ if ( ! myListFetch . data ) {
6350 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 > ;
6651 }
6752
68- const myList = myListFetch . value ;
53+ const myList = myListFetch . data ?? [ ] ;
6954
7055 return (
7156 < >
7257 < Row className = "my-2" >
7358 < 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+ >
7566 Create New List
7667 </ Button >
7768 </ Col >
@@ -86,15 +77,18 @@ export const UserProblemListPage = connect<unknown, Props>(() => ({
8677 internalListId = { internal_list_id }
8778 internalListName = { internal_list_name }
8879 listItemCount = { items . length }
89- deleteList = { props . deleteList }
80+ deleteList = { async ( internalListId ) => {
81+ await deleteList ( internalListId ) ;
82+ await myListFetch . mutate ( ) ;
83+ } }
9084 />
9185 ) ) }
9286 </ ListGroup >
9387 </ Col >
9488 </ Row >
9589 </ >
9690 ) ;
97- } ) ;
91+ } ;
9892
9993interface SingleListEntryProps {
10094 internalListId : string ;
0 commit comments