-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import useAlertToast from "../../../../components/useAlertToast"; | ||
Check failure on line 1 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js GitHub Actions / Code quality - ESLint
|
||
import addToCartRequest from "../../request/addToCartRequest"; | ||
Check failure on line 2 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js GitHub Actions / Code quality - ESLint
|
||
|
||
const { danger } = useAlertToast(); | ||
|
||
/** | ||
* Handle add to cart event on form submit | ||
* @param event {object} - submit event | ||
* @returns {Promise<void>} | ||
*/ | ||
const addToCartHandler = async (event) => { | ||
event.preventDefault(); | ||
Check failure on line 12 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js GitHub Actions / Code quality - ESLint
|
||
|
||
const form = event.currentTarget?.form; | ||
Check failure on line 14 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js GitHub Actions / Code quality - ESLint
|
||
const addToCartButton = event.currentTarget; | ||
Check failure on line 15 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js GitHub Actions / Code quality - ESLint
|
||
|
||
addToCartButton.setAttribute('disabled', true); | ||
Check failure on line 17 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js GitHub Actions / Code quality - ESLint
|
||
|
||
const isQuantityInputValid = (input) => { | ||
Check failure on line 19 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js GitHub Actions / Code quality - ESLint
|
||
let validInput = true; | ||
Check failure on line 20 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js GitHub Actions / Code quality - ESLint
|
||
|
||
const minimalValue = parseInt((input?.getAttribute('min') || 0), 10); | ||
Check failure on line 22 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js GitHub Actions / Code quality - ESLint
|
||
|
||
if (minimalValue && input.value < minimalValue) { | ||
validInput = false; | ||
} | ||
|
||
return validInput; | ||
}; | ||
|
||
const id_product = form.querySelector('[name=id_product]').value; | ||
const quantityInput = form.querySelector('[name=qty]'); | ||
const qty = quantityInput?.value || 0; | ||
const id_product_attribute = form.querySelector('[name=id_product_attribute]')?.value || 0; | ||
const id_customization = form.querySelector('[name=id_customization]')?.value || 0; | ||
|
||
const onInvalidQuantity = (input) => { | ||
danger(sprintf(prestashop.t.alert.minimalQuantity, input.getAttribute('min'))); | ||
}; | ||
|
||
if (quantityInput && !isQuantityInputValid(quantityInput)) { | ||
onInvalidQuantity(quantityInput); | ||
addToCartButton.removeAttribute('disabled'); | ||
|
||
return; | ||
} | ||
|
||
const payload = { | ||
id_product, | ||
qty, | ||
id_product_attribute, | ||
id_customization, | ||
}; | ||
|
||
const { getRequest } = addToCartRequest(payload); | ||
|
||
try { | ||
const resp = await getRequest(); | ||
|
||
if (!resp.hasError) { | ||
prestashop.emit('updateCart', { | ||
reason: { | ||
idProduct: resp.id_product, | ||
idProductAttribute: resp.id_product_attribute, | ||
idCustomization: resp.id_customization, | ||
linkAction: 'add-to-cart', | ||
cart: resp.cart, | ||
}, | ||
resp, | ||
}); | ||
} else { | ||
prestashop.emit('handleError', { | ||
eventType: 'addProductToCart', | ||
resp, | ||
}); | ||
} | ||
} catch (error) { | ||
danger(error.message); | ||
} | ||
|
||
setTimeout(() => { | ||
addToCartButton.removeAttribute('disabled'); | ||
}, 1000); | ||
} | ||
|
||
export default addToCartHandler; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import prestashop from "prestashop"; | ||
import useHttpRequest from "../../../components/http/useHttpRequest"; | ||
|
||
/** | ||
* @typedef ServerResponse | ||
* @type {object} | ||
* @property {string|string[]} errors - the errors returned by the server | ||
* @property {number} id_product - product id | ||
* @property {number} id_product_attribute - product attribute id | ||
* @property {number} id_customization - product customization id | ||
* @property {number} quantity - product quantity | ||
* @property {boolean} success - success flag | ||
* @property {object} cart - cart front object | ||
*/ | ||
|
||
/** | ||
* Add voucher to cart request | ||
* @param payload {Object} - payload object to send | ||
* @param payload.id_product {number} - product id - Required | ||
* @param payload.qty {number} - product quantity - Required | ||
* @param payload.id_product_attribute {number} - product id attribute - optional pass 0 if not set | ||
* @param payload.id_customization {number} - customization id - optional pass 0 if not set | ||
* @param payload.add {number} - optional | ||
* @param payload.action {string} - optional | ||
* @param payload.token {string} - optional | ||
* @param payload.ajax {number} - optional | ||
* @example | ||
* const payload = { | ||
* id_product: 1, // Required | ||
* qty: 1, // Required | ||
* id_product_attribute: 2, // optional | ||
* id_customization: 3, // optional | ||
* }; | ||
* | ||
* const { getRequest } = addToCartRequest(payload); | ||
* | ||
* try { | ||
* const resp = await getRequest(); | ||
* } catch (error) { | ||
* console.error(error); | ||
* } | ||
* @returns {{getRequest: (function(): Promise<ServerResponse>)}} | ||
*/ | ||
const addToCartRequest = (payload) => { | ||
const { request } = useHttpRequest(prestashop.urls.pages.cart); | ||
|
||
const payloadToSend = { | ||
add: 1, | ||
action: 'update', | ||
ajax: 1, | ||
token: prestashop.static_token, | ||
...payload, | ||
}; | ||
|
||
const getRequest = () => new Promise((resolve, reject) => { | ||
request | ||
.query(payloadToSend) | ||
.post() | ||
.json((resp) => { | ||
resolve(resp); | ||
}) | ||
.catch(() => { | ||
reject(Error(prestashop.t.alert.genericHttpError)); | ||
}); | ||
}); | ||
|
||
return { | ||
getRequest, | ||
}; | ||
}; | ||
|
||
export default addToCartRequest; |
This file was deleted.