Merge pull request #15139 from strapi/fix/avoid-undefined-paths-in-recursion

This commit is contained in:
Josh 2022-12-09 17:07:57 +00:00 committed by GitHub
commit 03f2de9b63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 3 deletions

View File

@ -111,7 +111,7 @@ const DynamicZoneComponent = ({
<StyledBox hasRadius>
<Accordion expanded={isOpen} onToggle={handleToggle} size="S" error={errorMessage}>
<AccordionToggle
startIcon={<FontAwesomeIcon icon={icon} />}
startIcon={icon && <FontAwesomeIcon icon={icon} />}
action={
<Stack horizontal spacing={0} expanded={isOpen}>
{showDownIcon && (

View File

@ -55,8 +55,15 @@ const recursivelyFindPathsBasedOnConditionSetup = (components, predicate = () =>
*
* NOTE: we don't need to know the path to the `array` because it's about data shape not about the actual data
*/
}).map((path) => path.split(`${componentName}.`)[1]);
}).map((path) => {
return path.split(`${componentName}.`)[1];
});
})
/**
* We filter because this will give you `dynamiczone.undefined` because the dynamic_zone component
* is not required to be returned in this circumstance.
*/
.filter((path) => Boolean(path))
.map((path) => `${key}.${path}`);
acc = [...acc, attributesInDynamicComponents];

View File

@ -611,4 +611,79 @@ describe('recursivelyFindPathsBasedOnCondition', () => {
expect(actual).toEqual(['dynamic_relations', 'dynamic_relations.simple']);
});
});
describe('components', () => {
test('given that a component exits, it should be returned', () => {
const components = {
'basic.simple': {
attributes: {
id: {
type: 'integer',
},
categories: {
type: 'relation',
relation: 'oneToMany',
target: 'api::category.category',
targetModel: 'api::category.category',
relationType: 'oneToMany',
},
my_name: {
type: 'string',
},
},
},
};
const attributes = {
relation: {
type: 'component',
component: 'basic.simple',
repeatable: false,
},
};
const actual = recursivelyFindPathsBasedOnCondition(
components,
(value) => value.type === 'component' && !value.repeatable
)(attributes);
expect(actual).toEqual(['relation']);
});
test('given that a component is in a dynamic zone it should not return the name of the dynamic zone', () => {
const components = {
'basic.simple': {
attributes: {
id: {
type: 'integer',
},
categories: {
type: 'relation',
relation: 'oneToMany',
target: 'api::category.category',
targetModel: 'api::category.category',
relationType: 'oneToMany',
},
my_name: {
type: 'string',
},
},
},
};
const attributes = {
dynamic_relations: {
type: 'dynamiczone',
components: ['basic.simple'],
},
};
const actual = recursivelyFindPathsBasedOnCondition(
components,
(value) => value.type === 'component' && value.repeatable === false
)(attributes);
expect(actual).toEqual([]);
});
});
});

View File

@ -40,7 +40,7 @@ export const RelationInputDataManager = ({
const { connectRelation, disconnectRelation, loadRelation, modifiedData, slug, initialData } =
useCMEditViewDataManager();
const relationsFromModifiedData = get(modifiedData, name) ?? [];
const relationsFromModifiedData = get(modifiedData, name, []);
const currentLastPage = Math.ceil(get(initialData, name, []).length / RELATIONS_TO_DISPLAY);