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,25 +9,29 @@ 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 ( ) ;
2720
2821 const [ userId , setUserId ] = useState ( "" ) ;
22+ const [ isUpdating , setIsUpdating ] = useState ( false ) ;
23+ const [ isValidResponse , setIsValidResponse ] = useState < boolean > ( ) ;
2924 const { path } = useRouteMatch ( ) ;
3025 const { pathname } = useLocation ( ) ;
3126
27+ const handleSubmit = async ( userId : string ) => {
28+ setIsUpdating ( true ) ;
29+ await updateUserInfo ( userId ) . then ( ( response ) => {
30+ setIsValidResponse ( response . status === 200 ) ;
31+ } ) ;
32+ setIsUpdating ( false ) ;
33+ } ;
34+
3235 useEffect ( ( ) => {
3336 if ( loginState . data ) {
3437 setUserId ( loginState . data . atcoder_user_id ?? "" ) ;
@@ -37,16 +40,12 @@ const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
3740 // eslint-disable-next-line react-hooks/exhaustive-deps
3841 } , [ ! ! loginState . data ] ) ;
3942
40- if ( loginState . error || updateUserInfoResponse . rejected ) {
43+ if ( loginState . error || isValidResponse === false ) {
4144 return < Redirect to = "/" /> ;
4245 } else if ( ! loginState . data ) {
4346 return < Spinner style = { { width : "3rem" , height : "3rem" } } /> ;
4447 } else {
45- const updating = updateUserInfoResponse . refreshing ;
46- const updated =
47- ! updating &&
48- updateUserInfoResponse . fulfilled &&
49- updateUserInfoResponse . value !== null ;
48+ const updated = ! isUpdating && isValidResponse ;
5049
5150 return (
5251 < >
@@ -90,8 +89,8 @@ const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
9089 < UserIdUpdate
9190 userId = { userId }
9291 setUserId = { setUserId }
93- onSubmit = { ( ) => props . updateUserInfo ( userId ) }
94- status = { updating ? "updating" : updated ? "updated" : "open" }
92+ onSubmit = { ( ) => handleSubmit ( userId ) }
93+ status = { isUpdating ? "updating" : updated ? "updated" : "open" }
9594 />
9695 </ Route >
9796 < Route exact path = { `${ path } /contests` } >
@@ -108,20 +107,3 @@ const InnerMyAccountPage = (props: InnerProps): JSX.Element => {
108107 ) ;
109108 }
110109} ;
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