addTimeSlot method

void addTimeSlot(
  1. String orderId,
  2. String slotId,
  3. String dateIndex,
  4. AddTimeSlots addTimeSlot,
)

Implementation

void addTimeSlot(String orderId, String slotId, String dateIndex,
    AddTimeSlots addTimeSlot) async {
  addTimeSlot.onLoading();

  final url = "https://api.plentrasphere.com/v2/client/index.php";
  final body = {
    'class': 'checkout',
    "appKey": appKey,
    "action": "addTimeSlot",
    "orderId": orderId,
    "slotId": slotId,
    "dateIndex": dateIndex,
  };

  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 == "app-expired") {
        addTimeSlot.onAppNotActive(data['info']['appName']);
        addTimeSlot.onLoadfinished();
        return;
      }
      if (status == "session-expired") {
        addTimeSlot.onNotLoggedIn();
        addTimeSlot.onLoadfinished();
        return;
      }
      if (status == "invalid-order") {
        addTimeSlot.onInvalidOrder();
        addTimeSlot.onLoadfinished();
        return;
      }

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

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