From 1205f0ebf787c704c209801c0309202d0b51adfd Mon Sep 17 00:00:00 2001 From: "Nathan.fooo" <86001920+appflowy@users.noreply.github.com> Date: Mon, 28 Aug 2023 21:00:55 +0800 Subject: [PATCH] chore: enable edit user email (#3286) --- .../application/user/settings_user_bloc.dart | 9 +++ .../settings/widgets/settings_user_view.dart | 70 +++++++++++++++++++ frontend/resources/translations/en.json | 1 + 3 files changed, 80 insertions(+) diff --git a/frontend/appflowy_flutter/lib/workspace/application/user/settings_user_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/user/settings_user_bloc.dart index 14cd44ebec..853148899c 100644 --- a/frontend/appflowy_flutter/lib/workspace/application/user/settings_user_bloc.dart +++ b/frontend/appflowy_flutter/lib/workspace/application/user/settings_user_bloc.dart @@ -57,6 +57,14 @@ class SettingsUserViewBloc extends Bloc { openHistoricalUser: (HistoricalUserPB historicalUser) async { await UserBackendService.openHistoricalUser(historicalUser); }, + updateUserEmail: (String email) { + _userService.updateUserProfile(email: email).then((result) { + result.fold( + (l) => null, + (err) => Log.error(err), + ); + }); + }, ); }); } @@ -96,6 +104,7 @@ class SettingsUserViewBloc extends Bloc { class SettingsUserEvent with _$SettingsUserEvent { const factory SettingsUserEvent.initial() = _Initial; const factory SettingsUserEvent.updateUserName(String name) = _UpdateUserName; + const factory SettingsUserEvent.updateUserEmail(String email) = _UpdateEmail; const factory SettingsUserEvent.updateUserIcon(String iconUrl) = _UpdateUserIcon; const factory SettingsUserEvent.updateUserOpenAIKey(String openAIKey) = diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_user_view.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_user_view.dart index 9e4980ceca..d3997751b9 100644 --- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_user_view.dart +++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_user_view.dart @@ -50,6 +50,12 @@ class SettingsUserView extends StatelessWidget { mainAxisSize: MainAxisSize.min, children: [ _renderUserNameInput(context), + + if (user.email.isNotEmpty) ...[ + const VSpace(20), + UserEmailInput(user.email) + ], + const VSpace(20), _renderCurrentIcon(context), const VSpace(20), @@ -174,6 +180,70 @@ class UserNameInputState extends State { } } +@visibleForTesting +class UserEmailInput extends StatefulWidget { + final String email; + + const UserEmailInput( + this.email, { + Key? key, + }) : super(key: key); + + @override + UserEmailInputState createState() => UserEmailInputState(); +} + +class UserEmailInputState extends State { + late TextEditingController _controller; + + Timer? _debounce; + final Duration _debounceDuration = const Duration(milliseconds: 500); + + @override + void initState() { + super.initState(); + _controller = TextEditingController(text: widget.email); + } + + @override + Widget build(BuildContext context) { + return TextField( + controller: _controller, + decoration: InputDecoration( + labelText: LocaleKeys.settings_user_email.tr(), + labelStyle: Theme.of(context) + .textTheme + .titleMedium! + .copyWith(fontWeight: FontWeight.w500), + enabledBorder: UnderlineInputBorder( + borderSide: + BorderSide(color: Theme.of(context).colorScheme.onBackground), + ), + focusedBorder: UnderlineInputBorder( + borderSide: BorderSide(color: Theme.of(context).colorScheme.primary), + ), + ), + onChanged: (val) { + if (_debounce?.isActive ?? false) { + _debounce!.cancel(); + } + + _debounce = Timer(_debounceDuration, () { + context + .read() + .add(SettingsUserEvent.updateUserEmail(val)); + }); + }, + ); + } + + @override + void dispose() { + _controller.dispose(); + super.dispose(); + } +} + class _OpenaiKeyInput extends StatefulWidget { final String openAIKey; const _OpenaiKeyInput( diff --git a/frontend/resources/translations/en.json b/frontend/resources/translations/en.json index 2af12f660c..d7df1f1970 100644 --- a/frontend/resources/translations/en.json +++ b/frontend/resources/translations/en.json @@ -305,6 +305,7 @@ }, "user": { "name": "Name", + "email": "Email", "icon": "Icon", "selectAnIcon": "Select an icon", "pleaseInputYourOpenAIKey": "please input your OpenAI key",