From fbb8ee7ab65714fa9bd2d39f33663cc8744976d9 Mon Sep 17 00:00:00 2001 From: Erick Date: Fri, 20 Sep 2024 00:04:32 +0300 Subject: [PATCH] feat: deleting academia account now works! --- lib/controllers/user_controller.dart | 5 ++++ lib/models/services/user_service.dart | 24 ++++++++++++++++++ lib/pages/settings_page.dart | 36 ++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/lib/controllers/user_controller.dart b/lib/controllers/user_controller.dart index fbee968..47338e8 100644 --- a/lib/controllers/user_controller.dart +++ b/lib/controllers/user_controller.dart @@ -153,4 +153,9 @@ class UserController extends GetxController { debugPrint("Error during logout: ${e.toString()}"); } } + + Future> deleteUser() async { + final result = await service.deleteStudent(authHeaders, user.value!.id!); + return result; + } } diff --git a/lib/models/services/user_service.dart b/lib/models/services/user_service.dart index 9b85b3c..f2646c7 100644 --- a/lib/models/services/user_service.dart +++ b/lib/models/services/user_service.dart @@ -34,6 +34,30 @@ class UserService with VerisafeService { } } + Future> deleteStudent( + Map authheaders, String userID) async { + try { + final response = await http.delete( + Uri.parse("${VerisafeService.urlPrefix}/students/delete/$userID"), + headers: authheaders, + ); + + if (response.statusCode == 200) { + return right(true); + } + + return const Left( + "We ran into an error please retry again later!", + ); + } catch (e) { + if (e is http.ClientException) { + return const Left( + "Error communicating to server please check your network and try again later"); + } + return Left(e.toString()); + } + } + /// Register /// Registers a user to verisafe Future> register(Map data) async { diff --git a/lib/pages/settings_page.dart b/lib/pages/settings_page.dart index 96f8c78..11f2fe0 100644 --- a/lib/pages/settings_page.dart +++ b/lib/pages/settings_page.dart @@ -192,7 +192,41 @@ class _SettingsPageState extends State { ListTile( leading: const Icon(Ionicons.trash), title: const Text("Delete my account"), - onTap: () {}, + onTap: () { + showDialog( + context: context, + builder: (context) => AlertDialog( + title: const Text("Confirmation"), + content: const Text( + "Are you sure you want to delete this acount? Doing this will result to data loss and cannot be restored!", + ), + actions: [ + OutlinedButton( + onPressed: () { + Navigator.pop(context); + }, + child: const Text("Cancel"), + ), + FilledButton( + onPressed: () async { + await HapticFeedback.heavyImpact(); + await settingsController.logout(); + await userController.deleteUser(); + if (context.mounted) { + Navigator.pop(context); + Navigator.of(context).pushReplacement( + MaterialPageRoute( + builder: (context) => const Academia(), + ), + ); + } + }, + child: const Text("Yes delete it"), + ) + ], + ), + ); + }, ), Padding( padding: const EdgeInsets.all(12),