@@ -106,17 +106,32 @@ export const LoginMethodScreen = (props: Props) => {
106
106
console . log ( 'updatedGuestDetailsToProvidedUser' ) ;
107
107
} ;
108
108
109
+ let userData : any = null ;
110
+
109
111
fetchUserWithToken ( serverUrl , requestOptions )
110
112
. then ( ( json ) => {
111
- props . dispatch ( updateUserName ( json . userprofile . myname ) ) ;
113
+ userData = json ;
114
+
115
+ if ( props . isGuest ) {
116
+ onGuestToUser ( ) ;
117
+ }
118
+ } )
119
+ . catch ( ( e ) => {
120
+ // We expect the error when use has been logged out, but dispatch continues because
121
+ // if we catch too early, not all the items won't dispatch ^
122
+ console . warn ( `Error on fetchUserTokenLogin :) ${ e } ` ) ;
123
+ } )
124
+ . finally ( ( ) => {
125
+ props . dispatch ( updateUserName ( userData . userprofile . myname ) ) ;
112
126
113
127
type FetchedTag = {
114
128
tag : string ;
115
129
usage : number ;
116
130
} ;
131
+ props . dispatch ( addToken ( token ) ) ;
117
132
118
133
// Create UserTags with id and string.
119
- const newUserTags : Array < UserTag > = json . tags . tags . map (
134
+ const newUserTags : Array < UserTag > = userData . tags . tags . map (
120
135
( tag : FetchedTag ) => newUserTag ( tag . tag )
121
136
) ;
122
137
props . dispatch ( updateUserTags ( newUserTags ) ) ;
@@ -126,28 +141,22 @@ export const LoginMethodScreen = (props: Props) => {
126
141
127
142
props . dispatch (
128
143
updateUserBlogs (
129
- json . blogs . blogs . map ( ( b : UserBlogJSON ) => userBlogJSONtoUserBlog ( b ) )
144
+ userData . blogs . blogs . map ( ( b : UserBlogJSON ) =>
145
+ userBlogJSONtoUserBlog ( b )
146
+ )
130
147
)
131
148
) ;
132
149
133
150
// Check if user has folders (they can be deleted on Mahara)
134
- if ( json . folders . folders . length !== 0 ) {
135
- props . dispatch ( updateUserFolders ( json . folders . folders ) ) ;
151
+ if ( userData . folders . folders . length !== 0 ) {
152
+ props . dispatch ( updateUserFolders ( userData . folders . folders ) ) ;
136
153
}
137
154
138
- props . dispatch ( setDefaultBlogId ( json . blogs . blogs [ 0 ] . id ) ) ;
139
- props . dispatch ( setDefaultFolder ( json . folders . folders [ 0 ] . title ) ) ;
140
- props . dispatch ( addToken ( token ) ) ;
141
- flashMessage ( t `Logged in: ${ json . userprofile . myname } ` , 'success' ) ;
155
+ props . dispatch ( setDefaultBlogId ( userData . blogs . blogs [ 0 ] . id ) ) ;
156
+ props . dispatch ( setDefaultFolder ( userData . folders . folders [ 0 ] . title ) ) ;
157
+ flashMessage ( t `Logged in: ${ userData . userprofile . myname } ` , 'success' ) ;
142
158
143
159
// checkValidInitialState(props.userBlogs, props.userFolders)
144
-
145
- if ( props . isGuest ) {
146
- onGuestToUser ( ) ;
147
- }
148
- } )
149
- . catch ( ( e ) => {
150
- console . warn ( `Error on fetchUserTokenLogin :) ${ e } ` ) ;
151
160
} ) ;
152
161
} ;
153
162
0 commit comments