From 9e3da127921a24aa1dbffc16e85a7a7c5f7f2e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Ste=CC=A8pien=CC=81?= Date: Mon, 18 Sep 2023 01:38:53 +0200 Subject: [PATCH] lint fix --- _dev/js/theme/core/cart/cartController.js | 2 +- .../cart/handler/cart/addToCartHandler.js | 135 +++++++++--------- .../core/cart/request/addToCartRequest.js | 48 +++---- 3 files changed, 93 insertions(+), 92 deletions(-) diff --git a/_dev/js/theme/core/cart/cartController.js b/_dev/js/theme/core/cart/cartController.js index ba333818..1bac91e9 100644 --- a/_dev/js/theme/core/cart/cartController.js +++ b/_dev/js/theme/core/cart/cartController.js @@ -3,7 +3,7 @@ import useEvent from '../../components/event/useEvent'; import submitVoucherHandler from './handler/voucher/submitVoucherHandler'; import codeLinkSubmitHandler from './handler/voucher/codeLinkSubmitHandler'; import deleteVoucherHandler from './handler/voucher/deleteVoucherHandler'; -import addToCartHandler from "./handler/cart/addToCartHandler"; +import addToCartHandler from './handler/cart/addToCartHandler'; const { on } = useEvent(); diff --git a/_dev/js/theme/core/cart/handler/cart/addToCartHandler.js b/_dev/js/theme/core/cart/handler/cart/addToCartHandler.js index 0c492421..89c9d31e 100644 --- a/_dev/js/theme/core/cart/handler/cart/addToCartHandler.js +++ b/_dev/js/theme/core/cart/handler/cart/addToCartHandler.js @@ -1,5 +1,6 @@ -import useAlertToast from "../../../../components/useAlertToast"; -import addToCartRequest from "../../request/addToCartRequest"; +import useAlertToast from '../../../../components/useAlertToast'; +import addToCartRequest from '../../request/addToCartRequest'; +import sprintf from '../../../../utils/sprintf'; const { danger } = useAlertToast(); @@ -9,78 +10,78 @@ const { danger } = useAlertToast(); * @returns {Promise} */ const addToCartHandler = async (event) => { - event.preventDefault(); + event.preventDefault(); - const form = event.currentTarget?.form; - const addToCartButton = event.currentTarget; + const form = event.currentTarget?.form; + const addToCartButton = event.currentTarget; - addToCartButton.setAttribute('disabled', true); + addToCartButton.setAttribute('disabled', true); - const isQuantityInputValid = (input) => { - let validInput = true; + const isQuantityInputValid = (input) => { + let validInput = true; - const minimalValue = parseInt((input?.getAttribute('min') || 0), 10); + const minimalValue = parseInt((input?.getAttribute('min') || 0), 10); - 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; + if (minimalValue && input.value < minimalValue) { + validInput = false; } - 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); + return validInput; + }; + + const idProduct = form.querySelector('[name=id_product]').value; + const quantityInput = form.querySelector('[name=qty]'); + const qty = quantityInput?.value || 0; + const idProductAttribute = form.querySelector('[name=id_product_attribute]')?.value || 0; + const idCustomization = 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: idProduct, + qty, + id_product_attribute: idProductAttribute, + id_customization: idCustomization, + }; + + 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, + }); } - - setTimeout(() => { - addToCartButton.removeAttribute('disabled'); - }, 1000); -} + } catch (error) { + danger(error.message); + } + + setTimeout(() => { + addToCartButton.removeAttribute('disabled'); + }, 1000); +}; export default addToCartHandler; diff --git a/_dev/js/theme/core/cart/request/addToCartRequest.js b/_dev/js/theme/core/cart/request/addToCartRequest.js index a9863257..a6ee9455 100644 --- a/_dev/js/theme/core/cart/request/addToCartRequest.js +++ b/_dev/js/theme/core/cart/request/addToCartRequest.js @@ -1,5 +1,5 @@ -import prestashop from "prestashop"; -import useHttpRequest from "../../../components/http/useHttpRequest"; +import prestashop from 'prestashop'; +import useHttpRequest from '../../../components/http/useHttpRequest'; /** * @typedef ServerResponse @@ -42,31 +42,31 @@ import useHttpRequest from "../../../components/http/useHttpRequest"; * @returns {{getRequest: (function(): Promise)}} */ const addToCartRequest = (payload) => { - const { request } = useHttpRequest(prestashop.urls.pages.cart); + const { request } = useHttpRequest(prestashop.urls.pages.cart); - const payloadToSend = { - add: 1, - action: 'update', - ajax: 1, - token: prestashop.static_token, - ...payload, - }; + 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)); - }); - }); + const getRequest = () => new Promise((resolve, reject) => { + request + .query(payloadToSend) + .post() + .json((resp) => { + resolve(resp); + }) + .catch(() => { + reject(Error(prestashop.t.alert.genericHttpError)); + }); + }); - return { - getRequest, - }; + return { + getRequest, + }; }; export default addToCartRequest;