Skip to content

Commit

Permalink
Add to cart action
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Sep 17, 2023
1 parent 7048c92 commit 85889bd
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 121 deletions.
78 changes: 0 additions & 78 deletions _dev/js/theme/core/cart/cartAddProduct.js

This file was deleted.

2 changes: 2 additions & 0 deletions _dev/js/theme/core/cart/cartController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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";

Check failure on line 6 in _dev/js/theme/core/cart/cartController.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Strings must use singlequote

Check failure on line 6 in _dev/js/theme/core/cart/cartController.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Strings must use singlequote

const { on } = useEvent();

Expand All @@ -20,6 +21,7 @@ const cartController = () => {
on(document, 'submit', '.js-voucher-form', submitVoucherHandler);
on(document, 'click', discountCode, codeLinkSubmitHandler);
on(document, 'click', '.js-voucher-delete', deleteVoucherHandler);
on(document, 'click', '[data-button-action="add-to-cart"]', addToCartHandler);
};

return {
Expand Down
86 changes: 86 additions & 0 deletions _dev/js/theme/core/cart/handler/cart/addToCartHandler.js
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

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Strings must use singlequote

Check failure on line 1 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Strings must use singlequote
import addToCartRequest from "../../request/addToCartRequest";

Check failure on line 2 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Strings must use singlequote

Check failure on line 2 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Strings must use singlequote

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

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 2 spaces but found 4

Check failure on line 12 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 2 spaces but found 4

const form = event.currentTarget?.form;

Check failure on line 14 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 2 spaces but found 4

Check failure on line 14 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 2 spaces but found 4
const addToCartButton = event.currentTarget;

Check failure on line 15 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 2 spaces but found 4

Check failure on line 15 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 2 spaces but found 4

addToCartButton.setAttribute('disabled', true);

Check failure on line 17 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 2 spaces but found 4

Check failure on line 17 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 2 spaces but found 4

const isQuantityInputValid = (input) => {

Check failure on line 19 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 2 spaces but found 4

Check failure on line 19 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 2 spaces but found 4
let validInput = true;

Check failure on line 20 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 4 spaces but found 8

Check failure on line 20 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 4 spaces but found 8

const minimalValue = parseInt((input?.getAttribute('min') || 0), 10);

Check failure on line 22 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 4 spaces but found 8

Check failure on line 22 in _dev/js/theme/core/cart/handler/cart/addToCartHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Expected indentation of 4 spaces but found 8

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;
5 changes: 0 additions & 5 deletions _dev/js/theme/core/cart/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import updateCart from '@js/theme/core/cart/updateCart';
import cartAddProduct from '@js/theme/core/cart/cartAddProduct';
import cartQuantity from '@js/theme/core/cart/cartQuantity';
import cartDelete from '@js/theme/core/cart/cartDelete';
import DOMReady from '@js/theme/utils/DOMReady';
Expand All @@ -13,7 +12,3 @@ DOMReady(() => {
cartDelete();
init();
});

$(() => {
cartAddProduct();
});
72 changes: 72 additions & 0 deletions _dev/js/theme/core/cart/request/addToCartRequest.js
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;
2 changes: 1 addition & 1 deletion _dev/js/theme/core/cart/request/addVoucherToCartRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import useHttpRequest from '../../../components/http/useHttpRequest';
* @property {number} id_product - always 0
* @property {number} id_product_attribute - always 0
* @property {number} quantity - always 0
* @property {boolean} success - always 0
* @property {boolean} success - success flag
* @property {object} cart - cart front object
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import useHttpRequest from '../../../components/http/useHttpRequest';
* @property {number} id_product - always 0
* @property {number} id_product_attribute - always 0
* @property {number} quantity - always 0
* @property {boolean} success - always 0
* @property {boolean} success - success flag
* @property {object} cart - cart front object
*/

Expand Down
2 changes: 0 additions & 2 deletions _dev/js/theme/frontAPI/apiAction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import addToCartAction from '@js/theme/frontAPI/cart/addToCartAction';
import refreshCartPageAction from '@js/theme/frontAPI/cart/refreshCartPageAction';
import updateCartQuantityAction from '@js/theme/frontAPI/cart/updateCartQuantityAction';
import deleteFromCartAction from '@js/theme/frontAPI/cart/deleteFromCartAction';
Expand All @@ -16,7 +15,6 @@ prestashop.addAction = (actionName, actionFunction) => {
prestashop.frontAPI[actionName] = actionFunction;
};

prestashop.addAction('addToCart', addToCartAction);
prestashop.addAction('refreshCartPage', refreshCartPageAction);
prestashop.addAction('updateCartQuantity', updateCartQuantityAction);
prestashop.addAction('deleteFromCart', deleteFromCartAction);
Expand Down
34 changes: 0 additions & 34 deletions _dev/js/theme/frontAPI/cart/addToCartAction.js

This file was deleted.

0 comments on commit 85889bd

Please sign in to comment.