Skip to content

Commit

Permalink
fix: save account repository
Browse files Browse the repository at this point in the history
  • Loading branch information
cevheri committed Dec 30, 2024
1 parent aa447a1 commit b34aa82
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/data/repository/account_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class AccountRepository {
if (user.id == null || user.id!.isEmpty) {
throw BadRequestException(userIdNotNull);
}
user = user.copyWith(langKey: user.langKey ?? "en");
final httpResponse = await HttpUtils.postRequest<User>("/$_resource", user);
final response = HttpUtils.decodeUTF8(httpResponse.body.toString());
var result = User.fromJsonString(response)!;
Expand Down
5 changes: 1 addition & 4 deletions lib/presentation/common_blocs/account/account_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_bloc_advance/configuration/app_logger.dart';
import 'package:flutter_bloc_advance/configuration/local_storage.dart';
import 'package:flutter_bloc_advance/data/repository/user_repository.dart';

import '../../../data/models/user.dart';
import '../../../data/repository/account_repository.dart';
Expand All @@ -26,8 +25,6 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
on<AccountSubmitEvent>(_onSubmit);
}

final UserRepository _userRepository = UserRepository();

/// Load the current account.
FutureOr<void> _onFetchAccount(AccountFetchEvent event, Emitter<AccountState> emit) async {
_log.debug("BEGIN: getAccount bloc: _onLoad");
Expand All @@ -50,7 +47,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
_log.debug("BEGIN: onSubmit AccountSubmitEvent event: {}", [event.data.toString()]);
emit(state.copyWith(status: AccountStatus.loading));
try {
final user = await _userRepository.update(event.data);
final user = await _repository.update(event.data);
emit(state.copyWith(status: AccountStatus.success, data: user));
_log.debug("END:onSubmitAccountSubmitEvent event success: {}", [user.toString()]);
} catch (e) {
Expand Down
13 changes: 12 additions & 1 deletion lib/presentation/screen/account/account_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,16 @@ class AccountScreen extends StatelessWidget {
return BlocBuilder<AccountBloc, AccountState>(
buildWhen: (previous, current) => previous.status != current.status,
builder: (context, state) {
// final initialValues = {
// 'login': state.data?.login,
// 'firstName': state.data?.firstName,
// 'lastName': state.data?.lastName,
// 'email': state.data?.email
// };

return ResponsiveFormBuilder(
formKey: _formKey,
// initialValue: initialValues,
children: [..._buildFormFields(context, state), _submitButton(context, state)],
);
},
Expand All @@ -61,7 +69,7 @@ class AccountScreen extends StatelessWidget {
UserFormFields.firstNameField(context, state.data?.firstName),
UserFormFields.lastNameField(context, state.data?.lastName),
UserFormFields.emailField(context, state.data?.email),
UserFormFields.activatedField(context, state.data?.activated),
//UserFormFields.activatedField(context, state.data?.activated),
];
}

Expand Down Expand Up @@ -91,6 +99,8 @@ class AccountScreen extends StatelessWidget {
final formData = _formKey.currentState!.value;
final user = _createUserFromData(formData, state.data?.id);
context.read<AccountBloc>().add(AccountSubmitEvent(user));
_formKey.currentState?.save();
context.read<AccountBloc>().add(const AccountFetchEvent());
}
}

Expand All @@ -113,6 +123,7 @@ class AccountScreen extends StatelessWidget {
break;
case AccountStatus.success:
_showSnackBar(context, S.of(context).success, duration);
//_formKey.currentState?.reset();
break;
case AccountStatus.failure:
_showSnackBar(context, S.of(context).failed, duration);
Expand Down

0 comments on commit b34aa82

Please sign in to comment.