Refactor card movement logic in BoardViewModel to improve error handling and success validation
- Updated the handling of card movement results to differentiate between successful and unsuccessful API responses. - Implemented rollback of board state and error messaging for failed moves, ensuring a more robust user experience. - Enhanced the notification system to surface relevant error messages when card movements fail.
This commit is contained in:
@@ -104,10 +104,24 @@ class BoardViewModel with ChangeNotifier {
|
||||
newOrder: newOrder,
|
||||
);
|
||||
|
||||
if (result is Error<MoveCardResponse>) {
|
||||
_board = _board!.copyWith(columns: previousColumns);
|
||||
notifyListeners();
|
||||
}
|
||||
switch (result) {
|
||||
case Ok<MoveCardResponse>():
|
||||
// HTTP 200 alone isn't success: the API can return
|
||||
// {"success": false, "message": ...} (e.g. column over-limit,
|
||||
// permission denied). Only keep the optimistic move when the body
|
||||
// confirms success; otherwise roll back and surface the message.
|
||||
if (result.value.success == true) {
|
||||
return result;
|
||||
}
|
||||
_board = _board!.copyWith(columns: previousColumns);
|
||||
notifyListeners();
|
||||
return Result.error(
|
||||
Exception(result.value.message ?? 'Failed to move card'),
|
||||
);
|
||||
case Error<MoveCardResponse>():
|
||||
_board = _board!.copyWith(columns: previousColumns);
|
||||
notifyListeners();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user