-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #287 from Oksydan/core-js-refacto
Core js refacto part v3
- Loading branch information
Showing
33 changed files
with
874 additions
and
548 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,31 @@ | ||
import $ from 'jquery'; | ||
import prestashop from 'prestashop'; | ||
import DOMReady from '../utils/DOMReady'; | ||
import parseToHtml from '../utils/parseToHtml'; | ||
|
||
$(() => { | ||
prestashop.on('clickQuickView', (elm) => { | ||
const data = { | ||
action: 'quickview', | ||
id_product: elm.dataset.idProduct, | ||
id_product_attribute: elm.dataset.idProductAttribute, | ||
}; | ||
$.post(prestashop.urls.pages.product, data, null, 'json') | ||
.then((resp) => { | ||
const $body = $('body'); | ||
$('body').append(resp.quickview_html); | ||
const productModal = $( | ||
`#quickview-modal-${resp.product.id}-${resp.product.id_product_attribute}`, | ||
); | ||
productModal.modal('show'); | ||
$body.addClass('js-quickview-open'); | ||
productModal.on('hidden.bs.modal', () => { | ||
productModal.remove(); | ||
$body.removeClass('js-quickview-open'); | ||
}); | ||
}) | ||
.fail((resp) => { | ||
prestashop.emit('handleError', { | ||
eventType: 'clickQuickView', | ||
resp, | ||
}); | ||
}); | ||
/** | ||
* Handle open quick view | ||
*/ | ||
const handleQuickViewOpen = ({ | ||
resp, | ||
}) => { | ||
const body = document.querySelector('body'); | ||
body.append(parseToHtml(resp.quickview_html)); | ||
|
||
// TO DO REMOVE JQUERY | ||
const productModal = $( | ||
`#quickview-modal-${resp.product.id}-${resp.product.id_product_attribute}`, | ||
); | ||
productModal.modal('show'); | ||
|
||
body.classList.add('js-quickview-open'); | ||
|
||
productModal.on('hidden.bs.modal', () => { | ||
productModal.remove(); | ||
body.classList.remove('js-quickview-open'); | ||
}); | ||
}; | ||
|
||
DOMReady(() => { | ||
prestashop.on('clickViewOpen', handleQuickViewOpen); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
_dev/js/theme/core/product/handler/product/productFormChangeHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import prestashop from 'prestashop'; | ||
import productStateStore from '../../store/productStateStore'; | ||
|
||
const { setFormChanged } = productStateStore(); | ||
|
||
/** | ||
* Sets the form changed state | ||
* Side effect: emits 'updateProduct' event | ||
* @param event {Event} | ||
*/ | ||
const productFormChangeHandler = (event) => { | ||
setFormChanged(true); | ||
|
||
prestashop.emit('updateProduct', { | ||
eventType: 'updatedProductCombination', | ||
event, | ||
}); | ||
}; | ||
|
||
export default productFormChangeHandler; |
41 changes: 41 additions & 0 deletions
41
_dev/js/theme/core/product/handler/product/productPopStateHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import prestashop from 'prestashop'; | ||
import productFormDataPersister from '../../persister/productFormDataPersister'; | ||
import productStateStore from '../../store/productStateStore'; | ||
|
||
const { setOnPopState, isFormChanged } = productStateStore(); | ||
|
||
const { get } = productFormDataPersister(); | ||
|
||
/** | ||
* Handle popstate event for product page | ||
* Side effect: emits 'updateProduct' event, sets popState in productStateStore | ||
* @param {Event} event | ||
*/ | ||
const productPopStateHandler = (event) => { | ||
setOnPopState(true); | ||
|
||
const formData = event?.state?.form || get(); | ||
|
||
if ((!formData || formData?.length === 0) && !isFormChanged()) { | ||
return; | ||
} | ||
|
||
const form = document.querySelector(`${prestashop.selectors.product.actions} .js-product-form`); | ||
|
||
const handleFormElementState = (data) => { | ||
const element = form.querySelector(`[name="${data.name}"]`); | ||
|
||
if (element) { | ||
element.value = data.value; | ||
} | ||
}; | ||
|
||
formData.forEach(handleFormElementState); | ||
|
||
prestashop.emit('updateProduct', { | ||
eventType: 'updatedProductCombination', | ||
event, | ||
}); | ||
}; | ||
|
||
export default productPopStateHandler; |
15 changes: 15 additions & 0 deletions
15
_dev/js/theme/core/product/handler/product/productUpdateErrorHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import useAlertToast from '../../../../components/useAlertToast'; | ||
|
||
const { danger } = useAlertToast(); | ||
|
||
/** | ||
* Handle product update error | ||
* @param event | ||
*/ | ||
const productUpdateErrorHandler = (event) => { | ||
if (event?.errorMessage) { | ||
danger(event.errorMessage); | ||
} | ||
}; | ||
|
||
export default productUpdateErrorHandler; |
27 changes: 27 additions & 0 deletions
27
_dev/js/theme/core/product/handler/product/updateProductCustomizationHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import prestashop from 'prestashop'; | ||
|
||
/** | ||
* Update product customization input value | ||
* Side effect: update product customization input value | ||
* @param eventType {string} - event type | ||
* @param eventData {object} - event data | ||
* @param eventData.id_customization {number} - customization id | ||
* @return {void} | ||
*/ | ||
const updateProductCustomizationHandler = (eventType, { id_customization: idCustomization }) => { | ||
const customizationIdInput = document.querySelector(prestashop.selectors.cart.productCustomizationId); | ||
|
||
// refill customizationId input value when updating quantity or combination | ||
if ( | ||
(eventType === 'updatedProductQuantity' || eventType === 'updatedProductCombination') | ||
&& idCustomization | ||
) { | ||
if (customizationIdInput) { | ||
customizationIdInput.value = idCustomization; | ||
} | ||
} else if (customizationIdInput) { | ||
customizationIdInput.value = 0; | ||
} | ||
}; | ||
|
||
export default updateProductCustomizationHandler; |
Oops, something went wrong.