Skip to content

Commit

Permalink
eslint fix and minor requests refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Sep 19, 2023
1 parent b33ece3 commit 92211e4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 43 deletions.
19 changes: 9 additions & 10 deletions _dev/js/theme/components/http/useHttpPayloadDefinition.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const useHttpPayloadDefinition = (payload, definition) => {
const errors = [];
const ERROR_MESSAGES = {
REQUIRED: 'field is required',
TYPE: 'field must be of type',
Expand Down Expand Up @@ -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 = {};
Expand All @@ -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,
Expand All @@ -54,7 +53,7 @@ const useHttpPayloadDefinition = (payload, definition) => {
minValue,
maxValue,
regex,
} = definition;
} = fieldDefinition;

if (required && !value) {
validateErrors.push(`'${fieldName}' ${ERROR_MESSAGES.REQUIRED}`);
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions _dev/js/theme/core/cart/request/addVoucherToCartRequest.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import prestashop from 'prestashop';
import useHttpRequest from '../../../components/http/useHttpRequest';
import useDefaultHttpRequest from '../../../components/http/useDefaultHttpRequest';

/**
* @typedef ServerResponse
Expand All @@ -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,
Expand Down
17 changes: 2 additions & 15 deletions _dev/js/theme/core/checkout/request/selectDeliveryMethodRequest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import prestashop from 'prestashop';
import useHttpRequest from '../../../components/http/useHttpRequest';
import useDefaultHttpRequest from '../../../components/http/useDefaultHttpRequest';

/**
* @typedef ServerResponse
Expand Down Expand Up @@ -30,26 +29,14 @@ import useHttpRequest from '../../../components/http/useHttpRequest';
* @returns {{getRequest: (function(): Promise<ServerResponse>)}}
*/
const selectDeliveryMethodRequest = (url, payload) => {
const { request } = useHttpRequest(url);

// payload not typed because delivery option parameter is dynamic
const payloadToSend = {
ajax: 1,
action: 'selectDeliveryOption',
...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,
Expand Down

0 comments on commit 92211e4

Please sign in to comment.