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:
AmirReza Jamali
2026-05-30 18:46:13 +03:30
parent e6b4720dcd
commit 83b77c05ef
+18 -4
View File
@@ -104,10 +104,24 @@ class BoardViewModel with ChangeNotifier {
newOrder: newOrder, newOrder: newOrder,
); );
if (result is Error<MoveCardResponse>) { switch (result) {
_board = _board!.copyWith(columns: previousColumns); case Ok<MoveCardResponse>():
notifyListeners(); // 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;
} }
return result;
} }
} }