-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
404 additions
and
205 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
36 changes: 36 additions & 0 deletions
36
_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,36 @@ | ||
import prestashop from 'prestashop'; | ||
import productFormDataPersister from '../../persister/productFormDataPersister'; | ||
import productStateStore from '../../store/productStateStore'; | ||
|
||
const { setOnPopState, isFormChanged } = productStateStore(); | ||
|
||
const { get } = productFormDataPersister(); | ||
|
||
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} 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; |
11 changes: 11 additions & 0 deletions
11
_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,11 @@ | ||
import useAlertToast from '../../../../components/useAlertToast'; | ||
|
||
const { danger } = useAlertToast(); | ||
|
||
const productUpdateErrorHandler = (event) => { | ||
if (event?.errorMessage) { | ||
danger(event.errorMessage); | ||
} | ||
}; | ||
|
||
export default productUpdateErrorHandler; |
File renamed without changes.
43 changes: 43 additions & 0 deletions
43
_dev/js/theme/core/product/handler/product/updatedProductHandler.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,43 @@ | ||
import isQuickViewOpen from '../../utils/isQuickViewOpen'; | ||
import productStateStore from '../../store/productStateStore'; | ||
|
||
const { isOnPopState, setOnPopState } = productStateStore(); | ||
|
||
const updatedProductHandler = ({ | ||
product_url: responseProductUrl = null, | ||
id_product_attribute: responseIdProductAttribute = null, | ||
product_title: responseProductTitle = '', | ||
}, formData) => { | ||
if (!responseProductUrl || !responseIdProductAttribute) { | ||
return; | ||
} | ||
|
||
/* | ||
* If quickview modal is present we are not on product page, so | ||
* we don't change the url nor title | ||
*/ | ||
if (isQuickViewOpen()) { | ||
return; | ||
} | ||
|
||
const pageTitle = document.title; | ||
|
||
if (responseProductTitle) { | ||
document.title = responseProductTitle; | ||
} | ||
|
||
if (!isOnPopState()) { | ||
window.history.pushState( | ||
{ | ||
id_product_attribute: responseIdProductAttribute, | ||
form: formData, | ||
}, | ||
pageTitle, | ||
responseProductUrl, | ||
); | ||
} | ||
|
||
setOnPopState(false); | ||
}; | ||
|
||
export default updatedProductHandler; |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
.../core/product/handler/quickViewHandler.js → ...uct/handler/quickView/quickViewHandler.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
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
_dev/js/theme/core/product/persister/productFormDataPersister.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,38 @@ | ||
import { formSerializeArray } from '../../../utils/formSerialize'; | ||
|
||
let formData = []; | ||
|
||
/** | ||
* Persists product form data | ||
* @module | ||
*/ | ||
const productFormDataPersister = () => { | ||
/** | ||
* Persists form data from the form element | ||
* @method | ||
* @param {HTMLFormElement} formElement - form element to persist | ||
* @throws {Error} - if formElement is not a form element | ||
* @return {void} | ||
*/ | ||
const persist = (formElement) => { | ||
if (formElement?.tagName !== 'FORM') { | ||
throw new Error('formElement is not a form element'); | ||
} | ||
|
||
formData = formSerializeArray(formElement); | ||
}; | ||
|
||
/** | ||
* Returns persisted data | ||
* @method | ||
* @return {*[]} | ||
*/ | ||
const get = () => formData; | ||
|
||
return { | ||
persist, | ||
get, | ||
}; | ||
}; | ||
|
||
export default productFormDataPersister; |
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
121 changes: 121 additions & 0 deletions
121
_dev/js/theme/core/product/request/product/updateProductRequest.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,121 @@ | ||
import prestashop from 'prestashop'; | ||
import useHttpRequest from '../../../../components/http/useHttpRequest'; | ||
import useHttpController from '../../../../components/http/useHttpController'; | ||
import useHttpPayloadDefinition from '../../../../components/http/useHttpPayloadDefinition'; | ||
|
||
const { dispatch, abortAll } = useHttpController(); | ||
|
||
/** | ||
* @typedef ServerResponse | ||
* @type {object} | ||
* @property {string} address_form - new address form html content | ||
*/ | ||
|
||
/** | ||
* Update listing facets request | ||
* @param payload {object} - payload for request | ||
* @param payload.preview {number} - is preview 1 or 0 | ||
* @param payload.quickview {number} - is quick view 1 or 0 | ||
* @param payload.quantity_wanted {number} - quantity wanted | ||
* @param payload.id_product {number} - product id | ||
* @param payload.id_product_attribute {number} - product attribute id | ||
* @param payload.id_customization {number} - customization id - optional, default 0 | ||
* @param payload.ajax {number} - optional, default 1 | ||
* @param payload.action {string} - optional, default refresh | ||
* @param payload.group[] {array} - array of attributes groups - optional | ||
* @example | ||
* const url = 'address-form.com/url'; // url to update address form | ||
* const payload = { | ||
* id_product: 1, | ||
* id_product_attribute: 1, | ||
* quantity_wanted: 1, | ||
* preview: 0, | ||
* quickview: 0, | ||
* } | ||
* const { getRequest } = updateProductRequest(payload); | ||
* | ||
* try { | ||
* const resp = await getRequest(); | ||
* } catch (error) { | ||
* console.error(error); | ||
* } | ||
* @returns {{getRequest: (function(): Promise<ServerResponse>)}} | ||
*/ | ||
const updateProductRequest = (payload) => { | ||
const { request, controller } = useHttpRequest(prestashop.urls.pages.product); | ||
const payloadToSend = { | ||
ajax: 1, | ||
action: 'refresh', | ||
...payload, | ||
}; | ||
|
||
const payloadDefinition = { | ||
action: { | ||
type: 'string', | ||
required: true, | ||
}, | ||
ajax: { | ||
type: 'int', | ||
required: true, | ||
}, | ||
preview: { | ||
type: 'int', | ||
required: true, | ||
}, | ||
quickview: { | ||
type: 'int', | ||
required: true, | ||
}, | ||
quantity_wanted: { | ||
type: 'int', | ||
required: true, | ||
}, | ||
id_product: { | ||
type: 'int', | ||
required: true, | ||
}, | ||
id_product_attribute: { | ||
type: 'int', | ||
required: true, | ||
}, | ||
id_customization: { | ||
type: 'int', | ||
required: false, | ||
}, | ||
}; | ||
|
||
const { validatePayload } = useHttpPayloadDefinition(payloadToSend, payloadDefinition); | ||
|
||
const validationErrors = validatePayload(); | ||
|
||
if (validationErrors.length) { | ||
throw Error(validationErrors.join(',\n')); | ||
} | ||
|
||
const getRequest = () => { | ||
abortAll(); | ||
|
||
return new Promise((resolve, reject) => { | ||
dispatch(request, controller)(() => request | ||
.query(payloadToSend) | ||
.post() | ||
.json((resp) => { | ||
resolve(resp); | ||
}) | ||
.catch((e) => { | ||
// IF ABORTED | ||
if (e instanceof DOMException) { | ||
return; | ||
} | ||
|
||
reject(); | ||
})); | ||
}); | ||
}; | ||
|
||
return { | ||
getRequest, | ||
}; | ||
}; | ||
|
||
export default updateProductRequest; |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.