Skip to content

Commit

Permalink
refactor: new user structure completed. user_list, user_editor(create…
Browse files Browse the repository at this point in the history
…, view, edit modes)
  • Loading branch information
cevheri committed Dec 18, 2024
1 parent e527a50 commit 16db4c1
Show file tree
Hide file tree
Showing 8 changed files with 468 additions and 671 deletions.
7 changes: 0 additions & 7 deletions lib/data/models/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ class User extends Equatable {
@JsonProperty(name: 'authorities')
final List<String>? authorities;

// @JsonProperty(name: 'phoneNumber')
// final String? phoneNumber;

//Constructor
const User({
this.id,
Expand All @@ -61,7 +58,6 @@ class User extends Equatable {
this.lastModifiedBy,
this.lastModifiedDate,
this.authorities,
// this.phoneNumber,
});

/// CopyWith method to create a new instance of the User class with new values
Expand All @@ -78,7 +74,6 @@ class User extends Equatable {
String? lastModifiedBy,
DateTime? lastModifiedDate,
List<String>? authorities,
// String? phoneNumber,
}) {
return User(
id: id ?? this.id,
Expand All @@ -93,7 +88,6 @@ class User extends Equatable {
lastModifiedBy: lastModifiedBy ?? this.lastModifiedBy,
lastModifiedDate: lastModifiedDate ?? this.lastModifiedDate,
authorities: authorities ?? this.authorities,
// phoneNumber: phoneNumber ?? this.phoneNumber,
);
}

Expand Down Expand Up @@ -133,7 +127,6 @@ class User extends Equatable {
lastModifiedBy,
lastModifiedDate,
authorities,
// phoneNumber,
];

@override
Expand Down
9 changes: 5 additions & 4 deletions lib/presentation/screen/components/user_form_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import 'package:form_builder_validators/form_builder_validators.dart';
/// This class contains the user form fields that are used in the user form.
/// The user form fields are used to display the user form fields in the user form.
class UserFormFields {

static _requiredValidator(BuildContext context) {
return FormBuilderValidators.required(errorText: S.of(context).required_field);
}

static _txtValidator(BuildContext context) {
return [
_requiredValidator(context),
Expand Down Expand Up @@ -106,11 +106,12 @@ class UserFormFields {
/// [context] BuildContext current context
/// [initialValue] List<String>? initial value of the field
/// [enabled] bool enable the field default is true
static Widget authoritiesField(BuildContext context, List<String>? initialValue, {bool enabled = true}) => FormBuilderDropdown<String>(
static Widget authoritiesField(BuildContext context, List<String?>? initialValue, {bool enabled = true}) => FormBuilderDropdown<String>(
key: const Key('userEditorAuthoritiesFieldKey'),
name: 'authorities',
decoration: InputDecoration(labelText: S.of(context).authorities, enabled: enabled),
items: initialValue?.map((e) => DropdownMenuItem(value: e, child: Text(e))).toList() ?? [],
enabled: enabled,
decoration: InputDecoration(labelText: S.of(context).authorities),
items: initialValue?.map((e) => DropdownMenuItem(value: e, child: Text(e ?? ''))).toList() ?? [],
validator: FormBuilderValidators.compose([_requiredValidator(context)]),
);
}
127 changes: 49 additions & 78 deletions lib/presentation/screen/user/bloc/user_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,130 +20,101 @@ class UserBloc extends Bloc<UserEvent, UserState> {
}) : _repository = repository,
super(const UserState()) {
on<UserEvent>((event, emit) {});
// on<UserCreate>(_onCreate);
on<UserSearch>(_onSearch);
// on<UserEditEvent>(_onEdit);
on<UserList>(_onList);
on<UserSearchEvent>(_onSearch);
on<UserFetchEvent>(_onFetchUser);
on<UserDeleteEvent>(_onDelete);
on<UserEditorInit>(_onEditorInit);
on<UserSubmitEvent>(_onSubmit);
on<UserViewCompleteEvent>(_onViewComplete);
on<UserSaveCompleteEvent>(_onSaveComplete);
}

/// Initialize the UserEditor.
FutureOr<void> _onEditorInit(UserEditorInit event, Emitter<UserState> emit) async {
_log.debug("BEGIN: onEditorInit UserEditorInit event: {}", []);
emit(const UserInitialState());
emit(const UserState());
_log.debug("END:onEditorInit UserEditorInit event success: {}", []);
}

/// Submit an entity in the EditorForm
FutureOr<void> _onSubmit(UserSubmitEvent event, Emitter<UserState> emit) async {
_log.debug("BEGIN: onSubmit UserSubmitEvent event: {}", [event.user.toString()]);
emit(state.copyWith(status: UserStatus.loading));
try {
final user = event.user.id == null ? await _repository.create(event.user) : await _repository.update(event.user);
emit(state.copyWith(status: UserStatus.success, data: user));
emit(state.copyWith(status: UserStatus.saveSuccess, data: user));
_log.debug("END:onSubmit UserSubmitEvent event success: {}", [user.toString()]);
} catch (e) {
emit(state.copyWith(status: UserStatus.failure));
_log.error("END:onSubmit UserSubmitEvent event error: {}", [e.toString()]);
}
}


/// Delete a user.
FutureOr<void> _onDelete(UserDeleteEvent event, Emitter<UserState> emit) async {
_log.debug("BEGIN: onDelete UserDelete event: {}", [event.id]);
emit(const UserState(status: UserStatus.loading));
try {
if (event.id == "user-1") {
emit(state.copyWith(status: UserStatus.failure, err: "Admin user cannot be deleted"));
_log.error("END:onDelete UserDelete event error: {}", ["Admin user cannot be deleted"]);
}
await _repository.deleteUser(event.id);
emit(state.copyWith(status: UserStatus.deleteSuccess));
_log.debug("END:onDelete UserDelete event success: {}", [event.id]);
} catch (e) {
emit(state.copyWith(status: UserStatus.failure, err: e.toString()));
_log.error("END:onDelete UserDelete event error: {}", [e.toString()]);
}
}

/// Retrieve a user by id.
FutureOr<void> _onFetchUser(UserFetchEvent event, Emitter<UserState> emit) async {
_log.debug("BEGIN: onFetchUser FetchUserEvent event: {}", [event.id]);
emit(const UserLoadInProgressState());
emit(const UserState(status: UserStatus.loading));
try {
var user = await _repository.getUser(event.id);
emit(UserLoadSuccessState(userLoadSuccess: user!));
_log.debug("END:onFetchUser FetchUserEvent event success: {}", [user.toString()]);
final entity = await _repository.getUser(event.id);
emit(state.copyWith(status: UserStatus.fetchSuccess, data: entity));
_log.debug("END:onFetchUser FetchUserEvent event success: {}", [entity.toString()]);
} catch (e) {
emit(UserLoadFailureState(message: e.toString()));
emit(state.copyWith(status: UserStatus.failure, err: e.toString()));
_log.error("END:onFetchUser FetchUserEvent event error: {}", [e.toString()]);
}
}

// /// Create a new user.
// FutureOr<void> _onCreate(UserCreate event, Emitter<UserState> emit) async {
// _log.debug("BEGIN: onCreate UserCreate event: {}", [event.user.toString()]);
// emit(const UserInitialState());
// try {
// var user = await _repository.create(event.user);
// emit(UserLoadSuccessState(userLoadSuccess: user!));
// _log.debug("END:onCreate UserCreate event success: {}", [user.toString()]);
// } catch (e) {
// emit(UserLoadFailureState(message: e.toString()));
// _log.error("END:onCreate UserCreate event error: {}", [e.toString()]);
// }
// }

/// Search a user by name or authority.
FutureOr<void> _onSearch(UserSearch event, Emitter<UserState> emit) async {
FutureOr<void> _onSearch(UserSearchEvent event, Emitter<UserState> emit) async {
_log.debug("BEGIN: onSearch UserSearch event: {}", [event.name]);
emit(const UserFindInitialState());
emit(state.copyWith(status: UserStatus.loading));
try {
if (event.name == "") {
List<User> user = await _repository.findUserByAuthority(event.rangeStart, event.rangeEnd, event.authority);
emit(UserSearchSuccessState(userList: user));
_log.debug("END:onSearch UserSearch event without name success: {}", [user.toString()]);
final entities = await _repository.findUserByAuthority(event.page, event.size, event.authority);
emit(state.copyWith(status: UserStatus.searchSuccess, userList: entities));
_log.debug("END:onSearch UserSearch event success content count: {}", [entities.length]);
}
if (event.name != "") {
List<User> user = await _repository.findUserByName(event.rangeStart, event.rangeEnd, event.name, event.authority);
emit(UserSearchSuccessState(userList: user));
_log.debug("END:onSearch UserSearch event with name success: {}", [user.toString()]);
final entities = await _repository.findUserByName(event.page, event.size, event.name, event.authority);
emit(state.copyWith(status: UserStatus.searchSuccess, userList: entities));
_log.debug("END:onSearch UserSearch event with name success content count: {}", [entities.length]);
}
} catch (e) {
emit(UserSearchFailureState(message: e.toString()));
emit(state.copyWith(status: UserStatus.failure, err: e.toString()));
_log.error("END:onSearch UserSearch event error: {}", [e.toString()]);
}
}

/// List all users.
FutureOr<void> _onList(UserList event, Emitter<UserState> emit) async {
_log.debug("BEGIN: onList UserList event: {}", []);
emit(const UserListInitialState());
try {
List<User> user = await _repository.listUser(0, 100);
emit(UserListSuccessState(userList: user));
_log.debug("END:onList UserList event success: {}", [user.toString()]);
} catch (e) {
emit(UserListFailureState(message: e.toString()));
_log.error("END:onList UserList event error: {}", [e.toString()]);
}
/// save screen completed
FutureOr<void> _onSaveComplete(UserSaveCompleteEvent event, Emitter<UserState> emit) async {
_log.debug("BEGIN: onSaveComplete UserSaveCompleteEvent event: {}", []);
emit(state.copyWith(status: UserStatus.saveSuccess));
_log.debug("END:onSaveComplete UserSaveCompleteEvent event success: {}", []);
}

// /// Update a user.
// FutureOr<void> _onEdit(UserEditEvent event, Emitter<UserState> emit) async {
// _log.debug("BEGIN: onEdit UserEdit event: {}", [event.user.toString()]);
// emit(const UserEditInitialState());
// try {
// var user = await _repository.update(event.user);
// emit(UserEditSuccessState(userEditSuccess: user!));
// _log.debug("END:onEdit UserEdit event success: {}", [user.toString()]);
// } catch (e) {
// emit(UserEditFailureState(message: e.toString()));
// _log.error("END:onEdit UserEdit event error: {}", [e.toString()]);
// }
// }

/// Delete a user.
FutureOr<void> _onDelete(UserDeleteEvent event, Emitter<UserState> emit) async {
_log.debug("BEGIN: onDelete UserDelete event: {}", [event.id]);
emit(const UserDeleteLoadingState());
try {
if(event.id == "user-1") {
emit(const UserDeleteFailureState(message: "Admin user cannot be deleted"));
_log.error("END:onDelete UserDelete event error: {}", ["Admin user cannot be deleted"]);
}
await _repository.deleteUser(event.id);
emit(const UserDeleteSuccessState());
_log.debug("END:onDelete UserDelete event success: {}", [event.id]);
} catch (e) {
emit(UserDeleteFailureState(message: e.toString()));
_log.error("END:onDelete UserDelete event error: {}", [e.toString()]);
}
/// View screen completed
FutureOr<void> _onViewComplete(UserViewCompleteEvent event, Emitter<UserState> emit) async {
_log.debug("BEGIN: onViewComplete UserViewCompleteEvent event: {}", []);
emit(state.copyWith(status: UserStatus.viewSuccess));
_log.debug("END:onViewComplete UserViewCompleteEvent event success: {}", []);
}

}
62 changes: 23 additions & 39 deletions lib/presentation/screen/user/bloc/user_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ class UserEvent extends Equatable {
List<Object> get props => [];
}

class UserSearch extends UserEvent {
final int rangeStart;
final int rangeEnd;
class UserSearchEvent extends UserEvent {
final int page;
final int size;
final String authority;
final String name;

const UserSearch(
this.rangeStart,
this.rangeEnd,
this.authority,
this.name,
);
const UserSearchEvent({
this.page = 0,
this.size = 10,
this.authority = "",
this.name = "",
});
}

class UserEditorInit extends UserEvent {
Expand All @@ -37,36 +37,6 @@ class UserSubmitEvent extends UserEvent {
List<Object> get props => [user];
}


// class UserCreate extends UserEvent {
// const UserCreate({required this.user});
//
// final User user;
//
// @override
// List<Object> get props => [];
// }
//
// class UserSaveEvent extends UserEvent {
// const UserSaveEvent({required this.user});
//
// final User user;
//
// @override
// List<Object> get props => [];
// }
//
// class UserEditEvent extends UserEvent {
// const UserEditEvent({required this.user});
//
// final User user;
//
// @override
// List<Object> get props => [];
// }

class UserList extends UserEvent {}

class UserFetchEvent extends UserEvent {
final String id;

Expand All @@ -84,3 +54,17 @@ class UserDeleteEvent extends UserEvent {
@override
List<Object> get props => [id];
}

class UserSaveCompleteEvent extends UserEvent {
const UserSaveCompleteEvent();

@override
List<Object> get props => [];
}

class UserViewCompleteEvent extends UserEvent {
const UserViewCompleteEvent();

@override
List<Object> get props => [];
}
Loading

0 comments on commit 16db4c1

Please sign in to comment.