Skip to content

Commit

Permalink
refactor: add responsive form and submit button on register, forgot_p…
Browse files Browse the repository at this point in the history
…assword, change_password and login.
  • Loading branch information
cevheri committed Dec 29, 2024
1 parent 1a1cc63 commit e26fbf3
Show file tree
Hide file tree
Showing 24 changed files with 565 additions and 506 deletions.
4 changes: 4 additions & 0 deletions assets/mock/POST_account_change-password.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"currentPassword": "123456",
"newPassword": "123456"
}
4 changes: 3 additions & 1 deletion assets/mock/POST_account_reset_password_init.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"email": "[email protected]"
}
2 changes: 1 addition & 1 deletion lib/data/http_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class HttpUtils {
// .replaceAll("-", "_")
// ;
// @formatter:on
final filePath = endpoint.replaceAll("/", "_");
final filePath = endpoint.replaceAll("/", "_").replaceAll("-", "_");
if (pathParams != null) {
path += "/$httpMethod${filePath}pathParams.json";
} else if (queryParams != null) {
Expand Down
1 change: 1 addition & 0 deletions lib/data/repository/account_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class AccountRepository {
// --data-raw '[email protected]'
HttpUtils.addCustomHttpHeader('Accept', 'application/json, text/plain, */*');
HttpUtils.addCustomHttpHeader('Content-Type', 'text/plain');
// final body = {"email": mailAddress};
final httpResponse = await HttpUtils.postRequest<String>("/$_resource/reset-password/init", mailAddress);
_log.debug("END:resetPassword successful");
return httpResponse.statusCode;
Expand Down
64 changes: 32 additions & 32 deletions lib/main/main_local.mapper.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import 'package:flutter_bloc_advance/data/models/menu.dart' as x4 show Menu;
import 'package:flutter_bloc_advance/data/models/user.dart' as x0 show User;
import 'package:flutter_bloc_advance/data/models/user_jwt.dart' as x2 show UserJWT;
import 'package:flutter_bloc_advance/presentation/common_blocs/account/account_bloc.dart' as x10 show AccountStatus;
import 'package:flutter_bloc_advance/presentation/common_blocs/authority/authority_bloc.dart' as x18 show AuthorityStatus;
import 'package:flutter_bloc_advance/presentation/common_widgets/drawer/drawer_bloc/drawer_bloc.dart' as x17 show DrawerStateStatus;
import 'package:flutter_bloc_advance/presentation/screen/change_password/bloc/change_password_bloc.dart' as x13 show ChangePasswordStatus;
import 'package:flutter_bloc_advance/presentation/common_blocs/authority/authority_bloc.dart' as x17 show AuthorityStatus;
import 'package:flutter_bloc_advance/presentation/common_widgets/drawer/drawer_bloc/drawer_bloc.dart' as x16 show DrawerStateStatus;
import 'package:flutter_bloc_advance/presentation/screen/change_password/bloc/change_password_bloc.dart' as x12 show ChangePasswordStatus;
import 'package:flutter_bloc_advance/presentation/screen/components/confirmation_dialog_widget.dart' as x11 show DialogType;
import 'package:flutter_bloc_advance/presentation/screen/components/editor_form_mode.dart' as x19 show EditorFormMode;
import 'package:flutter_bloc_advance/presentation/screen/forgot_password/bloc/forgot_password_bloc.dart' as x14 show ForgotPasswordStatus;
import 'package:flutter_bloc_advance/presentation/screen/login/bloc/login_bloc.dart' as x15 show LoginStatus;
import 'package:flutter_bloc_advance/presentation/screen/register/bloc/register_bloc.dart' as x16 show RegisterStatus;
import 'package:flutter_bloc_advance/presentation/screen/user/bloc/user_bloc.dart' as x12 show UserStatus;
import 'package:flutter_bloc_advance/presentation/screen/components/editor_form_mode.dart' as x18 show EditorFormMode;
import 'package:flutter_bloc_advance/presentation/screen/forgot_password/bloc/forgot_password_bloc.dart' as x13 show ForgotPasswordStatus;
import 'package:flutter_bloc_advance/presentation/screen/login/bloc/login_bloc.dart' as x14 show LoginStatus;
import 'package:flutter_bloc_advance/presentation/screen/register/bloc/register_bloc.dart' as x15 show RegisterStatus;
import 'package:flutter_bloc_advance/presentation/screen/user/bloc/user_bloc.dart' as x19 show UserStatus;
import 'package:flutter_bloc_advance/routes/app_router.dart' as x9 show RouterType;
// This file has been generated by the reflectable package.
// https://github.com/dart-lang/reflectable.
Expand Down Expand Up @@ -89,22 +89,22 @@ final mainLocalGeneratedAdapter = JsonMapperAdapter(
typeOf<Set<x10.AccountStatus>>(): (value) => value.cast<x10.AccountStatus>(),
typeOf<List<x11.DialogType>>(): (value) => value.cast<x11.DialogType>(),
typeOf<Set<x11.DialogType>>(): (value) => value.cast<x11.DialogType>(),
typeOf<List<x12.UserStatus>>(): (value) => value.cast<x12.UserStatus>(),
typeOf<Set<x12.UserStatus>>(): (value) => value.cast<x12.UserStatus>(),
typeOf<List<x13.ChangePasswordStatus>>(): (value) => value.cast<x13.ChangePasswordStatus>(),
typeOf<Set<x13.ChangePasswordStatus>>(): (value) => value.cast<x13.ChangePasswordStatus>(),
typeOf<List<x14.ForgotPasswordStatus>>(): (value) => value.cast<x14.ForgotPasswordStatus>(),
typeOf<Set<x14.ForgotPasswordStatus>>(): (value) => value.cast<x14.ForgotPasswordStatus>(),
typeOf<List<x15.LoginStatus>>(): (value) => value.cast<x15.LoginStatus>(),
typeOf<Set<x15.LoginStatus>>(): (value) => value.cast<x15.LoginStatus>(),
typeOf<List<x16.RegisterStatus>>(): (value) => value.cast<x16.RegisterStatus>(),
typeOf<Set<x16.RegisterStatus>>(): (value) => value.cast<x16.RegisterStatus>(),
typeOf<List<x17.DrawerStateStatus>>(): (value) => value.cast<x17.DrawerStateStatus>(),
typeOf<Set<x17.DrawerStateStatus>>(): (value) => value.cast<x17.DrawerStateStatus>(),
typeOf<List<x18.AuthorityStatus>>(): (value) => value.cast<x18.AuthorityStatus>(),
typeOf<Set<x18.AuthorityStatus>>(): (value) => value.cast<x18.AuthorityStatus>(),
typeOf<List<x19.EditorFormMode>>(): (value) => value.cast<x19.EditorFormMode>(),
typeOf<Set<x19.EditorFormMode>>(): (value) => value.cast<x19.EditorFormMode>()
typeOf<List<x12.ChangePasswordStatus>>(): (value) => value.cast<x12.ChangePasswordStatus>(),
typeOf<Set<x12.ChangePasswordStatus>>(): (value) => value.cast<x12.ChangePasswordStatus>(),
typeOf<List<x13.ForgotPasswordStatus>>(): (value) => value.cast<x13.ForgotPasswordStatus>(),
typeOf<Set<x13.ForgotPasswordStatus>>(): (value) => value.cast<x13.ForgotPasswordStatus>(),
typeOf<List<x14.LoginStatus>>(): (value) => value.cast<x14.LoginStatus>(),
typeOf<Set<x14.LoginStatus>>(): (value) => value.cast<x14.LoginStatus>(),
typeOf<List<x15.RegisterStatus>>(): (value) => value.cast<x15.RegisterStatus>(),
typeOf<Set<x15.RegisterStatus>>(): (value) => value.cast<x15.RegisterStatus>(),
typeOf<List<x16.DrawerStateStatus>>(): (value) => value.cast<x16.DrawerStateStatus>(),
typeOf<Set<x16.DrawerStateStatus>>(): (value) => value.cast<x16.DrawerStateStatus>(),
typeOf<List<x17.AuthorityStatus>>(): (value) => value.cast<x17.AuthorityStatus>(),
typeOf<Set<x17.AuthorityStatus>>(): (value) => value.cast<x17.AuthorityStatus>(),
typeOf<List<x18.EditorFormMode>>(): (value) => value.cast<x18.EditorFormMode>(),
typeOf<Set<x18.EditorFormMode>>(): (value) => value.cast<x18.EditorFormMode>(),
typeOf<List<x19.UserStatus>>(): (value) => value.cast<x19.UserStatus>(),
typeOf<Set<x19.UserStatus>>(): (value) => value.cast<x19.UserStatus>()
},
enumValues: {
x6.LogFormat: x6.LogFormat.values,
Expand All @@ -114,14 +114,14 @@ final mainLocalGeneratedAdapter = JsonMapperAdapter(
x9.RouterType: x9.RouterType.values,
x10.AccountStatus: x10.AccountStatus.values,
x11.DialogType: x11.DialogType.values,
x12.UserStatus: x12.UserStatus.values,
x13.ChangePasswordStatus: x13.ChangePasswordStatus.values,
x14.ForgotPasswordStatus: x14.ForgotPasswordStatus.values,
x15.LoginStatus: x15.LoginStatus.values,
x16.RegisterStatus: x16.RegisterStatus.values,
x17.DrawerStateStatus: x17.DrawerStateStatus.values,
x18.AuthorityStatus: x18.AuthorityStatus.values,
x19.EditorFormMode: x19.EditorFormMode.values
x12.ChangePasswordStatus: x12.ChangePasswordStatus.values,
x13.ForgotPasswordStatus: x13.ForgotPasswordStatus.values,
x14.LoginStatus: x14.LoginStatus.values,
x15.RegisterStatus: x15.RegisterStatus.values,
x16.DrawerStateStatus: x16.DrawerStateStatus.values,
x17.AuthorityStatus: x17.AuthorityStatus.values,
x18.EditorFormMode: x18.EditorFormMode.values,
x19.UserStatus: x19.UserStatus.values
});

Future<JsonMapper> initializeJsonMapperAsync({Iterable<JsonMapperAdapter> adapters = const [], SerializationOptions? serializationOptions, DeserializationOptions? deserializationOptions}) => Future(() => initializeJsonMapper(adapters: adapters, serializationOptions: serializationOptions, deserializationOptions: deserializationOptions));
Expand Down
64 changes: 32 additions & 32 deletions lib/main/main_prod.mapper.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import 'package:flutter_bloc_advance/data/models/menu.dart' as x4 show Menu;
import 'package:flutter_bloc_advance/data/models/user.dart' as x0 show User;
import 'package:flutter_bloc_advance/data/models/user_jwt.dart' as x2 show UserJWT;
import 'package:flutter_bloc_advance/presentation/common_blocs/account/account_bloc.dart' as x10 show AccountStatus;
import 'package:flutter_bloc_advance/presentation/common_blocs/authority/authority_bloc.dart' as x18 show AuthorityStatus;
import 'package:flutter_bloc_advance/presentation/common_widgets/drawer/drawer_bloc/drawer_bloc.dart' as x17 show DrawerStateStatus;
import 'package:flutter_bloc_advance/presentation/screen/change_password/bloc/change_password_bloc.dart' as x13 show ChangePasswordStatus;
import 'package:flutter_bloc_advance/presentation/common_blocs/authority/authority_bloc.dart' as x17 show AuthorityStatus;
import 'package:flutter_bloc_advance/presentation/common_widgets/drawer/drawer_bloc/drawer_bloc.dart' as x16 show DrawerStateStatus;
import 'package:flutter_bloc_advance/presentation/screen/change_password/bloc/change_password_bloc.dart' as x12 show ChangePasswordStatus;
import 'package:flutter_bloc_advance/presentation/screen/components/confirmation_dialog_widget.dart' as x11 show DialogType;
import 'package:flutter_bloc_advance/presentation/screen/components/editor_form_mode.dart' as x19 show EditorFormMode;
import 'package:flutter_bloc_advance/presentation/screen/forgot_password/bloc/forgot_password_bloc.dart' as x14 show ForgotPasswordStatus;
import 'package:flutter_bloc_advance/presentation/screen/login/bloc/login_bloc.dart' as x15 show LoginStatus;
import 'package:flutter_bloc_advance/presentation/screen/register/bloc/register_bloc.dart' as x16 show RegisterStatus;
import 'package:flutter_bloc_advance/presentation/screen/user/bloc/user_bloc.dart' as x12 show UserStatus;
import 'package:flutter_bloc_advance/presentation/screen/components/editor_form_mode.dart' as x18 show EditorFormMode;
import 'package:flutter_bloc_advance/presentation/screen/forgot_password/bloc/forgot_password_bloc.dart' as x13 show ForgotPasswordStatus;
import 'package:flutter_bloc_advance/presentation/screen/login/bloc/login_bloc.dart' as x14 show LoginStatus;
import 'package:flutter_bloc_advance/presentation/screen/register/bloc/register_bloc.dart' as x15 show RegisterStatus;
import 'package:flutter_bloc_advance/presentation/screen/user/bloc/user_bloc.dart' as x19 show UserStatus;
import 'package:flutter_bloc_advance/routes/app_router.dart' as x9 show RouterType;
// This file has been generated by the reflectable package.
// https://github.com/dart-lang/reflectable.
Expand Down Expand Up @@ -89,22 +89,22 @@ final mainProdGeneratedAdapter = JsonMapperAdapter(
typeOf<Set<x10.AccountStatus>>(): (value) => value.cast<x10.AccountStatus>(),
typeOf<List<x11.DialogType>>(): (value) => value.cast<x11.DialogType>(),
typeOf<Set<x11.DialogType>>(): (value) => value.cast<x11.DialogType>(),
typeOf<List<x12.UserStatus>>(): (value) => value.cast<x12.UserStatus>(),
typeOf<Set<x12.UserStatus>>(): (value) => value.cast<x12.UserStatus>(),
typeOf<List<x13.ChangePasswordStatus>>(): (value) => value.cast<x13.ChangePasswordStatus>(),
typeOf<Set<x13.ChangePasswordStatus>>(): (value) => value.cast<x13.ChangePasswordStatus>(),
typeOf<List<x14.ForgotPasswordStatus>>(): (value) => value.cast<x14.ForgotPasswordStatus>(),
typeOf<Set<x14.ForgotPasswordStatus>>(): (value) => value.cast<x14.ForgotPasswordStatus>(),
typeOf<List<x15.LoginStatus>>(): (value) => value.cast<x15.LoginStatus>(),
typeOf<Set<x15.LoginStatus>>(): (value) => value.cast<x15.LoginStatus>(),
typeOf<List<x16.RegisterStatus>>(): (value) => value.cast<x16.RegisterStatus>(),
typeOf<Set<x16.RegisterStatus>>(): (value) => value.cast<x16.RegisterStatus>(),
typeOf<List<x17.DrawerStateStatus>>(): (value) => value.cast<x17.DrawerStateStatus>(),
typeOf<Set<x17.DrawerStateStatus>>(): (value) => value.cast<x17.DrawerStateStatus>(),
typeOf<List<x18.AuthorityStatus>>(): (value) => value.cast<x18.AuthorityStatus>(),
typeOf<Set<x18.AuthorityStatus>>(): (value) => value.cast<x18.AuthorityStatus>(),
typeOf<List<x19.EditorFormMode>>(): (value) => value.cast<x19.EditorFormMode>(),
typeOf<Set<x19.EditorFormMode>>(): (value) => value.cast<x19.EditorFormMode>()
typeOf<List<x12.ChangePasswordStatus>>(): (value) => value.cast<x12.ChangePasswordStatus>(),
typeOf<Set<x12.ChangePasswordStatus>>(): (value) => value.cast<x12.ChangePasswordStatus>(),
typeOf<List<x13.ForgotPasswordStatus>>(): (value) => value.cast<x13.ForgotPasswordStatus>(),
typeOf<Set<x13.ForgotPasswordStatus>>(): (value) => value.cast<x13.ForgotPasswordStatus>(),
typeOf<List<x14.LoginStatus>>(): (value) => value.cast<x14.LoginStatus>(),
typeOf<Set<x14.LoginStatus>>(): (value) => value.cast<x14.LoginStatus>(),
typeOf<List<x15.RegisterStatus>>(): (value) => value.cast<x15.RegisterStatus>(),
typeOf<Set<x15.RegisterStatus>>(): (value) => value.cast<x15.RegisterStatus>(),
typeOf<List<x16.DrawerStateStatus>>(): (value) => value.cast<x16.DrawerStateStatus>(),
typeOf<Set<x16.DrawerStateStatus>>(): (value) => value.cast<x16.DrawerStateStatus>(),
typeOf<List<x17.AuthorityStatus>>(): (value) => value.cast<x17.AuthorityStatus>(),
typeOf<Set<x17.AuthorityStatus>>(): (value) => value.cast<x17.AuthorityStatus>(),
typeOf<List<x18.EditorFormMode>>(): (value) => value.cast<x18.EditorFormMode>(),
typeOf<Set<x18.EditorFormMode>>(): (value) => value.cast<x18.EditorFormMode>(),
typeOf<List<x19.UserStatus>>(): (value) => value.cast<x19.UserStatus>(),
typeOf<Set<x19.UserStatus>>(): (value) => value.cast<x19.UserStatus>()
},
enumValues: {
x6.LogFormat: x6.LogFormat.values,
Expand All @@ -114,14 +114,14 @@ final mainProdGeneratedAdapter = JsonMapperAdapter(
x9.RouterType: x9.RouterType.values,
x10.AccountStatus: x10.AccountStatus.values,
x11.DialogType: x11.DialogType.values,
x12.UserStatus: x12.UserStatus.values,
x13.ChangePasswordStatus: x13.ChangePasswordStatus.values,
x14.ForgotPasswordStatus: x14.ForgotPasswordStatus.values,
x15.LoginStatus: x15.LoginStatus.values,
x16.RegisterStatus: x16.RegisterStatus.values,
x17.DrawerStateStatus: x17.DrawerStateStatus.values,
x18.AuthorityStatus: x18.AuthorityStatus.values,
x19.EditorFormMode: x19.EditorFormMode.values
x12.ChangePasswordStatus: x12.ChangePasswordStatus.values,
x13.ForgotPasswordStatus: x13.ForgotPasswordStatus.values,
x14.LoginStatus: x14.LoginStatus.values,
x15.RegisterStatus: x15.RegisterStatus.values,
x16.DrawerStateStatus: x16.DrawerStateStatus.values,
x17.AuthorityStatus: x17.AuthorityStatus.values,
x18.EditorFormMode: x18.EditorFormMode.values,
x19.UserStatus: x19.UserStatus.values
});

Future<JsonMapper> initializeJsonMapperAsync({Iterable<JsonMapperAdapter> adapters = const [], SerializationOptions? serializationOptions, DeserializationOptions? deserializationOptions}) => Future(() => initializeJsonMapper(adapters: adapters, serializationOptions: serializationOptions, deserializationOptions: deserializationOptions));
Expand Down
29 changes: 11 additions & 18 deletions lib/presentation/screen/account/account_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class AccountScreen extends StatelessWidget {

Widget _buildBody(BuildContext context) {
return BlocBuilder<AccountBloc, AccountState>(
buildWhen: (previous, current) => previous.status != current.status, //previous.data != current.data ||
buildWhen: (previous, current) => previous.status != current.status,
builder: (context, state) {
return ResponsiveFormBuilder(
formKey: _formKey,
Expand All @@ -66,38 +66,31 @@ class AccountScreen extends StatelessWidget {
}

Widget _submitButton(BuildContext context, AccountState state) {
debugPrint('state.status: ${state.status}');
return ResponsiveSubmitButton(
onPressed: () => state.status == AccountStatus.loading ? null : _onSubmit(context, state),
isLoading: state.status == AccountStatus.loading,
);
}

void _onSubmit(BuildContext context, AccountState state) {
debugPrint("onSubmit");
FocusScope.of(context).unfocus();
if (!(_formKey.currentState?.validate() ?? false)) {
debugPrint("validate");
_showSnackBar(context, S.of(context).failed, const Duration(milliseconds: 1000));
return;
}

if (!(_formKey.currentState?.isDirty ?? false)) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(S.of(context).no_changes_made)),
);
debugPrint("no changes made");
_showSnackBar(context, S.of(context).no_changes_made, const Duration(milliseconds: 1000));
return;
}

if (_formKey.currentState?.saveAndValidate() ?? false) {
final formData = _formKey.currentState!.value;
final user = _createUserFromData(formData, state.data?.id);
context.read<AccountBloc>().add(AccountSubmitEvent(user));
if (state.status == AccountStatus.success) {
_formKey.currentState?.reset();
}

//context.read<UserBloc>().add(UserSubmitEvent(user));
// late final StreamSubscription<UserState> subscription;
// subscription = context.read<UserBloc>().stream.listen((userState) {
// if ((userState.status == UserStatus.success || userState.status == UserStatus.saveSuccess) && context.mounted) {
// context.read<AccountBloc>().add(const AccountFetchEvent());
// _formKey.currentState?.reset();
// subscription.cancel();
// }
// }); // cancel the stream after the first event
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ChangePasswordBloc extends Bloc<ChangePasswordEvent, ChangePasswordState>

ChangePasswordBloc({required AccountRepository repository})
: _repository = repository,
super(const ChangePasswordInitialState()) {
super(const ChangePasswordState(status: ChangePasswordStatus.initial)) {
on<ChangePasswordChanged>(_onSubmit);
}

Expand All @@ -31,19 +31,29 @@ class ChangePasswordBloc extends Bloc<ChangePasswordEvent, ChangePasswordState>

FutureOr<void> _onSubmit(ChangePasswordChanged event, Emitter<ChangePasswordState> emit) async {
_log.debug("BEGIN: changePassword bloc: _onSubmit");
emit(const ChangePasswordLoadingState());
emit(state.copyWith(status: ChangePasswordStatus.loading));
try {
if (event.currentPassword == "" || event.newPassword == "") {
emit(state.copyWith(status: ChangePasswordStatus.failure));
return;
}

if (event.currentPassword == event.newPassword) {
emit(state.copyWith(status: ChangePasswordStatus.failure));
return;
}

PasswordChangeDTO passwordChangeDTO = PasswordChangeDTO(
currentPassword: event.currentPassword,
newPassword: event.newPassword,
);
var result = await _repository.changePassword(passwordChangeDTO);
result < HttpStatus.badRequest
? emit(const ChangePasswordCompletedState())
: emit(const ChangePasswordErrorState(message: "Reset Password API Error"));
? emit(state.copyWith(status: ChangePasswordStatus.success))
: emit(state.copyWith(status: ChangePasswordStatus.failure));
_log.debug("END: changePassword bloc: _onSubmit success: {}", [result.toString()]);
} catch (e) {
emit(const ChangePasswordErrorState(message: "Reset Password Unhandled Error"));
emit(state.copyWith(status: ChangePasswordStatus.failure));
_log.error("END: changePassword bloc: _onSubmit error: {}", [e.toString()]);
}
}
Expand Down
Loading

0 comments on commit e26fbf3

Please sign in to comment.