From e5a8745e0ef4f5abf5d26581942b068d68992e56 Mon Sep 17 00:00:00 2001 From: alzalia1 Date: Thu, 14 Aug 2025 11:19:06 +0200 Subject: [PATCH] feat: made the ongoing screen... for now --- lib/data/repositories/bal_repository.dart | 6 +++ lib/data/services/api_client.dart | 27 +++++++++++ .../bal_page/view_model/bal_view_model.dart | 12 +++++ .../widget/ongoing/bal_ongoing_screen.dart | 45 ++++++++++++++++++- .../widget/pending/bal_pending_screen.dart | 31 ++++++++++++- 5 files changed, 119 insertions(+), 2 deletions(-) diff --git a/lib/data/repositories/bal_repository.dart b/lib/data/repositories/bal_repository.dart index 14f4046..4a1f2dc 100644 --- a/lib/data/repositories/bal_repository.dart +++ b/lib/data/repositories/bal_repository.dart @@ -55,6 +55,12 @@ class BalRepository { false; } + Future> stopBal(int id) async { + final result = await _apiClient.stopBal(id); + _getBalsNoCache(); + return result; + } + Future> startBal(int id) async { if (isABalOngoing()) { return Result.error( diff --git a/lib/data/services/api_client.dart b/lib/data/services/api_client.dart index c093323..c9283bc 100644 --- a/lib/data/services/api_client.dart +++ b/lib/data/services/api_client.dart @@ -42,6 +42,33 @@ class ApiClient { * ================= */ + Future> stopBal(int id) async { + final client = Client(); + try { + final headers = await _getHeaders(); + final response = await client.post( + Uri.parse("https://$apiBasePath/bal/${id.toString()}/stop"), + headers: headers, + ); + if (response.statusCode == 200) { + final json = jsonDecode(response.body); + return Result.ok(Bal.fromJSON(json)); + } else if (response.statusCode == 403) { + throw "You don't own the specified BAL"; + } else if (response.statusCode == 404) { + throw "No BAL with specified ID found"; + } else if (response.statusCode == 409) { + throw "Selected BAL was not on ongoing state"; + } else { + throw "Unknown error"; + } + } catch (e) { + return Result.error(Exception(e)); + } finally { + client.close(); + } + } + Future> startBal(int id) async { final client = Client(); try { diff --git a/lib/ui/bal_page/view_model/bal_view_model.dart b/lib/ui/bal_page/view_model/bal_view_model.dart index 5dad1ac..0fa1857 100644 --- a/lib/ui/bal_page/view_model/bal_view_model.dart +++ b/lib/ui/bal_page/view_model/bal_view_model.dart @@ -17,6 +17,18 @@ class BalViewModel extends ChangeNotifier { Bal? get bal => _bal; bool isABalOngoing = false; + Future> stopBal(int id) async { + final result = await _balRepository.stopBal(id); + switch (result) { + case Ok(): + _bal = result.value; + notifyListeners(); + break; + default: + } + return result; + } + Future> startBal(int id) async { if (isABalOngoing) { return Result.error(Exception("Cannot have multiple BALs ongoing !")); diff --git a/lib/ui/bal_page/widget/ongoing/bal_ongoing_screen.dart b/lib/ui/bal_page/widget/ongoing/bal_ongoing_screen.dart index 09e427b..a9ca41e 100644 --- a/lib/ui/bal_page/widget/ongoing/bal_ongoing_screen.dart +++ b/lib/ui/bal_page/widget/ongoing/bal_ongoing_screen.dart @@ -12,7 +12,50 @@ class BalOngoingScreen extends StatelessWidget { return Scaffold( bottomNavigationBar: AppNavigationBar(startIndex: 0), appBar: AppBar(title: Text(viewModel.bal!.name)), - body: Center(child: Text("Ongoing BAL")), + body: Padding( + padding: const EdgeInsets.all(10.0), + child: Column( + children: [ + Center( + child: ElevatedButton( + onPressed: () { + showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: Text( + "Êtes-vous sûr·e de vouloir arrêter cette BAL ?", + ), + content: Text( + "Cette action est irréversible. Vous pourrez ensuite consulter des statistiques sur la BAL, mais ne pourrez plus ajouter ou vendre de livre qui en fasse partie.", + ), + actions: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: Text("Annuler"), + ), + TextButton( + onPressed: () async { + await viewModel.stopBal(viewModel.bal!.id); + if (context.mounted) { + Navigator.of(context).pop(); + } + }, + child: Text("Valider"), + ), + ], + ); + }, + ); + }, + child: Text("Arrêter la BAL"), + ), + ), + ], + ), + ), ); } } diff --git a/lib/ui/bal_page/widget/pending/bal_pending_screen.dart b/lib/ui/bal_page/widget/pending/bal_pending_screen.dart index 696abd5..c7af227 100644 --- a/lib/ui/bal_page/widget/pending/bal_pending_screen.dart +++ b/lib/ui/bal_page/widget/pending/bal_pending_screen.dart @@ -38,7 +38,36 @@ class BalPendingScreen extends StatelessWidget { ); } : () { - viewModel.startBal(viewModel.bal!.id); + showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: Text( + "Êtes-vous sûr·e de vouloir débuter cette BAL ?", + ), + content: Text( + "Cette action est irréversible. Vous pourrez ensuite intéragir avec la BAL, mais pas la remettre en mode \"À venir\"", + ), + actions: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: Text("Annuler"), + ), + TextButton( + onPressed: () async { + await viewModel.startBal(viewModel.bal!.id); + if (context.mounted) { + Navigator.of(context).pop(); + } + }, + child: Text("Valider"), + ), + ], + ); + }, + ); }, child: Text("Démarrer cette BAL"), ),