11import React , { useEffect , useState } from "react" ;
2- import { connect , PromiseState , PropsMapInner } from "react-refetch" ;
32import { Nav , NavItem , NavLink , Spinner } from "reactstrap" ;
43import {
54 Redirect ,
@@ -10,24 +9,32 @@ import {
109 useLocation ,
1110} from "react-router-dom" ;
1211import { useLoginState } from "../../../api/InternalAPIClient" ;
13- import { USER_UPDATE } from "../ApiUrl" ;
1412import { UserProblemListPage } from "../UserProblemListPage" ;
1513import { MyContestList } from "./MyContestList" ;
1614import { ResetProgress } from "./ResetProgress" ;
1715import { UserIdUpdate } from "./UserIdUpdate" ;
16+ import { updateUserInfo } from "./ApiClient" ;
1817
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 => {
2619 const loginState = useLoginState ( ) ;
20+ const [ isUpdating , setIsUpdating ] = useState ( false ) ;
21+ const [ isValidResponse , setIsValidResponse ] = useState < boolean > ( ) ;
2722
2823 const [ userId , setUserId ] = useState ( "" ) ;
2924 const { path } = useRouteMatch ( ) ;
3025 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+ } ;
3138
3239 useEffect ( ( ) => {
3340 if ( loginState . data ) {
@@ -37,16 +44,12 @@ const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
3744 // eslint-disable-next-line react-hooks/exhaustive-deps
3845 } , [ ! ! loginState . data ] ) ;
3946
40- if ( loginState . error || updateUserInfoResponse . rejected ) {
47+ if ( loginState . error || isValidResponse === false ) {
4148 return < Redirect to = "/" /> ;
4249 } else if ( ! loginState . data ) {
4350 return < Spinner style = { { width : "3rem" , height : "3rem" } } /> ;
4451 } else {
45- const updating = updateUserInfoResponse . refreshing ;
46- const updated =
47- ! updating &&
48- updateUserInfoResponse . fulfilled &&
49- updateUserInfoResponse . value !== null ;
52+ const updated = ! isUpdating && isValidResponse ;
5053
5154 return (
5255 < >
@@ -90,8 +93,8 @@ const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
9093 < UserIdUpdate
9194 userId = { userId }
9295 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" }
9598 />
9699 </ Route >
97100 < Route exact path = { `${ path } /contests` } >
@@ -108,20 +111,3 @@ const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
108111 ) ;
109112 }
110113} ;
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