diff --git a/_dev/js/theme/components/http/useHttpPayloadDefinition.js b/_dev/js/theme/components/http/useHttpPayloadDefinition.js index 4744788d..b6828bec 100644 --- a/_dev/js/theme/components/http/useHttpPayloadDefinition.js +++ b/_dev/js/theme/components/http/useHttpPayloadDefinition.js @@ -1,5 +1,4 @@ const useHttpPayloadDefinition = (payload, definition) => { - const errors = []; const ERROR_MESSAGES = { REQUIRED: 'field is required', TYPE: 'field must be of type', @@ -31,9 +30,9 @@ const useHttpPayloadDefinition = (payload, definition) => { throw new Error('Payload is required'); } - const setDefaultDefinitionForField = (definition) => ({ + const setDefaultDefinitionForField = (customDefinition) => ({ ...defaultDefinitionForField, - ...definition, + ...customDefinition, }); const payloadDefinition = {}; @@ -44,7 +43,7 @@ const useHttpPayloadDefinition = (payload, definition) => { payloadDefinition[fieldName] = setDefaultDefinitionForField(definitionForField); }); - const validate = (fieldName, value, definition) => { + const validate = (fieldName, value, fieldDefinition) => { const validateErrors = []; const { type, @@ -54,7 +53,7 @@ const useHttpPayloadDefinition = (payload, definition) => { minValue, maxValue, regex, - } = definition; + } = fieldDefinition; if (required && !value) { validateErrors.push(`'${fieldName}' ${ERROR_MESSAGES.REQUIRED}`); @@ -118,9 +117,9 @@ const useHttpPayloadDefinition = (payload, definition) => { return validateErrors; }; - const validateDefinitionForField = (fieldName, definition) => { + const validateDefinitionForField = (fieldName, fieldsDefinition) => { const definitionErrors = []; - const definitionKeys = Object.keys(definition); + const definitionKeys = Object.keys(fieldsDefinition); requiredDefinitionFields.forEach((requiredDefinitionField) => { if (!definitionKeys.includes(requiredDefinitionField)) { @@ -131,13 +130,13 @@ const useHttpPayloadDefinition = (payload, definition) => { return definitionErrors; }; - const validateDefinition = (definition) => { + const validateDefinition = (fieldsDefinition) => { const definitionErrors = []; - const definitionKeys = Object.keys(definition); + const definitionKeys = Object.keys(fieldsDefinition); definitionKeys.forEach((defName) => { const fieldName = defName; - const fieldDef = definition[defName]; + const fieldDef = fieldsDefinition[defName]; const definitionForFieldErrors = validateDefinitionForField(fieldName, fieldDef); definitionErrors.push(...definitionForFieldErrors); diff --git a/_dev/js/theme/core/cart/request/addVoucherToCartRequest.js b/_dev/js/theme/core/cart/request/addVoucherToCartRequest.js index 9ed20233..8273ef3a 100644 --- a/_dev/js/theme/core/cart/request/addVoucherToCartRequest.js +++ b/_dev/js/theme/core/cart/request/addVoucherToCartRequest.js @@ -1,7 +1,6 @@ import prestashop from 'prestashop'; -import useHttpRequest from '../../../components/http/useHttpRequest'; import useDefaultHttpRequest from '../../../components/http/useDefaultHttpRequest'; -import useHttpPayloadDefinition from "../../../components/http/useHttpPayloadDefinition"; +import useHttpPayloadDefinition from '../../../components/http/useHttpPayloadDefinition'; /** * @typedef ServerResponse diff --git a/_dev/js/theme/core/cart/request/deleteVoucherFromCartRequest.js b/_dev/js/theme/core/cart/request/deleteVoucherFromCartRequest.js index 1a6fb27b..9da678e1 100644 --- a/_dev/js/theme/core/cart/request/deleteVoucherFromCartRequest.js +++ b/_dev/js/theme/core/cart/request/deleteVoucherFromCartRequest.js @@ -1,7 +1,6 @@ import prestashop from 'prestashop'; -import useHttpRequest from '../../../components/http/useHttpRequest'; import useDefaultHttpRequest from '../../../components/http/useDefaultHttpRequest'; -import useHttpPayloadDefinition from "../../../components/http/useHttpPayloadDefinition"; +import useHttpPayloadDefinition from '../../../components/http/useHttpPayloadDefinition'; /** * @typedef ServerResponse diff --git a/_dev/js/theme/core/checkout/request/checkCartStillOrderableRequest.js b/_dev/js/theme/core/checkout/request/checkCartStillOrderableRequest.js index d81d4f2f..1793f3c2 100644 --- a/_dev/js/theme/core/checkout/request/checkCartStillOrderableRequest.js +++ b/_dev/js/theme/core/checkout/request/checkCartStillOrderableRequest.js @@ -1,5 +1,5 @@ import prestashop from 'prestashop'; -import useHttpRequest from '../../../components/http/useHttpRequest'; +import useDefaultHttpRequest from '../../../components/http/useDefaultHttpRequest'; /** * @typedef ServerResponse @@ -14,23 +14,12 @@ import useHttpRequest from '../../../components/http/useHttpRequest'; */ const checkCartStillOrderableRequest = () => { // payload not typed because it isn't needed - const payload = { + const payloadToSend = { ajax: 1, action: 'checkCartStillOrderable', }; - const { request } = useHttpRequest(prestashop.urls.pages.order); - const getRequest = () => new Promise((resolve, reject) => { - request - .query(payload) - .post() - .json((resp) => { - resolve(resp); - }) - .catch(() => { - reject(Error(prestashop.t.alert.genericHttpError)); - }); - }); + const getRequest = () => useDefaultHttpRequest(prestashop.urls.pages.order, payloadToSend); return { getRequest, diff --git a/_dev/js/theme/core/checkout/request/selectDeliveryMethodRequest.js b/_dev/js/theme/core/checkout/request/selectDeliveryMethodRequest.js index 6d015fd1..1927f09e 100644 --- a/_dev/js/theme/core/checkout/request/selectDeliveryMethodRequest.js +++ b/_dev/js/theme/core/checkout/request/selectDeliveryMethodRequest.js @@ -1,5 +1,4 @@ -import prestashop from 'prestashop'; -import useHttpRequest from '../../../components/http/useHttpRequest'; +import useDefaultHttpRequest from '../../../components/http/useDefaultHttpRequest'; /** * @typedef ServerResponse @@ -30,8 +29,6 @@ import useHttpRequest from '../../../components/http/useHttpRequest'; * @returns {{getRequest: (function(): Promise)}} */ const selectDeliveryMethodRequest = (url, payload) => { - const { request } = useHttpRequest(url); - // payload not typed because delivery option parameter is dynamic const payloadToSend = { ajax: 1, @@ -39,17 +36,7 @@ const selectDeliveryMethodRequest = (url, payload) => { ...payload, }; - const getRequest = () => new Promise((resolve, reject) => { - request - .query(payloadToSend) - .post() - .json((resp) => { - resolve(resp); - }) - .catch(() => { - reject(Error(prestashop.t.alert.genericHttpError)); - }); - }); + const getRequest = () => useDefaultHttpRequest(url, payloadToSend); return { getRequest,