From bfd959a5df94c4c7a237f8aa45370f8e86ed9430 Mon Sep 17 00:00:00 2001 From: akhdanre Date: Fri, 23 May 2025 13:42:51 +0700 Subject: [PATCH] fix: notification setup --- .../question/true_false_question_model.dart | 4 +- lib/data/services/auth_service.dart | 53 +++++++++++-------- .../detail_quiz/view/detail_quix_view.dart | 2 +- .../login/controllers/login_controller.dart | 8 +-- .../controller/register_controller.dart | 19 ++++--- 5 files changed, 52 insertions(+), 34 deletions(-) diff --git a/lib/data/models/quiz/question/true_false_question_model.dart b/lib/data/models/quiz/question/true_false_question_model.dart index 0a0717e..af01464 100644 --- a/lib/data/models/quiz/question/true_false_question_model.dart +++ b/lib/data/models/quiz/question/true_false_question_model.dart @@ -11,11 +11,13 @@ class TrueFalseQuestion extends BaseQuestionModel { }) : super(type: 'true_false'); factory TrueFalseQuestion.fromJson(Map json) { + print(json['target_answer']); + return TrueFalseQuestion( index: json['index'], question: json['question'], duration: json['duration'], - targetAnswer: json['target_answer'], + targetAnswer: json['target_answer'].toString().toLowerCase() == 'true', ); } diff --git a/lib/data/services/auth_service.dart b/lib/data/services/auth_service.dart index 1922006..cf1de61 100644 --- a/lib/data/services/auth_service.dart +++ b/lib/data/services/auth_service.dart @@ -17,47 +17,58 @@ class AuthService extends GetxService { } Future register(RegisterRequestModel request) async { - var data = await dio.post( - APIEndpoint.register, - data: request.toJson(), - ); - if (data.statusCode == 200) { - return true; - } else { - throw Exception("Registration failed"); + try { + final response = await dio.post( + APIEndpoint.register, + data: request.toJson(), + ); + + return response.statusCode == 200; + } on DioException catch (e) { + if (e.response?.statusCode == 409) { + // Status 409 = Conflict = User already exists + throw Exception("Email sudah dipakai"); + } + + // Other Dio errors + final errorMessage = e.response?.data['message'] ?? "Pendaftaran gagal"; + throw Exception(errorMessage); + } catch (e) { + throw Exception("Terjadi kesalahan saat mendaftar"); } } Future loginWithEmail(LoginRequestModel request) async { - final data = request.toJson(); - final response = await dio.post(APIEndpoint.login, data: data); + try { + final data = request.toJson(); + final response = await dio.post(APIEndpoint.login, data: data); - if (response.statusCode == 200) { - print(response.data); final baseResponse = BaseResponseModel.fromJson( response.data, (json) => LoginResponseModel.fromJson(json), ); return baseResponse.data!; - } else { - throw Exception("Login failed"); + } on DioException catch (e) { + final errorMessage = e.response?.data['message'] ?? "Login gagal"; + throw Exception(errorMessage); } } Future loginWithGoogle(String idToken) async { - final response = await dio.post( - APIEndpoint.loginGoogle, - data: {"token_id": idToken}, - ); + try { + final response = await dio.post( + APIEndpoint.loginGoogle, + data: {"token_id": idToken}, + ); - if (response.statusCode == 200) { final baseResponse = BaseResponseModel.fromJson( response.data, (json) => LoginResponseModel.fromJson(json), ); return baseResponse.data!; - } else { - throw Exception("Google login failed"); + } on DioException catch (e) { + final errorMessage = e.response?.data['message'] ?? "Login Google gagal"; + throw Exception(errorMessage); } } } diff --git a/lib/feature/detail_quiz/view/detail_quix_view.dart b/lib/feature/detail_quiz/view/detail_quix_view.dart index 5042b96..b597eda 100644 --- a/lib/feature/detail_quiz/view/detail_quix_view.dart +++ b/lib/feature/detail_quiz/view/detail_quix_view.dart @@ -77,7 +77,7 @@ class DetailQuizView extends GetView { GlobalButton(text: "Kerjakan", onPressed: controller.goToPlayPage), const SizedBox(height: 20), - GlobalButton(text: "buat ruangan", onPressed: () {}), + // GlobalButton(text: "buat ruangan", onPressed: () {}), const SizedBox(height: 20), const Divider(thickness: 1.2, color: AppColors.borderLight), diff --git a/lib/feature/login/controllers/login_controller.dart b/lib/feature/login/controllers/login_controller.dart index e512f05..682200b 100644 --- a/lib/feature/login/controllers/login_controller.dart +++ b/lib/feature/login/controllers/login_controller.dart @@ -55,7 +55,7 @@ class LoginController extends GetxController { final password = passwordController.text.trim(); if (email.isEmpty || password.isEmpty) { - Get.snackbar("Error", "Email and password are required"); + Get.snackbar("Kesalahan", "Email dan kata sandi wajib diisi"); return; } @@ -75,7 +75,7 @@ class LoginController extends GetxController { Get.offAllNamed(AppRoutes.mainPage); } catch (e, stackTrace) { logC.e(e, stackTrace: stackTrace); - CustomNotification.error(title: "failed", message: "Check username and password"); + CustomNotification.error(title: "Gagal", message: "Periksa kembali email dan kata sandi Anda"); } finally { isLoading.value = false; } @@ -86,13 +86,13 @@ class LoginController extends GetxController { try { final user = await _googleAuthService.signIn(); if (user == null) { - Get.snackbar("Error", "Google Sign-In canceled"); + Get.snackbar("Kesalahan", "Masuk dengan Google dibatalkan"); return; } final idToken = await user.authentication.then((auth) => auth.idToken); if (idToken == null || idToken.isEmpty) { - Get.snackbar("Error", "No ID Token received."); + Get.snackbar("Kesalahan", "Tidak menerima ID Token dari Google"); return; } diff --git a/lib/feature/register/controller/register_controller.dart b/lib/feature/register/controller/register_controller.dart index 010cec3..79b2db1 100644 --- a/lib/feature/register/controller/register_controller.dart +++ b/lib/feature/register/controller/register_controller.dart @@ -37,26 +37,27 @@ class RegisterController extends GetxController { String phone = phoneController.text.trim(); if (email.isEmpty || password.isEmpty || confirmPassword.isEmpty || name.isEmpty || birthDate.isEmpty) { - Get.snackbar("Error", "All fields are required"); + CustomNotification.error(title: "Kesalahan", message: "Semua data harus diisi"); return; } if (!_isValidEmail(email)) { - Get.snackbar("Error", "Invalid email format"); + CustomNotification.error(title: "Kesalahan", message: "Format email tidak valid"); return; } if (!_isValidDateFormat(birthDate)) { - Get.snackbar("Error", "Invalid date format. Use dd-mm-yyyy"); + CustomNotification.error(title: "Kesalahan", message: "Format tanggal tidak valid. Gunakan format seperti ini 12-09-2003"); return; } + if (password != confirmPassword) { - Get.snackbar("Error", "Passwords do not match"); + CustomNotification.error(title: "Kesalahan", message: "Kata sandi tidak cocok"); return; } if (phone.isNotEmpty && (phone.length < 10 || phone.length > 13)) { - Get.snackbar("Error", "Phone number must be between 10 and 13 digits"); + CustomNotification.error(title: "Kesalahan", message: "Nomor telepon harus terdiri dari 10 hingga 13 digit"); return; } @@ -74,9 +75,13 @@ class RegisterController extends GetxController { Get.back(); CustomFloatingLoading.hideLoadingDialog(Get.context!); - CustomNotification.success(title: "register success", message: "created account successfuly"); + CustomNotification.success(title: "Pendaftaran Berhasil", message: "Akun berhasil dibuat"); } catch (e) { - Get.snackbar("Error", "Failed to register: ${e.toString()}"); + CustomFloatingLoading.hideLoadingDialog(Get.context!); + + String errorMessage = e.toString().replaceFirst("Exception: ", ""); + + CustomNotification.error(title: "Pendaftaran gagal", message: errorMessage); } }