UNPKG

@apicart/vue-components

Version:

Apicart Vue.Js components for simple e-commerce platform development

50 lines (39 loc) 1.15 kB
import Apicart from '@apicart/core-sdk'; let eventListenersInitialized = false; const init = (): void => { if (eventListenersInitialized) { return; } eventListenersInitialized = true; let store = null; const getStore = (): any => { if (!store) { store = Apicart.getConfigParameter('store'); } return store; }; Apicart.Utils.Dom .on('click', '[data-apicart-add-item]', async (event): Promise<void> => { const el = event.currentTarget; (await getStore().getCart()).addItem( el.getAttribute('data-apicart-add-item'), el.getAttribute('data-apicart-item-quantity') || 1 ); }) .on('click', '[data-apicart-remove-item]', async (event): Promise<void> => { const el = event.currentTarget; const itemUrl = el.getAttribute('data-apicart-remove-item'); const cart = await getStore().getCart(); let quantity = el.getAttribute('data-apicart-item-quantity') || 1; if (quantity === 'all') { const cartItem = await cart.findItemByDataUrl(itemUrl); if (cartItem) { quantity = cartItem.getQuantity(); } } cart.removeItem(itemUrl, quantity); }); }; export default { init: init };