Skip to content

Commit

Permalink
update cart minor refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Sep 26, 2023
1 parent b0e378b commit 2e0c8e3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 26 deletions.
9 changes: 8 additions & 1 deletion _dev/js/theme/core/cart/handler/cart/updateCartHandler.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import prestashop from 'prestashop';
import parseToHtml from '../../../../utils/parseToHtml';
import useAlertToast from '../../../../components/useAlertToast';
import updateCartContentRequest from '../../request/cart/updateCartContentRequest';

const { danger } = useAlertToast();

/**
* Update cart handler - update cart content and emit updatedCart event
* @param {object} event - update cart event object
* @returns {Promise<void>}
*/
const updateCartHandler = async (event) => {
prestashop.cart = event.resp.cart;
document.querySelector('body').classList.add('cart-loading');
const { getRequest } = updateCartContentRequest();

try {
const resp = await prestashop.frontAPI.refreshCartPage();
const resp = await getRequest();

document.querySelector(prestashop.selectors.cart.detailedTotals)
?.replaceWith(parseToHtml(resp.cart_detailed_totals));
Expand Down
3 changes: 3 additions & 0 deletions _dev/js/theme/core/cart/handler/cart/updatedCartHandler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Updated cart handler
*/
const updatedCartHandler = () => {
document.querySelector('body').classList.remove('cart-loading');
};
Expand Down
2 changes: 1 addition & 1 deletion _dev/js/theme/core/cart/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DOMReady from '@js/theme/utils/DOMReady';
import DOMReady from '../../utils/DOMReady';
import prestashop from 'prestashop';
import cartController from './cartController';

Expand Down
42 changes: 42 additions & 0 deletions _dev/js/theme/core/cart/request/cart/updateCartContentRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import useDefaultHttpRequest from "../../../../components/http/useDefaultHttpRequest";
import prestashop from "prestashop";
/**
* @typedef ServerResponse
* @type {object}
* @property {string} cart_detailed - cart detailed html used in cart page
* @property {string} cart_detailed_actions - cart actions html used in cart page
* @property {string} cart_detailed_totals - cart totals html used in cart page
* @property {string} cart_summary_items_subtotal - cart summary items subtotal html used in checkout page
* @property {string} cart_summary_products - cart summary products html used in checkout page
* @property {string} cart_summary_subtotals_container - cart summary products html used in checkout page
* @property {string} cart_summary_top - cart summary top html used in checkout page
* @property {string} cart_summary_totals - cart summary totals html used in checkout page
* @property {string} cart_voucher - cart voucher html used in checkout page
*/

/**
* Add voucher to cart request
* @example
* const { getRequest } = updateCartContentRequest();
*
* try {
* const resp = await getRequest();
* } catch (error) {
* console.error(error);
* }
* @returns {{getRequest: (function(): Promise<ServerResponse>)}}
*/
const updateCartContentRequest = () => {
const payloadToSend = {
action: 'refresh',
ajax: 1,
};

const getRequest = () => useDefaultHttpRequest(prestashop.urls.pages.cart, payloadToSend);

return {
getRequest,
}
}

export default updateCartContentRequest;
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 refreshCartPageAction from '@js/theme/frontAPI/cart/refreshCartPageAction';
import updateProductAction from '@js/theme/frontAPI/product/updateProductAction';
import updateListingFacetsAction from '@js/theme/frontAPI/listing/updateListingFacetsAction';
import updateAddressAction from '@js/theme/frontAPI/address/updateAddressAction';
Expand All @@ -13,7 +12,6 @@ prestashop.addAction = (actionName, actionFunction) => {
prestashop.frontAPI[actionName] = actionFunction;
};

prestashop.addAction('refreshCartPage', refreshCartPageAction);
prestashop.addAction('updateProduct', updateProductAction);
prestashop.addAction('updateListingFacets', updateListingFacetsAction);
prestashop.addAction('updateAddress', updateAddressAction);
22 changes: 0 additions & 22 deletions _dev/js/theme/frontAPI/cart/refreshCartPageAction.js

This file was deleted.

0 comments on commit 2e0c8e3

Please sign in to comment.