addToWishlist method

void addToWishlist(
  1. String itemId,
  2. AddtoWishlist addToWishlist
)

Implementation

void addToWishlist(String itemId, AddtoWishlist addToWishlist) async {
  addToWishlist.onLoading();

  final url = "https://api.plentrasphere.com/v2/client/index.php";
  final body = {
    'class': 'wishlist',
    "appKey": appKey,
    "action": "addToWishlist",
    "itemId": itemId,
  };

  try {
    final response = await http.post(
      Uri.parse(url),
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        'Authorization': 'Bearer $token'
      },
      body: body,
    );

    final data = json.decode(response.body);
    int code = data['response']['code'];
    String status = data['response']['status'];

    if (code == 400) {
      if (status == "session-expired" || status == "please-login") {
        addToWishlist.onNotLoggedIn();
        addToWishlist.onLoadfinished();
        return;
      }

      if (status == "app-expired") {
        addToWishlist.onAppNotActive(data['info']['appName']);
        addToWishlist.onLoadfinished();
        return;
      }

      if (status == "wishlist-max-limit-reached") {
        addToWishlist.onWishlistMaxLimitReached();
        addToWishlist.onLoadfinished();
        return;
      }

      if (status == "item-already-added") {
        addToWishlist.onItemAlreadyAdded();
        addToWishlist.onLoadfinished();
        return;
      }

      addToWishlist.onError(status);
      addToWishlist.onLoadfinished();
      return;
    }

    addToWishlist.onSuccess(status);
    addToWishlist.onLoadfinished();
  } catch (e) {
    addToWishlist.onError(e.toString());
    addToWishlist.onLoadfinished();
  }
}