fix: notification setup

This commit is contained in:
akhdanre 2025-05-23 13:42:51 +07:00
parent e3d2cbb7a6
commit bfd959a5df
5 changed files with 52 additions and 34 deletions

View File

@ -11,11 +11,13 @@ class TrueFalseQuestion extends BaseQuestionModel {
}) : super(type: 'true_false'); }) : super(type: 'true_false');
factory TrueFalseQuestion.fromJson(Map<String, dynamic> json) { factory TrueFalseQuestion.fromJson(Map<String, dynamic> json) {
print(json['target_answer']);
return TrueFalseQuestion( return TrueFalseQuestion(
index: json['index'], index: json['index'],
question: json['question'], question: json['question'],
duration: json['duration'], duration: json['duration'],
targetAnswer: json['target_answer'], targetAnswer: json['target_answer'].toString().toLowerCase() == 'true',
); );
} }

View File

@ -17,47 +17,58 @@ class AuthService extends GetxService {
} }
Future<bool> register(RegisterRequestModel request) async { Future<bool> register(RegisterRequestModel request) async {
var data = await dio.post( try {
APIEndpoint.register, final response = await dio.post(
data: request.toJson(), APIEndpoint.register,
); data: request.toJson(),
if (data.statusCode == 200) { );
return true;
} else { return response.statusCode == 200;
throw Exception("Registration failed"); } 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<LoginResponseModel> loginWithEmail(LoginRequestModel request) async { Future<LoginResponseModel> loginWithEmail(LoginRequestModel request) async {
final data = request.toJson(); try {
final response = await dio.post(APIEndpoint.login, data: data); final data = request.toJson();
final response = await dio.post(APIEndpoint.login, data: data);
if (response.statusCode == 200) {
print(response.data);
final baseResponse = BaseResponseModel<LoginResponseModel>.fromJson( final baseResponse = BaseResponseModel<LoginResponseModel>.fromJson(
response.data, response.data,
(json) => LoginResponseModel.fromJson(json), (json) => LoginResponseModel.fromJson(json),
); );
return baseResponse.data!; return baseResponse.data!;
} else { } on DioException catch (e) {
throw Exception("Login failed"); final errorMessage = e.response?.data['message'] ?? "Login gagal";
throw Exception(errorMessage);
} }
} }
Future<LoginResponseModel> loginWithGoogle(String idToken) async { Future<LoginResponseModel> loginWithGoogle(String idToken) async {
final response = await dio.post( try {
APIEndpoint.loginGoogle, final response = await dio.post(
data: {"token_id": idToken}, APIEndpoint.loginGoogle,
); data: {"token_id": idToken},
);
if (response.statusCode == 200) {
final baseResponse = BaseResponseModel<LoginResponseModel>.fromJson( final baseResponse = BaseResponseModel<LoginResponseModel>.fromJson(
response.data, response.data,
(json) => LoginResponseModel.fromJson(json), (json) => LoginResponseModel.fromJson(json),
); );
return baseResponse.data!; return baseResponse.data!;
} else { } on DioException catch (e) {
throw Exception("Google login failed"); final errorMessage = e.response?.data['message'] ?? "Login Google gagal";
throw Exception(errorMessage);
} }
} }
} }

View File

@ -77,7 +77,7 @@ class DetailQuizView extends GetView<DetailQuizController> {
GlobalButton(text: "Kerjakan", onPressed: controller.goToPlayPage), GlobalButton(text: "Kerjakan", onPressed: controller.goToPlayPage),
const SizedBox(height: 20), const SizedBox(height: 20),
GlobalButton(text: "buat ruangan", onPressed: () {}), // GlobalButton(text: "buat ruangan", onPressed: () {}),
const SizedBox(height: 20), const SizedBox(height: 20),
const Divider(thickness: 1.2, color: AppColors.borderLight), const Divider(thickness: 1.2, color: AppColors.borderLight),

View File

@ -55,7 +55,7 @@ class LoginController extends GetxController {
final password = passwordController.text.trim(); final password = passwordController.text.trim();
if (email.isEmpty || password.isEmpty) { if (email.isEmpty || password.isEmpty) {
Get.snackbar("Error", "Email and password are required"); Get.snackbar("Kesalahan", "Email dan kata sandi wajib diisi");
return; return;
} }
@ -75,7 +75,7 @@ class LoginController extends GetxController {
Get.offAllNamed(AppRoutes.mainPage); Get.offAllNamed(AppRoutes.mainPage);
} catch (e, stackTrace) { } catch (e, stackTrace) {
logC.e(e, stackTrace: 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 { } finally {
isLoading.value = false; isLoading.value = false;
} }
@ -86,13 +86,13 @@ class LoginController extends GetxController {
try { try {
final user = await _googleAuthService.signIn(); final user = await _googleAuthService.signIn();
if (user == null) { if (user == null) {
Get.snackbar("Error", "Google Sign-In canceled"); Get.snackbar("Kesalahan", "Masuk dengan Google dibatalkan");
return; return;
} }
final idToken = await user.authentication.then((auth) => auth.idToken); final idToken = await user.authentication.then((auth) => auth.idToken);
if (idToken == null || idToken.isEmpty) { if (idToken == null || idToken.isEmpty) {
Get.snackbar("Error", "No ID Token received."); Get.snackbar("Kesalahan", "Tidak menerima ID Token dari Google");
return; return;
} }

View File

@ -37,26 +37,27 @@ class RegisterController extends GetxController {
String phone = phoneController.text.trim(); String phone = phoneController.text.trim();
if (email.isEmpty || password.isEmpty || confirmPassword.isEmpty || name.isEmpty || birthDate.isEmpty) { 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; return;
} }
if (!_isValidEmail(email)) { if (!_isValidEmail(email)) {
Get.snackbar("Error", "Invalid email format"); CustomNotification.error(title: "Kesalahan", message: "Format email tidak valid");
return; return;
} }
if (!_isValidDateFormat(birthDate)) { 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; return;
} }
if (password != confirmPassword) { if (password != confirmPassword) {
Get.snackbar("Error", "Passwords do not match"); CustomNotification.error(title: "Kesalahan", message: "Kata sandi tidak cocok");
return; return;
} }
if (phone.isNotEmpty && (phone.length < 10 || phone.length > 13)) { 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; return;
} }
@ -74,9 +75,13 @@ class RegisterController extends GetxController {
Get.back(); Get.back();
CustomFloatingLoading.hideLoadingDialog(Get.context!); CustomFloatingLoading.hideLoadingDialog(Get.context!);
CustomNotification.success(title: "register success", message: "created account successfuly"); CustomNotification.success(title: "Pendaftaran Berhasil", message: "Akun berhasil dibuat");
} catch (e) { } 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);
} }
} }