1
1
import React , { useEffect , useState } from "react" ;
2
- import { connect , PromiseState , PropsMapInner } from "react-refetch" ;
3
2
import { Nav , NavItem , NavLink , Spinner } from "reactstrap" ;
4
3
import {
5
4
Redirect ,
@@ -10,24 +9,32 @@ import {
10
9
useLocation ,
11
10
} from "react-router-dom" ;
12
11
import { useLoginState } from "../../../api/InternalAPIClient" ;
13
- import { USER_UPDATE } from "../ApiUrl" ;
14
12
import { UserProblemListPage } from "../UserProblemListPage" ;
15
13
import { MyContestList } from "./MyContestList" ;
16
14
import { ResetProgress } from "./ResetProgress" ;
17
15
import { UserIdUpdate } from "./UserIdUpdate" ;
16
+ import { updateUserInfo } from "./ApiClient" ;
18
17
19
- interface InnerProps {
20
- updateUserInfo : ( atcoderUser : string ) => void ;
21
- updateUserInfoResponse : PromiseState < Record < string , unknown > | null > ;
22
- }
23
-
24
- const InnerMyAccountPage = ( props : InnerProps ) : JSX . Element => {
25
- const { updateUserInfoResponse } = props ;
18
+ export const MyAccountPage = ( ) : JSX . Element => {
26
19
const loginState = useLoginState ( ) ;
20
+ const [ isUpdating , setIsUpdating ] = useState ( false ) ;
21
+ const [ isValidResponse , setIsValidResponse ] = useState < boolean > ( ) ;
27
22
28
23
const [ userId , setUserId ] = useState ( "" ) ;
29
24
const { path } = useRouteMatch ( ) ;
30
25
const { pathname } = useLocation ( ) ;
26
+ console . log ( isUpdating ) ;
27
+ console . log ( isValidResponse ) ;
28
+
29
+ const handleSubmit = async ( userId : string ) => {
30
+ setIsUpdating ( true ) ;
31
+
32
+ await updateUserInfo ( userId ) . then ( ( response ) => {
33
+ setIsValidResponse ( response . status === 200 ) ;
34
+ } ) ;
35
+
36
+ setIsUpdating ( false ) ;
37
+ } ;
31
38
32
39
useEffect ( ( ) => {
33
40
if ( loginState . data ) {
@@ -37,16 +44,12 @@ const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
37
44
// eslint-disable-next-line react-hooks/exhaustive-deps
38
45
} , [ ! ! loginState . data ] ) ;
39
46
40
- if ( loginState . error || updateUserInfoResponse . rejected ) {
47
+ if ( loginState . error || isValidResponse === false ) {
41
48
return < Redirect to = "/" /> ;
42
49
} else if ( ! loginState . data ) {
43
50
return < Spinner style = { { width : "3rem" , height : "3rem" } } /> ;
44
51
} else {
45
- const updating = updateUserInfoResponse . refreshing ;
46
- const updated =
47
- ! updating &&
48
- updateUserInfoResponse . fulfilled &&
49
- updateUserInfoResponse . value !== null ;
52
+ const updated = ! isUpdating && isValidResponse ;
50
53
51
54
return (
52
55
< >
@@ -90,8 +93,8 @@ const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
90
93
< UserIdUpdate
91
94
userId = { userId }
92
95
setUserId = { setUserId }
93
- onSubmit = { ( ) => props . updateUserInfo ( userId ) }
94
- status = { updating ? "updating" : updated ? "updated" : "open" }
96
+ onSubmit = { async ( ) => handleSubmit ( userId ) }
97
+ status = { isUpdating ? "updating" : updated ? "updated" : "open" }
95
98
/>
96
99
</ Route >
97
100
< Route exact path = { `${ path } /contests` } >
@@ -108,20 +111,3 @@ const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
108
111
) ;
109
112
}
110
113
} ;
111
-
112
- export const MyAccountPage = connect < unknown , InnerProps > ( ( ) => ( {
113
- updateUserInfo : (
114
- atcoderUser : string
115
- ) : PropsMapInner < Pick < InnerProps , "updateUserInfoResponse" > > => ( {
116
- updateUserInfoResponse : {
117
- force : true ,
118
- refreshing : true ,
119
- url : USER_UPDATE ,
120
- method : "POST" ,
121
- body : JSON . stringify ( { atcoder_user_id : atcoderUser } ) ,
122
- } ,
123
- } ) ,
124
- updateUserInfoResponse : {
125
- value : null ,
126
- } ,
127
- } ) ) ( InnerMyAccountPage ) ;
0 commit comments