Browse Source

chore: add new event to update user name

appflowy 2 years ago
parent
commit
8a0709ca23

+ 19 - 1
frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart

@@ -31,6 +31,17 @@ class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
         fetchWorkspaces: () async {
           //
         },
+        didReceiveUserProfile: (UserProfile newUserProfile) {
+          emit(state.copyWith(userProfile: newUserProfile));
+        },
+        updateUserName: (String name) {
+          _userService.updateUserProfile(name: name).then((result) {
+            result.fold(
+              (l) => null,
+              (err) => Log.error(err),
+            );
+          });
+        },
       );
     });
   }
@@ -47,7 +58,12 @@ class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
     result.fold((l) => null, (error) => Log.error(error));
   }
 
-  void _profileUpdated(Either<UserProfile, FlowyError> userProfileOrFailed) {}
+  void _profileUpdated(Either<UserProfile, FlowyError> userProfileOrFailed) {
+    userProfileOrFailed.fold(
+      (newUserProfile) => add(MenuUserEvent.didReceiveUserProfile(newUserProfile)),
+      (err) => Log.error(err),
+    );
+  }
 
   void _workspaceListUpdated(Either<List<Workspace>, FlowyError> workspacesOrFailed) {
     // Do nothing by now
@@ -58,6 +74,8 @@ class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
 class MenuUserEvent with _$MenuUserEvent {
   const factory MenuUserEvent.initial() = _Initial;
   const factory MenuUserEvent.fetchWorkspaces() = _FetchWorkspaces;
+  const factory MenuUserEvent.updateUserName(String name) = _UpdateUserName;
+  const factory MenuUserEvent.didReceiveUserProfile(UserProfile newUserProfile) = _DidReceiveUserProfile;
 }
 
 @freezed

+ 17 - 17
frontend/rust-lib/flowy-user/src/services/user_session.rs

@@ -183,23 +183,23 @@ impl UserSession {
 }
 
 impl UserSession {
-    fn read_user_profile_on_server(&self, token: &str) -> Result<(), FlowyError> {
-        let server = self.cloud_service.clone();
-        let token = token.to_owned();
-        tokio::spawn(async move {
-            match server.get_user(&token).await {
-                Ok(profile) => {
-                    // dart_notify(&token, UserNotification::UserProfileUpdated)
-                    //     .payload(profile)
-                    //     .send();
-                }
-                Err(_e) => {
-                    // dart_notify(&token, UserNotification::UserProfileUpdated)
-                    //     .error(e)
-                    //     .send();
-                }
-            }
-        });
+    fn read_user_profile_on_server(&self, _token: &str) -> Result<(), FlowyError> {
+        // let server = self.cloud_service.clone();
+        // let token = token.to_owned();
+        // tokio::spawn(async move {
+        //     match server.get_user(&token).await {
+        //         Ok(profile) => {
+        //             dart_notify(&token, UserNotification::UserProfileUpdated)
+        //                 .payload(profile)
+        //                 .send();
+        //         }
+        //         Err(e) => {
+        //             dart_notify(&token, UserNotification::UserProfileUpdated)
+        //                 .error(e)
+        //                 .send();
+        //         }
+        //     }
+        // });
         Ok(())
     }