Skip to content

Commit

Permalink
change address refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Sep 26, 2023
1 parent 79e5a8e commit 2e081ac
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 51 deletions.
16 changes: 16 additions & 0 deletions _dev/js/theme/core/address/addressController.js
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;
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import prestashop from 'prestashop';
import parseToHtml from '@js/theme/utils/parseToHtml';
import useAlertToast from '@js/theme/components/useAlertToast';
import useEvent from '@js/theme/components/event/useEvent';
import useAlertToast from '../../../components/useAlertToast';
import parseToHtml from '../../../utils/parseToHtml';
import updateAddressRequest from '../request/updateAddressRequest';

const { danger } = useAlertToast();
const { on } = useEvent();

const handleAddressChange = async () => {
/**
* Change address country handler
* @returns {Promise<void>}
*/
const changeAddressCountryHandler = async () => {
const DOMSelectors = {
addressFormWrapperSelector: '.js-address-form',
countrySelectSelector: '.js-country',
Expand All @@ -29,11 +32,18 @@ const handleAddressChange = async () => {
formInputs,
} = getDOMAddressElements();
const url = addressForm.dataset?.refreshUrl;
const idCountry = countrySelect.value;
const idAddress = addressForm.dataset?.idAddress;
const idCountry = Number.parseInt(countrySelect.value, 10);
const idAddress = Number.parseInt(addressForm.dataset?.idAddress, 10);

const payload = {
id_address: idAddress,
id_country: idCountry,
};

const { getRequest } = updateAddressRequest(url, payload);

try {
const data = await prestashop.frontAPI.updateAddress(url, idAddress, idCountry);
const data = await getRequest();

const inputsValue = [];

Expand Down Expand Up @@ -70,8 +80,4 @@ const handleAddressChange = async () => {
}
};

const countryAddressChange = () => {
on(document, 'change', '.js-country', handleAddressChange);
};

export default countryAddressChange;
export default changeAddressCountryHandler;
10 changes: 6 additions & 4 deletions _dev/js/theme/core/address/index.js
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 _dev/js/theme/core/address/request/updateAddressRequest.js
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;
32 changes: 0 additions & 32 deletions _dev/js/theme/frontAPI/address/updateAddressAction.js

This file was deleted.

2 changes: 0 additions & 2 deletions _dev/js/theme/frontAPI/apiAction.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import updateProductAction from '@js/theme/frontAPI/product/updateProductAction';
import updateAddressAction from '@js/theme/frontAPI/address/updateAddressAction';

prestashop.frontAPI = {};

Expand All @@ -12,4 +11,3 @@ prestashop.addAction = (actionName, actionFunction) => {
};

prestashop.addAction('updateProduct', updateProductAction);
prestashop.addAction('updateAddress', updateAddressAction);

0 comments on commit 2e081ac

Please sign in to comment.