Skip to content

Commit af94e7e

Browse files
committed
fix(app): revert user state on feed decorator status update failure
- Add local variable 'originalUser' to store the current user state - Update user feed decorator status and emit the updated state - Revert to original user state in case of backend update failure to maintain consistency
1 parent f135ef3 commit af94e7e

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,11 @@ class AppBloc extends Bloc<AppEvent, AppState> {
506506
Emitter<AppState> emit,
507507
) async {
508508
if (state.user != null && state.user!.id == event.userId) {
509+
final originalUser = state.user!;
509510
final now = DateTime.now();
510511
// Get the current status for the decorator, or create a default if not present.
511512
final currentStatus =
512-
state.user!.feedDecoratorStatus[event.feedDecoratorType] ??
513+
originalUser.feedDecoratorStatus[event.feedDecoratorType] ??
513514
const UserFeedDecoratorStatus(isCompleted: false);
514515

515516
// Create an updated status.
@@ -524,15 +525,15 @@ class AppBloc extends Bloc<AppEvent, AppState> {
524525
// Create a new map with the updated status for the specific decorator type.
525526
final newFeedDecoratorStatus =
526527
Map<FeedDecoratorType, UserFeedDecoratorStatus>.from(
527-
state.user!.feedDecoratorStatus,
528+
originalUser.feedDecoratorStatus,
528529
)..update(
529530
event.feedDecoratorType,
530531
(_) => updatedDecoratorStatus,
531532
ifAbsent: () => updatedDecoratorStatus,
532533
);
533534

534535
// Update the user with the new feedDecoratorStatus map.
535-
final updatedUser = state.user!.copyWith(
536+
final updatedUser = originalUser.copyWith(
536537
feedDecoratorStatus: newFeedDecoratorStatus,
537538
);
538539

@@ -552,8 +553,8 @@ class AppBloc extends Bloc<AppEvent, AppState> {
552553
);
553554
} catch (e) {
554555
print('Failed to update feed decorator status on backend: $e');
555-
// TODO(fulleni): handle the error, e.g., by reverting the state
556-
// or scheduling a retry. For now, we just log the error.
556+
// Revert the state on failure to maintain consistency.
557+
emit(state.copyWith(user: originalUser));
557558
}
558559
}
559560
}

0 commit comments

Comments
 (0)