Skip to content

Commit 3c30f25

Browse files
author
Doris Tam
committed
Reorder the dispatch for login
- if we catch too early, not all the dispatches will happen
1 parent ff7e583 commit 3c30f25

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

MaharaMobile/src/screens/LoginMethodScreen/LoginMethodScreen.tsx

+25-16
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,32 @@ export const LoginMethodScreen = (props: Props) => {
106106
console.log('updatedGuestDetailsToProvidedUser');
107107
};
108108

109+
let userData: any = null;
110+
109111
fetchUserWithToken(serverUrl, requestOptions)
110112
.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));
112126

113127
type FetchedTag = {
114128
tag: string;
115129
usage: number;
116130
};
131+
props.dispatch(addToken(token));
117132

118133
// Create UserTags with id and string.
119-
const newUserTags: Array<UserTag> = json.tags.tags.map(
134+
const newUserTags: Array<UserTag> = userData.tags.tags.map(
120135
(tag: FetchedTag) => newUserTag(tag.tag)
121136
);
122137
props.dispatch(updateUserTags(newUserTags));
@@ -126,28 +141,22 @@ export const LoginMethodScreen = (props: Props) => {
126141

127142
props.dispatch(
128143
updateUserBlogs(
129-
json.blogs.blogs.map((b: UserBlogJSON) => userBlogJSONtoUserBlog(b))
144+
userData.blogs.blogs.map((b: UserBlogJSON) =>
145+
userBlogJSONtoUserBlog(b)
146+
)
130147
)
131148
);
132149

133150
// 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));
136153
}
137154

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');
142158

143159
// checkValidInitialState(props.userBlogs, props.userFolders)
144-
145-
if (props.isGuest) {
146-
onGuestToUser();
147-
}
148-
})
149-
.catch((e) => {
150-
console.warn(`Error on fetchUserTokenLogin :) ${e}`);
151160
});
152161
};
153162

0 commit comments

Comments
 (0)