Skip to content

Commit

Permalink
add comments to unclear functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vashjs committed Feb 6, 2025
1 parent 30ecc8b commit f75bf4f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,12 @@ export const ContentUpdatesForm = ({ fields, setFields, options }) => {
return field;
});

const finalizedFields = hasActionChanged
? getFieldsWithRules({ fields: mappedFields, option, value, rowIndex })
// Check if there are rules should be applied based on if action changed and option value
const fieldsWithRules = hasActionChanged
? getFieldsWithRules({ fields: mappedFields, option, value })
: mappedFields;

setFields(finalizedFields);
setFields(fieldsWithRules);
};

const handleRemove = (index) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,18 +403,40 @@ export const getActionIndex = (fields, action) => fields.findIndex(({ option, ac
.some(({ name }) => name === action);
});

export const getExceptionalOptions = (fields) => {
/**
* Checks if there is at least one field with the STATISTICAL_CODE option that contains
* either an ADD_TO_EXISTING or REMOVE_SOME action.
*/
const hasAddOrRemoveFieldAction = (fields) => {
const addActionIndex = getActionIndex(fields, ACTIONS.ADD_TO_EXISTING);
const removeActionIndex = getActionIndex(fields, ACTIONS.REMOVE_SOME);

return addActionIndex !== -1 || removeActionIndex !== -1 ? [OPTIONS.STATISTICAL_CODE] : [];
return addActionIndex !== -1 || removeActionIndex !== -1;
};

/**
* Determines the additional options that should be applied based on the provided fields.
* If the number of STATISTICAL_CODE fields is not exactly 2 and at least one STATISTICAL_CODE field
* has an ADD_TO_EXISTING or REMOVE_SOME action, this function returns an array containing STATISTICAL_CODE.
* It will be used to show/hide the extra STATISTICAl_CODE option in options list.
*/
export const getAdditionalOptions = (fields) => {
const statisticalCodeFields = fields.filter(({ option }) => option === OPTIONS.STATISTICAL_CODE);
const hasAddOrRemove = hasAddOrRemoveFieldAction(fields);

return statisticalCodeFields.length !== 2 && hasAddOrRemove ? [OPTIONS.STATISTICAL_CODE] : [];
};

/**
* Filters and updates the provided fields by marking options as hidden based on other fields’ options.
* For each field, it computes the unique set of options available among all fields and then hides
* options that are present in other fields – except for those that are returned by getAdditionalOptions.
*/
export const getFilteredFields = (initialFields) => {
return initialFields.map(f => {
const uniqOptions = new Set(initialFields.map(i => i.option));

const optionsExceptCurrent = [...uniqOptions].filter(u => u !== f.option && !getExceptionalOptions(initialFields).includes(u));
const optionsExceptCurrent = [...uniqOptions].filter(u => u !== f.option && !getAdditionalOptions(initialFields).includes(u));

return {
...f,
Expand All @@ -423,17 +445,31 @@ export const getFilteredFields = (initialFields) => {
});
};

export const getFieldsWithRules = ({ fields, option, value, rowIndex }) => {
if (option !== OPTIONS.STATISTICAL_CODE) return fields;

return fields.map((field, i) => {
const isCurrentRow = i === rowIndex;
const isStatisticalCode = field.option === OPTIONS.STATISTICAL_CODE;
/**
* Determines whether an "add" button should be shown for fields.
* The add button is shown if:
* - The provided index corresponds to the last field in the array, and
* - The total number of fields is less than the allowed maximum.
* The allowed maximum is defined as the length of the base options plus 1 if any STATISTICAL_CODE field
* contains an ADD_TO_EXISTING or REMOVE_SOME action as exceptional case.
*/
export const isAddButtonShown = (index, fields, options) => {
const additionalFieldsCount = hasAddOrRemoveFieldAction(fields) ? 1 : 0;
const maxFieldsLength = options.length + additionalFieldsCount;
return index === fields.length - 1 && fields.length < maxFieldsLength;
};

if (value === ACTIONS.REMOVE_ALL && isStatisticalCode && !isCurrentRow) {
return null; // Remove this item
}
/**
* Updates the fields with new visibility rules for their options.
* For now supports only the STATISTICAL_CODE option.
* If the provided option is not STATISTICAL_CODE, the fields are returned unchanged.
* Otherwise, for each field, if an option's value is STATISTICAL_CODE, its 'hidden' property is set to true
* if the provided value equals REMOVE_ALL; otherwise, it remains unchanged.
*/
export const getFieldsWithRules = ({ fields, option, value }) => {
if (option !== OPTIONS.STATISTICAL_CODE) return fields;

return fields.map((field) => {
return {
...field,
options: field.options.map(o => ({
Expand All @@ -443,9 +479,16 @@ export const getFieldsWithRules = ({ fields, option, value, rowIndex }) => {
: o.hidden,
})),
};
}).filter(Boolean); // Remove null values
});
};

/**
* Normalizes the rules for fields having the STATISTICAL_CODE option.
* This function extracts each STATISTICAL_CODE field’s selected actions and available actions
* (from its actionsDetails), then adjusts the available actions for fields that are not the primary
* (first) field having either an ADD_TO_EXISTING or REMOVE_SOME action.
* The result is returned as an object keyed by the original field indices.
*/
export const getNormalizedFieldsRules = (fields) => {
const statisticalCodeFields = fields.reduce((acc, field, index) => {
const actions = field.actionsDetails.actions.filter(Boolean);
Expand Down Expand Up @@ -496,11 +539,6 @@ export const getNormalizedFieldsRules = (fields) => {
}, {});
};

export const isAddButtonShown = (index, fields, options) => {
const maxFieldsLength = options.length + getExceptionalOptions(fields).length;
return index === fields.length - 1 && fields.length < maxFieldsLength;
};

export const getExtraActions = (option, action) => {
switch (`${option}-${action}`) {
case `${OPTIONS.ITEM_NOTE}-${ACTIONS.FIND}`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
getNormalizedFieldsRules,
isAddButtonShown,
isContentUpdatesFormValid,
getExceptionalOptions,
getAdditionalOptions,
} from './helpers';

// Mock the functions
Expand Down Expand Up @@ -1404,7 +1404,7 @@ describe('ContentUpdatesForm helpers', () => {
});
});

describe('getExceptionalOptions', () => {
describe('getAdditionalOptions', () => {
it('should return an empty array when no STATISTICAL_CODE field has ADD_TO_EXISTING or REMOVE_SOME actions', () => {
const fields = [
// STATISTICAL_CODE field with no actions
Expand All @@ -1419,7 +1419,7 @@ describe('ContentUpdatesForm helpers', () => {
},
];

expect(getExceptionalOptions(fields)).toEqual([]);
expect(getAdditionalOptions(fields)).toEqual([]);
});

it('should return [STATISTICAL_CODE] when a STATISTICAL_CODE field has an ADD_TO_EXISTING action', () => {
Expand All @@ -1434,7 +1434,7 @@ describe('ContentUpdatesForm helpers', () => {
},
];

expect(getExceptionalOptions(fields)).toEqual([OPTIONS.STATISTICAL_CODE]);
expect(getAdditionalOptions(fields)).toEqual([OPTIONS.STATISTICAL_CODE]);
});

it('should return [STATISTICAL_CODE] when a STATISTICAL_CODE field has a REMOVE_SOME action', () => {
Expand All @@ -1449,10 +1449,10 @@ describe('ContentUpdatesForm helpers', () => {
},
];

expect(getExceptionalOptions(fields)).toEqual([OPTIONS.STATISTICAL_CODE]);
expect(getAdditionalOptions(fields)).toEqual([OPTIONS.STATISTICAL_CODE]);
});

it('should return [STATISTICAL_CODE] when both ADD_TO_EXISTING and REMOVE_SOME actions are found among STATISTICAL_CODE fields', () => {
it('should return [] when both ADD_TO_EXISTING and REMOVE_SOME actions are found among STATISTICAL_CODE fields', () => {
const fields = [
{
option: OPTIONS.STATISTICAL_CODE,
Expand All @@ -1468,7 +1468,7 @@ describe('ContentUpdatesForm helpers', () => {
},
];

expect(getExceptionalOptions(fields)).toEqual([OPTIONS.STATISTICAL_CODE]);
expect(getAdditionalOptions(fields)).toEqual([]);
});

it('should return an empty array if an action exists but the field option is not STATISTICAL_CODE', () => {
Expand All @@ -1483,7 +1483,7 @@ describe('ContentUpdatesForm helpers', () => {
},
];

expect(getExceptionalOptions(fields)).toEqual([]);
expect(getAdditionalOptions(fields)).toEqual([]);
});
});

Expand Down Expand Up @@ -1739,61 +1739,6 @@ describe('ContentUpdatesForm helpers', () => {
expect(result).toEqual(expected);
});

it('should remove non-current rows with STATISTICAL_CODE when value equals REMOVE_ALL', () => {
const fields = [
{
option: OPTIONS.STATISTICAL_CODE,
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: false },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: false },
],
id: 1,
},
{
option: OPTIONS.STATISTICAL_CODE,
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: false },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: false },
],
id: 2,
},
{
option: OPTIONS.ADMINISTRATIVE_NOTE,
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: false },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: false },
],
id: 3,
},
];

const result = getFieldsWithRules({
fields,
option: OPTIONS.STATISTICAL_CODE,
value: ACTIONS.REMOVE_ALL,
rowIndex: 1,
});

const expected = [
{
...fields[1],
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: true },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: false },
],
},
{
...fields[2],
options: [
{ value: OPTIONS.STATISTICAL_CODE, hidden: true },
{ value: OPTIONS.ADMINISTRATIVE_NOTE, hidden: false },
],
},
];

expect(result).toEqual(expected);
});

it('should return an empty array when fields array is empty', () => {
const result = getFieldsWithRules({
fields: [],
Expand Down

0 comments on commit f75bf4f

Please sign in to comment.