-
-
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.
- Loading branch information
Showing
6 changed files
with
121 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import useEvent from '../../components/event/useEvent'; | ||
import changeAddressCountryHandler from './handler/changeAddressCountryHandler'; | ||
|
||
const { on } = useEvent(); | ||
|
||
const addressController = () => { | ||
const init = () => { | ||
on(document, 'change', '.js-country', changeAddressCountryHandler); | ||
}; | ||
|
||
return { | ||
init, | ||
}; | ||
}; | ||
|
||
export default addressController; |
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,6 +1,8 @@ | ||
import $ from 'jquery'; | ||
import countryAddressChange from '@js/theme/core/address/countryAddressChange'; | ||
import addressController from './addressController'; | ||
import DOMReady from '../../utils/DOMReady'; | ||
|
||
$(() => { | ||
countryAddressChange(); | ||
const { init } = addressController(); | ||
|
||
DOMReady(() => { | ||
init(); | ||
}); |
80 changes: 80 additions & 0 deletions
80
_dev/js/theme/core/address/request/updateAddressRequest.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,80 @@ | ||
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 url {string} - new url with from-xhr param | ||
* @param payload {object} - payload for request | ||
* @param payload.id_country {int} - country id | ||
* @param payload.id_address {int} - address id | ||
* @example | ||
* const url = 'address-form.com/url'; // url to update address form | ||
* const payload = { | ||
* id_address: 1, | ||
* id_country: 1, | ||
* } | ||
* const { getRequest } = updateAddressRequest(url, payload); | ||
* | ||
* try { | ||
* const resp = await getRequest(); | ||
* } catch (error) { | ||
* console.error(error); | ||
* } | ||
* @returns {{getRequest: (function(): Promise<ServerResponse>)}} | ||
*/ | ||
const updateAddressRequest = (url, payload) => { | ||
const { request, controller } = useHttpRequest(url); | ||
|
||
const payloadDefinition = { | ||
id_country: { | ||
type: 'int', | ||
required: true, | ||
}, | ||
id_address: { | ||
type: 'int', | ||
required: true, | ||
}, | ||
}; | ||
|
||
const { validatePayload } = useHttpPayloadDefinition(payload, payloadDefinition); | ||
|
||
const validationErrors = validatePayload(); | ||
|
||
if (validationErrors.length) { | ||
throw Error(validationErrors.join(',\n')); | ||
} | ||
|
||
const getRequest = () => new Promise((resolve, reject) => { | ||
abortAll(); | ||
|
||
dispatch(request, controller)(() => request | ||
.query(payload) | ||
.post() | ||
.json((resp) => { | ||
resolve(resp); | ||
}) | ||
.catch((e) => { | ||
// IF ABORTED | ||
if (e instanceof DOMException) { | ||
return; | ||
} | ||
|
||
reject(e); | ||
})); | ||
}); | ||
|
||
return { | ||
getRequest, | ||
}; | ||
}; | ||
|
||
export default updateAddressRequest; |
This file was deleted.
Oops, something went wrong.
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