Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/js/components/UseHandleCartAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const handleCartAction = (event: Event): void => {
event.stopPropagation();
event.preventDefault();

// Get the target element and its dataset
const target = event.target as HTMLElement;

// Make request to refresh the cart
sendCartRefreshRequest(target);
};

const sendCartRefreshRequest = (target: HTMLElement): void => {
const {prestashop, Theme: {events}} = window;
const {dataset} = target;

const targetUrl = target.getAttribute('href');

if (targetUrl === null) {
return;
}

const formData = new FormData();
formData.append('ajax', '1');
formData.append('action', 'update');

fetch(targetUrl, {
method: 'POST',
body: formData,
})
.then((resp: Response) => {
// Refresh cart preview
prestashop.emit(events.updateCart, {
reason: dataset,
resp,
});
})
.catch((err) => {
const errorData = err as Response;
prestashop.emit(events.handleError, {
eventType: 'updateProductInCart',
errorData,
});
});
};

export default handleCartAction;
2 changes: 2 additions & 0 deletions src/js/constants/selectors-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ export const listing = {
};

export const cart = {
container: '.cart-container',
overview: '.cart-overview',
discountCode: '.js-discount .js-code',
discountName: '[name=discount_name]',
displayPromo: '.display-promo',
promoCode: '#promo-code',
deleteLinkAction: 'delete-from-cart',
};

export const blockcart = {
Expand Down
13 changes: 13 additions & 0 deletions src/js/pages/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import {Collapse} from 'bootstrap';
import {isHTMLElement} from '@helpers/typeguards';
import handleCartAction from '../components/UseHandleCartAction';

export default () => {
const {Theme} = window;
Expand All @@ -31,4 +32,16 @@ export default () => {
return false;
});
});

const cartContainer = document.querySelector<HTMLElement>(Theme.selectors.cart.container);

if (cartContainer) {
cartContainer.addEventListener('click', (event: Event) => {
const eventTarget = event.target as HTMLElement;

if (eventTarget.dataset.linkAction === Theme.selectors.cart.deleteLinkAction) {
handleCartAction(event);
}
});
}
};
2 changes: 2 additions & 0 deletions types/selectors.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ declare type cart = {
discountName: string,
displayPromo: string,
promoCode: string,
deleteLinkAction: string,
container: string,
};

declare type blockcart = {
Expand Down