diff --git a/_dev/js/theme/components/http/useDefaultHttpRequest.js b/_dev/js/theme/components/http/useDefaultHttpRequest.js index 5f5c6184..130c8909 100644 --- a/_dev/js/theme/components/http/useDefaultHttpRequest.js +++ b/_dev/js/theme/components/http/useDefaultHttpRequest.js @@ -16,13 +16,7 @@ const useDefaultHttpRequest = (url, payload, options = {}) => { .query(payload) .post() .json((resp) => { - if (resp.errors) { - const errors = Array.isArray(resp.errors) ? resp.errors : [resp.errors]; - - reject(Error(errors.join('\n'))); - } else { - resolve(resp); - } + resolve(resp); }) .catch(() => { reject(Error(prestashop.t.alert.genericHttpError)); diff --git a/_dev/js/theme/core/cart/cartController.js b/_dev/js/theme/core/cart/cartController.js index 5f59782e..3d58c5eb 100644 --- a/_dev/js/theme/core/cart/cartController.js +++ b/_dev/js/theme/core/cart/cartController.js @@ -8,6 +8,7 @@ import deleteFromCartHandler from './handler/cart/deleteFromCartHandler'; import quantityChangeHandler from './handler/cart/quantityChangeHandler'; import updateCartHandler from './handler/cart/updateCartHandler'; import updatedCartHandler from './handler/cart/updatedCartHandler'; +import cartErrorsHandler from './handler/cart/cartErrorsHandler'; import useCustomQuantityInput from '../../components/useCustomQuantityInput'; const { on } = useEvent(); @@ -50,11 +51,14 @@ const cartController = () => { prestashop.on('updatedCart', (event) => { attachSpinnerEvents(); updatedCartHandler(event); + cartErrorsHandler(); }); prestashop.on('updateCart', (event) => { updateCartHandler(event); }); + + cartErrorsHandler(); }; return { diff --git a/_dev/js/theme/core/cart/handler/cart/cartErrorsHandler.js b/_dev/js/theme/core/cart/handler/cart/cartErrorsHandler.js new file mode 100644 index 00000000..c179280c --- /dev/null +++ b/_dev/js/theme/core/cart/handler/cart/cartErrorsHandler.js @@ -0,0 +1,61 @@ +import prestashop from 'prestashop'; +import cartStateStore from '../../store/cartStateStore'; + +const { + setIsUpdateOperation, + setHasError, + setErrorMsg, + getErrorMsg, + getHasError, + getIsUpdateOperation, +} = cartStateStore(); + +/** + * Handles cart page errors + * Side effects: updates cart state and cart notification DOM + * @returns {void} + */ +const cartErrorsHandler = () => { + const checkoutBtn = document.querySelector(prestashop.selectors.checkout.btn); + const errorMsg = getErrorMsg(); + const hasError = getHasError(); + const isUpdateOperation = getIsUpdateOperation(); + const notification = document.querySelector(prestashop.selectors.notifications.container); + + if (document.querySelector(prestashop.selectors.notifications.dangerAlert) || (errorMsg !== '' && !hasError)) { + checkoutBtn.classList.add('disabled'); + } + + if (errorMsg !== '') { + const alertBlock = ` + + `; + + if (notification) { + notification.innerHTML = alertBlock; + } + + setErrorMsg(''); + setIsUpdateOperation(false); + + if (hasError) { + // if hasError is true, quantity was not updated : allow checkout + checkoutBtn.classList.remove('disabled'); + } + } else if (!hasError && isUpdateOperation) { + setHasError(false); + setIsUpdateOperation(false); + + if (notification) { + notification.innerHTML = ''; + } + + checkoutBtn.classList.remove('disabled'); + } +} + +export default cartErrorsHandler; diff --git a/_dev/js/theme/core/cart/handler/cart/quantityChangeHandler.js b/_dev/js/theme/core/cart/handler/cart/quantityChangeHandler.js index f03ccd90..67d62d88 100644 --- a/_dev/js/theme/core/cart/handler/cart/quantityChangeHandler.js +++ b/_dev/js/theme/core/cart/handler/cart/quantityChangeHandler.js @@ -1,9 +1,12 @@ import prestashop from 'prestashop'; import quantityChangeRequest from '../../request/cart/quantityChangeRequest'; import useAlertToast from '../../../../components/useAlertToast'; +import cartStateStore from '../../store/cartStateStore'; const { danger } = useAlertToast(); +const { setIsUpdateOperation, setHasError, setErrorMsg } = cartStateStore(); + /** * @param {string} operation - increase|decrease * @param {number} qtyDifference - quantity difference @@ -31,6 +34,15 @@ const quantityChangeHandler = async (operation, qtyDifference, input) => { try { const resp = await getRequest(); + const { + errors = '', + hasError = false, + } = resp; + + setIsUpdateOperation(true); + setHasError(hasError); + setErrorMsg(errors); + if (!resp.hasError) { prestashop.emit('updateCart', { reason: dataset || resp, diff --git a/_dev/js/theme/core/cart/store/cartStateStore.js b/_dev/js/theme/core/cart/store/cartStateStore.js new file mode 100644 index 00000000..2c50b96d --- /dev/null +++ b/_dev/js/theme/core/cart/store/cartStateStore.js @@ -0,0 +1,78 @@ +const state = { + errorMsg: '', + isUpdateOperation: false, + hasError: false, +}; + +/** + * Store for cart state + * @module + * @returns {object} + */ +const cartStateStore = () => { + + /** + * Sets the error message + * @method setErrorMsg + * @public + * @param {string} value + */ + const setErrorMsg = (value) => { + state.errorMsg = value; + } + + /** + * Returns the error message + * @method getErrorMsg + * @public + * @return {string} + */ + const getErrorMsg = () => state.errorMsg; + + /** + * Sets the isUpdateOperation value + * @method setIsUpdateOperation + * @public + * @param {boolean} value + */ + const setIsUpdateOperation = (value) => { + state.isUpdateOperation = value; + } + + /** + * Returns the isUpdateOperation value + * @method getIsUpdateOperation + * @public + * @return {boolean} + */ + const getIsUpdateOperation = () => state.isUpdateOperation; + + /** + * Sets the hasError value + * @method setHasError + * @public + * @param {boolean} value + */ + const setHasError = (value) => { + state.hasError = value; + } + + /** + * Returns the hasError value + * @method getHasError + * @public + * @return {boolean} + */ + const getHasError = () => state.hasError; + + return { + setErrorMsg, + getErrorMsg, + setIsUpdateOperation, + getIsUpdateOperation, + setHasError, + getHasError, + } +} + +export default cartStateStore; diff --git a/_dev/js/theme/core/product/productController.js b/_dev/js/theme/core/product/productController.js index 4ea81aed..acd614c0 100644 --- a/_dev/js/theme/core/product/productController.js +++ b/_dev/js/theme/core/product/productController.js @@ -25,7 +25,19 @@ const persistFormDataOnInit = () => { } }; +/** + * Product controller + * @module productController + * @return {{init: init}} + */ const productController = () => { + + /** + * Init product controller + * @method init + * @public + * @return {void} + */ const init = () => { persistFormDataOnInit(); on(document, 'click', prestashop.selectors.listing.quickview, quickViewClickHandler);