saveInformation method

dynamic saveInformation()

Implementation

saveInformation() async {
  if (userFirstNameController.text.trim().length < 2) {
    DefaultSnackbar.show(
        "Invalid first name", "Please enter a valid first name");
    return;
  } else if (userLastNameController.text.trim().length < 2) {
    DefaultSnackbar.show(
        "Invalid last name", "Please enter a valid last name");
    return;
  } else if (!RegExp(r'^[0-9+]+$')
      .hasMatch(countryCode.value + userPhoneController.text.trim())) {
    DefaultSnackbar.show(
        "Invalid phone number", "Please enter a valid phone number");
    return;
  }

  try {
    final response = await Requests.getDio().put(
      '/api/users/update-user',
      data: {
        'firstName': userFirstNameController.text.trim(),
        'lastName': userLastNameController.text.trim(),
        'phone': userPhoneController.text.trim(),
        'countryCode': countryCode.value,
      },
    );

    if (response.statusCode == 200) {
      isEditingInformation.value = false;
      await authenticationController.getUserProfile();
      DefaultSnackbar.show(
          "Success", "Updated user information successfully");
    } else {
      log('Error updating profile info: ${response.data}');
      final String? errorMsg = response.data?['message'];
      DefaultSnackbar.show(
          "Error", errorMsg ?? "Error while updating user information");
    }
  } catch (_) {
    log('Error updating profile info: ${_.toString()}');
    DefaultSnackbar.show("Error", "Error while updating user information");
  }
}