-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #745 from givtnl/feature/kids-838-implement-edit-a…
…llowance-screen Feature/kids 838 implement edit allowance screen
- Loading branch information
Showing
20 changed files
with
489 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 68 additions & 7 deletions
75
lib/features/children/details/cubit/child_details_cubit.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,97 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:bloc/bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:givt_app/core/enums/amplitude_events.dart'; | ||
import 'package:givt_app/core/logging/logging.dart'; | ||
import 'package:givt_app/features/children/details/models/profile_ext.dart'; | ||
import 'package:givt_app/features/children/details/repositories/child_details_repository.dart'; | ||
import 'package:givt_app/features/children/edit_child/repositories/edit_child_repository.dart'; | ||
import 'package:givt_app/features/children/overview/models/profile.dart'; | ||
import 'package:givt_app/utils/analytics_helper.dart'; | ||
|
||
part 'child_details_state.dart'; | ||
|
||
class ChildDetailsCubit extends Cubit<ChildDetailsState> { | ||
ChildDetailsCubit( | ||
this._childDetailsRepository, | ||
this._editChildRepository, | ||
this._profile, | ||
) : super(const ChildDetailsInitialState()); | ||
|
||
final ChildDetailsRepository _childDetailsRepository; | ||
final EditChildRepository _editChildRepository; | ||
final Profile _profile; | ||
ProfileExt? _profileExt; | ||
|
||
Future<void> fetchChildDetails() async { | ||
emit(const ChildDetailsFetchingState()); | ||
_emitLoading(); | ||
try { | ||
final response = await _childDetailsRepository.fetchChildDetails( | ||
_profileExt = await _childDetailsRepository.fetchChildDetails( | ||
_profile, | ||
); | ||
emit( | ||
ChildDetailsFetchedState( | ||
profileDetails: response, | ||
), | ||
); | ||
_emitData(); | ||
} catch (error, stackTrace) { | ||
await LoggingInfo.instance | ||
.error(error.toString(), methodName: stackTrace.toString()); | ||
emit(ChildDetailsErrorState(errorMessage: error.toString())); | ||
} | ||
} | ||
|
||
void _emitLoading() { | ||
emit(const ChildDetailsFetchingState()); | ||
} | ||
|
||
void _emitData() { | ||
emit( | ||
ChildDetailsFetchedState( | ||
profileDetails: _profileExt!, | ||
), | ||
); | ||
} | ||
|
||
Future<void> updateAllowance(int allowance) async { | ||
await _logConfirmUpdateAllowanceEvent(allowance); | ||
|
||
try { | ||
_emitLoading(); | ||
final isSuccess = await _editChildRepository.editChildAllowance( | ||
_profile.id, | ||
allowance, | ||
); | ||
if (isSuccess) { | ||
_emitData(); | ||
emit(ChildEditGivingAllowanceSuccessState(allowance: allowance)); | ||
//retrieve updated profile after changing the allowance | ||
unawaited(fetchChildDetails()); | ||
} else { | ||
emit( | ||
const ChildDetailsErrorState( | ||
errorMessage: 'Failed to update allowance', | ||
), | ||
); | ||
} | ||
} catch (e, s) { | ||
_emitData(); | ||
await _handleEditAllowanceApiError(e, s); | ||
} | ||
} | ||
|
||
Future<void> _handleEditAllowanceApiError(Object e, StackTrace s) async { | ||
await LoggingInfo.instance.error(e.toString(), methodName: s.toString()); | ||
emit( | ||
ChildDetailsErrorState(errorMessage: e.toString()), | ||
); | ||
} | ||
|
||
Future<void> _logConfirmUpdateAllowanceEvent(int allowance) async { | ||
await AnalyticsHelper.logEvent( | ||
eventName: AmplitudeEvents.childEditMonthlyAllowanceSaveClicked, | ||
eventProperties: { | ||
'child_name': _profile.firstName, | ||
'new_giving_allowance': allowance, | ||
'old_giving_allowance': _profileExt?.givingAllowance.amount, | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.