File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/resources/js/site/services/shoppingCartService.js
import {ErrorResponse} from "../../global/models/errorResponse";
class ShoppingCartService
{
constructor()
{
this.baseRoute = '/api/cart/';
}
/**
* Validate a product quantity input.
* Will validate that the quantity is a number and a multiply of the steps if needed
*
* @param input
*/
validateProductQuantityInput(input)
{
let quantity = parseInt(input.value);
const quantityMin = parseInt(input.min);
const quantityCap = parseInt(input.max);
// If there has not been a value set, set the quantity step
if(isNaN(quantity)) {
if(!isNaN(quantityMin)) quantity = quantityMin;
else quantity = quantityStep;
input.value = quantity;
return;
}
// Check that de min quantity is correct
if(!isNaN(quantityMin)) {
if(quantity < quantityMin) {
quantity = quantityMin;
input.value = quantity;
return;
}
}
/**
* Because it has exceeded the cap we set it to the cap and remove one quantity step.
* The validateOrRoundUpQuantityToStep function then will automatically round it up.
*/
if(quantity > quantityCap) {
quantity = quantityCap;
input.value = quantity;
return;
}
}
/**
* Adds a item to the shopping cart
*
* @param id
* @param type
* @param quantity
* @param redirect
*/
addItemToCart(id, type, quantity, redirect = false) {
const self = this;
return new Promise(function(resolve, reject) {
Ajax.post(
self.baseRoute + 'add-item-to-cart',
{productableId: id, itemType: type, quantity: quantity},
function (xhr) {
if (xhr.status === 200) {
resolve();
} else {
console.log(id);
console.log(type);
console.log(quantity);
debugger;
reject();
}
}
);
});
};
/**
* Remove a item from the shopping cart
*
* @param productId
* @param locationId
*/
removeItemFromCart(productId, locationId) {
const self = this;
return new Promise(function(resolve, reject) {
Ajax.post(
self.baseRoute + 'remove-item-from-cart',
{productId: productId, locationId: locationId},
function (xhr) {
resolve();
}
);
});
};
/**
* Change the date of shopping cart
*
* @param date
*/
changeDate(date) {
const self = this;
return new Promise(function(resolve, reject) {
Ajax.post(
self.baseRoute + 'change-date',
{date: date},
function (xhr) {
resolve();
}
);
});
};
/**
* Update the shopping cart item
*
* @param productId
* @param locationId
* @param quantity
* @param timeSlot
* @param notification
*/
updateCartItem(productId, locationId, quantity, timeSlot, notification) {
const self = this;
if (quantity === null || quantity > 0) {
return new Promise(function(resolve, reject) {
Ajax.post(
self.baseRoute + 'update-item',
{productId: productId, locationId: locationId, quantity: quantity, timeSlot: timeSlot, notification: notification},
function (xhr) {
resolve();
}
);
});
} else {
return this.removeItemFromCart(productId, locationId);
}
};
/**
* Get the current cart information
*
* @returns {Promise|Promise|Promise}
*/
getCurrentCartInformation() {
const self = this;
return new Promise(function(resolve, reject) {
Ajax.get(
self.baseRoute + 'get-current-cart-information',
function(xhr) {
let response = JSON.parse(xhr.response);
let isErrorResponse = ErrorResponse.is(xhr.response, false);
// let isCheckoutInformationResponse = CheckoutInformationResponse.is(xhr.response, false);
if(!isErrorResponse) {
resolve(response);
} else {
let response = ErrorResponse.fromJsonString(xhr.response);
reject(response);
}
}
);
})
}
}
export { ShoppingCartService }