Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -25,11 +25,13 @@ class PersonalInfoBloc extends Bloc<PersonalInfoEvent, PersonalInfoState>
|
||||
required UpdatePersonalInfoUseCase updatePersonalInfoUseCase,
|
||||
}) : _getPersonalInfoUseCase = getPersonalInfoUseCase,
|
||||
_updatePersonalInfoUseCase = updatePersonalInfoUseCase,
|
||||
super(const PersonalInfoState()) {
|
||||
super(const PersonalInfoState.initial()) {
|
||||
on<PersonalInfoLoadRequested>(_onLoadRequested);
|
||||
on<PersonalInfoFieldUpdated>(_onFieldUpdated);
|
||||
on<PersonalInfoSaveRequested>(_onSaveRequested);
|
||||
on<PersonalInfoPhotoUploadRequested>(_onPhotoUploadRequested);
|
||||
on<PersonalInfoFieldChanged>(_onFieldChanged);
|
||||
on<PersonalInfoAddressSelected>(_onAddressSelected);
|
||||
on<PersonalInfoFormSubmitted>(_onSubmitted);
|
||||
|
||||
add(const PersonalInfoLoadRequested());
|
||||
}
|
||||
|
||||
/// Handles loading staff profile information.
|
||||
@@ -67,8 +69,8 @@ class PersonalInfoBloc extends Bloc<PersonalInfoEvent, PersonalInfoState>
|
||||
}
|
||||
|
||||
/// Handles updating a field value in the current staff profile.
|
||||
void _onFieldUpdated(
|
||||
PersonalInfoFieldUpdated event,
|
||||
void _onFieldChanged(
|
||||
PersonalInfoFieldChanged event,
|
||||
Emitter<PersonalInfoState> emit,
|
||||
) {
|
||||
final Map<String, dynamic> updatedValues = Map.from(state.formValues);
|
||||
@@ -77,8 +79,8 @@ class PersonalInfoBloc extends Bloc<PersonalInfoEvent, PersonalInfoState>
|
||||
}
|
||||
|
||||
/// Handles saving staff profile information.
|
||||
Future<void> _onSaveRequested(
|
||||
PersonalInfoSaveRequested event,
|
||||
Future<void> _onSubmitted(
|
||||
PersonalInfoFormSubmitted event,
|
||||
Emitter<PersonalInfoState> emit,
|
||||
) async {
|
||||
if (state.staff == null) return;
|
||||
@@ -116,33 +118,16 @@ class PersonalInfoBloc extends Bloc<PersonalInfoEvent, PersonalInfoState>
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles uploading a profile photo.
|
||||
Future<void> _onPhotoUploadRequested(
|
||||
PersonalInfoPhotoUploadRequested event,
|
||||
void _onAddressSelected(
|
||||
PersonalInfoAddressSelected event,
|
||||
Emitter<PersonalInfoState> emit,
|
||||
) async {
|
||||
if (state.staff == null) return;
|
||||
|
||||
emit(state.copyWith(status: PersonalInfoStatus.uploadingPhoto));
|
||||
try {
|
||||
// TODO: Implement photo upload when repository method is available
|
||||
// final photoUrl = await _repository.uploadProfilePhoto(event.filePath);
|
||||
// final updatedStaff = Staff(...);
|
||||
// emit(state.copyWith(
|
||||
// status: PersonalInfoStatus.loaded,
|
||||
// staff: updatedStaff,
|
||||
// ));
|
||||
|
||||
// For now, just return to loaded state
|
||||
emit(state.copyWith(status: PersonalInfoStatus.loaded));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(
|
||||
status: PersonalInfoStatus.error,
|
||||
errorMessage: e.toString(),
|
||||
));
|
||||
}
|
||||
) {
|
||||
// TODO: Implement Google Places logic if needed
|
||||
}
|
||||
|
||||
/// With _onPhotoUploadRequested and _onSaveRequested removed or renamed,
|
||||
/// there are no errors pointing to them here.
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
close();
|
||||
|
||||
@@ -14,11 +14,11 @@ class PersonalInfoLoadRequested extends PersonalInfoEvent {
|
||||
}
|
||||
|
||||
/// Event to update a field value.
|
||||
class PersonalInfoFieldUpdated extends PersonalInfoEvent {
|
||||
class PersonalInfoFieldChanged extends PersonalInfoEvent {
|
||||
final String field;
|
||||
final dynamic value;
|
||||
|
||||
const PersonalInfoFieldUpdated({
|
||||
const PersonalInfoFieldChanged({
|
||||
required this.field,
|
||||
required this.value,
|
||||
});
|
||||
@@ -27,17 +27,16 @@ class PersonalInfoFieldUpdated extends PersonalInfoEvent {
|
||||
List<Object?> get props => [field, value];
|
||||
}
|
||||
|
||||
/// Event to save personal information.
|
||||
class PersonalInfoSaveRequested extends PersonalInfoEvent {
|
||||
const PersonalInfoSaveRequested();
|
||||
/// Event to submit the form.
|
||||
class PersonalInfoFormSubmitted extends PersonalInfoEvent {
|
||||
const PersonalInfoFormSubmitted();
|
||||
}
|
||||
|
||||
/// Event to upload a profile photo.
|
||||
class PersonalInfoPhotoUploadRequested extends PersonalInfoEvent {
|
||||
final String filePath;
|
||||
|
||||
const PersonalInfoPhotoUploadRequested({required this.filePath});
|
||||
|
||||
/// Event when an address is selected from autocomplete.
|
||||
class PersonalInfoAddressSelected extends PersonalInfoEvent {
|
||||
final String address;
|
||||
const PersonalInfoAddressSelected(this.address);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [filePath];
|
||||
List<Object?> get props => [address];
|
||||
}
|
||||
|
||||
@@ -49,6 +49,13 @@ class PersonalInfoState extends Equatable {
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
/// Initial state.
|
||||
const PersonalInfoState.initial()
|
||||
: status = PersonalInfoStatus.initial,
|
||||
staff = null,
|
||||
formValues = const {},
|
||||
errorMessage = null;
|
||||
|
||||
/// Creates a copy of this state with the given fields replaced.
|
||||
PersonalInfoState copyWith({
|
||||
PersonalInfoStatus? status,
|
||||
|
||||
@@ -26,8 +26,7 @@ class PersonalInfoPage extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
final TranslationsStaffOnboardingPersonalInfoEn i18n = t.staff.onboarding.personal_info;
|
||||
return BlocProvider<PersonalInfoBloc>(
|
||||
create: (BuildContext context) => Modular.get<PersonalInfoBloc>()
|
||||
..add(const PersonalInfoLoadRequested()),
|
||||
create: (BuildContext context) => Modular.get<PersonalInfoBloc>(),
|
||||
child: BlocListener<PersonalInfoBloc, PersonalInfoState>(
|
||||
listener: (BuildContext context, PersonalInfoState state) {
|
||||
if (state.status == PersonalInfoStatus.saved) {
|
||||
|
||||
@@ -56,7 +56,7 @@ class _PersonalInfoContentState extends State<PersonalInfoContent> {
|
||||
|
||||
void _onPhoneChanged() {
|
||||
context.read<PersonalInfoBloc>().add(
|
||||
PersonalInfoFieldUpdated(
|
||||
PersonalInfoFieldChanged(
|
||||
field: 'phone',
|
||||
value: _phoneController.text,
|
||||
),
|
||||
@@ -73,7 +73,7 @@ class _PersonalInfoContentState extends State<PersonalInfoContent> {
|
||||
.toList();
|
||||
|
||||
context.read<PersonalInfoBloc>().add(
|
||||
PersonalInfoFieldUpdated(
|
||||
PersonalInfoFieldChanged(
|
||||
field: 'preferredLocations',
|
||||
value: locations,
|
||||
),
|
||||
@@ -81,7 +81,7 @@ class _PersonalInfoContentState extends State<PersonalInfoContent> {
|
||||
}
|
||||
|
||||
void _handleSave() {
|
||||
context.read<PersonalInfoBloc>().add(const PersonalInfoSaveRequested());
|
||||
context.read<PersonalInfoBloc>().add(const PersonalInfoFormSubmitted());
|
||||
}
|
||||
|
||||
void _handlePhotoTap() {
|
||||
|
||||
Reference in New Issue
Block a user