1- import React , { ReactElement , useCallback , useEffect , useReducer , useState } from "react" ;
2- import { emptyTsrUser , getUserInfo , setUserSettings , TsrUser , TsrUserSettings } from "./UserApi" ;
1+ import React , {
2+ ReactElement ,
3+ useCallback ,
4+ useContext ,
5+ useEffect ,
6+ useReducer ,
7+ useState ,
8+ } from "react" ;
9+ import { setUserSettings , TsrUserSettings } from "./UserApi" ;
310import { Option } from "../api" ;
411import { FieldValues , SubmitHandler , useForm } from "react-hook-form" ;
512import {
@@ -14,6 +21,7 @@ import "./UserSettings.css";
1421import { LabeledInput } from "../Inputs/LabeledInput" ;
1522import sortedUniqBy from "lodash/sortedUniqBy" ;
1623import { useHistory } from "react-router-dom" ;
24+ import UserContext from "./UserContext" ;
1725
1826interface FormData extends FieldValues {
1927 email : string ;
@@ -23,6 +31,7 @@ interface FormData extends FieldValues {
2331
2432export const UserSettings : React . FC = ( ) : ReactElement => {
2533 const history = useHistory ( ) ;
34+ const tsrUser = useContext ( UserContext ) ;
2635
2736 const orgCacheReducer = ( state : Organization [ ] , action : OrgCacheReducerAction ) => {
2837 if ( action . type === OrganizationActionTypes . LOAD ) {
@@ -36,7 +45,6 @@ export const UserSettings: React.FC = (): ReactElement => {
3645 } ;
3746 const [ organizationsCache , organizationCacheDispatch ] = useReducer ( orgCacheReducer , [ ] ) ;
3847
39- const [ user , setUser ] = useState < TsrUser > ( emptyTsrUser ) ;
4048 const [ orgValues , setOrgValues ] = useState < Option [ ] > ( [ ] ) ;
4149 const {
4250 control,
@@ -70,21 +78,19 @@ export const UserSettings: React.FC = (): ReactElement => {
7078 ) ;
7179
7280 useEffect ( ( ) : void => {
73- ( async ( ) => {
74- await getUserInfo ( )
75- . then ( ( result ) => {
76- setUser ( result ) ;
77- organizationCacheDispatch ( {
78- type : OrganizationActionTypes . LOAD ,
79- organizations : result . settings . organizations ,
80- } ) ;
81- setFormValues ( result . settings ) ;
82- } )
83- . catch ( ( error ) => {
84- console . error ( `unable to get current user ${ error . message } ` ) ;
85- } ) ;
86- } ) ( ) ;
87- } , [ setUser , setFormValues ] ) ;
81+ if ( tsrUser === undefined ) {
82+ return ;
83+ }
84+ organizationCacheDispatch ( {
85+ type : OrganizationActionTypes . LOAD ,
86+ organizations : tsrUser . settings . organizations ,
87+ } ) ;
88+ setFormValues ( tsrUser . settings ) ;
89+ } , [ tsrUser , setFormValues ] ) ;
90+
91+ if ( tsrUser === undefined ) {
92+ return < > </ > ;
93+ }
8894
8995 const onSubmit : SubmitHandler < FormData > = async ( data ) : Promise < void > => {
9096 const { phone, email } = data ;
@@ -116,13 +122,13 @@ export const UserSettings: React.FC = (): ReactElement => {
116122 } ;
117123 const onCancel = ( e : React . MouseEvent < HTMLButtonElement > ) : void => {
118124 e . preventDefault ( ) ;
119- setFormValues ( user . settings ) ;
125+ setFormValues ( tsrUser . settings ) ;
120126 } ;
121127
122128 return (
123129 < >
124130 < LinkButton onClick = { ( ) => history . push ( "/" ) } > { "< back to events" } </ LinkButton >
125- < h1 className = "UserSettings-Header" > { `${ user . username } settings` } </ h1 >
131+ < h1 className = "UserSettings-Header" > { `${ tsrUser . username } settings` } </ h1 >
126132 < div className = "UserSettings-Content" >
127133 < form
128134 className = "Form-Content"
0 commit comments