Skip to content

Commit

Permalink
product update event trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Oct 1, 2023
1 parent bf8d402 commit c55cdfc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import prestashop from 'prestashop';
import productStateStore from '../../store/productStateStore';

const { setFormChanged } = productStateStore();

const productFormChangeHandler = (event) => {
setFormChanged(true);

prestashop.emit('updateProduct', {
eventType: 'updatedProductCombination',
event,
});
};

export default productFormChangeHandler;
2 changes: 2 additions & 0 deletions _dev/js/theme/core/product/productController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import productUpdateErrorHandler from './handler/product/productUpdateErrorHandl
import productFormDataPersister from './persister/productFormDataPersister';
import productPopStateHandler from './handler/product/productPopStateHandler';
import updatedProductHandler from './handler/product/updatedProductHandler';
import productFormChangeHandler from './handler/product/productFormChangeHandler';

const { on } = useEvent();
const { persist } = productFormDataPersister();
Expand All @@ -22,6 +23,7 @@ const productController = () => {
const init = () => {
persistFormDataOnInit();
on(document, 'click', prestashop.selectors.listing.quickview, quickViewClickHandler);
on(document, 'change', `${prestashop.selectors.product.variants} *[name]`, productFormChangeHandler);

window.addEventListener('popstate', productPopStateHandler);
prestashop.on('updatedProduct', updatedProductHandler);
Expand Down
21 changes: 1 addition & 20 deletions _dev/js/theme/core/product/updateProduct.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import prestashop from 'prestashop';
import { fromSerializeObject } from '@js/theme/utils/formSerialize';
import parseToHtml from '@js/theme/utils/parseToHtml';
import useAlertToast from '@js/theme/components/useAlertToast';
import useEvent from '@js/theme/components/event/useEvent';
import isQuickViewOpen from './utils/isQuickViewOpen';
import isProductPreview from './utils/isProductPreview';
import updateProductRequest from './request/product/updateProductRequest';
import productFormDataPersister from './persister/productFormDataPersister';
import productStateStore from './store/productStateStore';

const { setFormChanged, getCurrentRequestDelayedId, setCurrentRequestDelayedId } = productStateStore();

const { on } = useEvent();
const { getCurrentRequestDelayedId, setCurrentRequestDelayedId } = productStateStore();

const { danger } = useAlertToast();

Expand Down Expand Up @@ -233,20 +230,6 @@ const updateProductData = async (event, eventType) => {
setCurrentRequestDelayedId(timeoutId);
};

const handleProductFormChange = (event) => {
setFormChanged(true);

prestashop.emit('updateProduct', {
eventType: 'updatedProductCombination',
event,
// Following variables are not used anymore, but kept for backward compatibility
resp: {},
reason: {
productUrl: prestashop.urls.pages.product || '',
},
});
};

const handleUpdateCart = (event) => {
if (!event || !event.reason || event.reason.linkAction !== 'add-to-cart') {
return;
Expand All @@ -265,8 +248,6 @@ const handleUpdateProduct = ({ event, eventType }) => {
};

const attachEventListeners = () => {
on(document, 'change', `${prestashop.selectors.product.variants} *[name]`, handleProductFormChange);

prestashop.on('updateCart', handleUpdateCart);
// Refresh all the product content
prestashop.on('updateProduct', handleUpdateProduct);
Expand Down
4 changes: 2 additions & 2 deletions _dev/js/theme/core/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ prestashop.selectors = {
customizationModal: '.js-customization-modal',
imageContainer: '.js-quickview js-images-container, .page-product:not(.js-quickview-open) .js-product-container .images-container',
container: '.js-product-container',
availability: '#product-availability, .js-product-availability',
actions: '.product-actions, .js-product-actions',
availability: '.js-product-availability',
actions: '.js-product-actions',
variants: '.js-product-variants',
refresh: '.js-product-refresh',
miniature: '.js-product-miniature',
Expand Down

0 comments on commit c55cdfc

Please sign in to comment.