Skip to content

Commit

Permalink
lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Sep 17, 2023
1 parent 85889bd commit 9e3da12
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 92 deletions.
2 changes: 1 addition & 1 deletion _dev/js/theme/core/cart/cartController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
135 changes: 68 additions & 67 deletions _dev/js/theme/core/cart/handler/cart/addToCartHandler.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -9,78 +10,78 @@ const { danger } = useAlertToast();
* @returns {Promise<void>}
*/
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;
48 changes: 24 additions & 24 deletions _dev/js/theme/core/cart/request/addToCartRequest.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -42,31 +42,31 @@ import useHttpRequest from "../../../components/http/useHttpRequest";
* @returns {{getRequest: (function(): Promise<ServerResponse>)}}
*/
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;

0 comments on commit 9e3da12

Please sign in to comment.