fix: consider variable types when traversing collection fields (#3347)

This commit is contained in:
Erez Rokah 2020-02-28 20:06:34 +01:00 committed by GitHub
parent d6715c22bc
commit 961cde4ea5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 10 deletions

View File

@ -120,6 +120,15 @@ describe('collections', () => {
}, },
], ],
}, },
{
name: 'list_3',
types: [
{
name: 'list_3_type',
media_folder: 'list_3_type_media_folder',
},
],
},
], ],
}), }),
), ),
@ -134,6 +143,10 @@ describe('collections', () => {
name: 'list_2_item', name: 'list_2_item',
media_folder: 'list_2_item_media_folder', media_folder: 'list_2_item_media_folder',
}), }),
fromJS({
name: 'list_3_type',
media_folder: 'list_3_type_media_folder',
}),
]); ]);
}); });
@ -182,6 +195,15 @@ describe('collections', () => {
name: 'list_2_item', name: 'list_2_item',
media_folder: 'list_2_item_media_folder', media_folder: 'list_2_item_media_folder',
}, },
{
name: 'list_3',
types: [
{
name: 'list_3_type',
media_folder: 'list_3_type_media_folder',
},
],
},
], ],
}, },
], ],
@ -195,6 +217,10 @@ describe('collections', () => {
name: 'list_2_item', name: 'list_2_item',
media_folder: 'list_2_item_media_folder', media_folder: 'list_2_item_media_folder',
}), }),
fromJS({
name: 'list_3_type',
media_folder: 'list_3_type_media_folder',
}),
]); ]);
}); });
}); });
@ -219,11 +245,19 @@ describe('collections', () => {
name: 'image', name: 'image',
media_folder: '{{media_folder}}/customers/', media_folder: '{{media_folder}}/customers/',
}, },
{
name: 'list',
types: [{ name: 'widget', media_folder: '{{media_folder}}/widgets' }],
},
], ],
}), }),
fromJS({ slug: 'name', path: 'src/post/post1.md', data: {} }), fromJS({ slug: 'name', path: 'src/post/post1.md', data: {} }),
), ),
).toEqual(['static/img/general', 'static/img/general/customers']); ).toEqual([
'static/img/general',
'static/img/general/customers',
'static/img/general/widgets',
]);
}); });
it('should return fields, file and collection folders', () => { it('should return fields, file and collection folders', () => {
@ -242,6 +276,10 @@ describe('collections', () => {
name: 'image', name: 'image',
media_folder: '{{media_folder}}/logos/', media_folder: '{{media_folder}}/logos/',
}, },
{
name: 'list',
types: [{ name: 'widget', media_folder: '{{media_folder}}/widgets' }],
},
], ],
}, },
], ],
@ -252,6 +290,7 @@ describe('collections', () => {
'static/img/general', 'static/img/general',
'static/img/general/customers', 'static/img/general/customers',
'static/img/general/customers/logos', 'static/img/general/customers/logos',
'static/img/general/customers/widgets',
]); ]);
}); });
}); });
@ -270,18 +309,25 @@ describe('collections', () => {
{ name: 'en', fields: [{ name: 'title' }, { name: 'body' }] }, { name: 'en', fields: [{ name: 'title' }, { name: 'body' }] },
{ name: 'es', fields: [{ name: 'title' }, { name: 'body' }] }, { name: 'es', fields: [{ name: 'title' }, { name: 'body' }] },
{ name: 'it', field: { name: 'title', fields: [{ name: 'subTitle' }] } }, { name: 'it', field: { name: 'title', fields: [{ name: 'subTitle' }] } },
{
name: 'fr',
fields: [{ name: 'title', widget: 'list', types: [{ name: 'variableType' }] }],
},
], ],
}); });
expect(getFieldsNames(collection.get('fields').toArray())).toEqual([ expect(getFieldsNames(collection.get('fields').toArray())).toEqual([
'en', 'en',
'es', 'es',
'it', 'it',
'fr',
'en.title', 'en.title',
'en.body', 'en.body',
'es.title', 'es.title',
'es.body', 'es.body',
'it.title', 'it.title',
'it.title.subTitle', 'it.title.subTitle',
'fr.title',
'fr.title.variableType',
]); ]);
}); });
}); });
@ -300,6 +346,10 @@ describe('collections', () => {
{ name: 'en', fields: [{ name: 'title' }, { name: 'body' }] }, { name: 'en', fields: [{ name: 'title' }, { name: 'body' }] },
{ name: 'es', fields: [{ name: 'title' }, { name: 'body' }] }, { name: 'es', fields: [{ name: 'title' }, { name: 'body' }] },
{ name: 'it', field: { name: 'title', fields: [{ name: 'subTitle' }] } }, { name: 'it', field: { name: 'title', fields: [{ name: 'subTitle' }] } },
{
name: 'fr',
fields: [{ name: 'title', widget: 'list', types: [{ name: 'variableType' }] }],
},
], ],
}); });
@ -319,6 +369,16 @@ describe('collections', () => {
.get('fields') .get('fields')
.get(0), .get(0),
); );
expect(selectField(collection, 'fr.title.variableType')).toBe(
collection
.get('fields')
.get(3)
.get('fields')
.get(0)
.get('types')
.get(0),
);
}); });
}); });
}); });

View File

@ -289,7 +289,12 @@ describe('entries', () => {
it('should cascade media_folders', () => { it('should cascade media_folders', () => {
const mainImageField = fromJS({ name: 'main_image' }); const mainImageField = fromJS({ name: 'main_image' });
const logoField = fromJS({ name: 'logo', media_folder: '{{media_folder}}/logos/' }); const logoField = fromJS({ name: 'logo', media_folder: '{{media_folder}}/logos/' });
const nestedField2 = fromJS({ name: 'nested', media_folder: '{{media_folder}}/nested2/' }); const nestedField3 = fromJS({ name: 'nested', media_folder: '{{media_folder}}/nested3/' });
const nestedField2 = fromJS({
name: 'nested',
media_folder: '{{media_folder}}/nested2/',
types: [nestedField3],
});
const nestedField1 = fromJS({ const nestedField1 = fromJS({
name: 'nested', name: 'nested',
media_folder: '{{media_folder}}/nested1/', media_folder: '{{media_folder}}/nested1/',
@ -324,6 +329,9 @@ describe('entries', () => {
expect(selectMediaFolder(...args, nestedField2)).toBe( expect(selectMediaFolder(...args, nestedField2)).toBe(
'static/img/general/customers/nested/nested1/nested2', 'static/img/general/customers/nested/nested1/nested2',
); );
expect(selectMediaFolder(...args, nestedField3)).toBe(
'static/img/general/customers/nested/nested1/nested2/nested3',
);
}); });
}); });

View File

@ -124,10 +124,12 @@ const getFieldsWithMediaFolders = (fields: EntryField[]) => {
if (f.has('fields')) { if (f.has('fields')) {
const fields = f.get('fields')?.toArray() as EntryField[]; const fields = f.get('fields')?.toArray() as EntryField[];
acc = [...acc, ...getFieldsWithMediaFolders(fields)]; acc = [...acc, ...getFieldsWithMediaFolders(fields)];
} } else if (f.has('field')) {
if (f.has('field')) {
const field = f.get('field') as EntryField; const field = f.get('field') as EntryField;
acc = [...acc, ...getFieldsWithMediaFolders([field])]; acc = [...acc, ...getFieldsWithMediaFolders([field])];
} else if (f.has('types')) {
const types = f.get('types')?.toArray() as EntryField[];
acc = [...acc, ...getFieldsWithMediaFolders(types)];
} }
return acc; return acc;
@ -200,10 +202,12 @@ export const getFieldsNames = (fields: EntryField[], prefix = '') => {
if (f.has('fields')) { if (f.has('fields')) {
const fields = f.get('fields')?.toArray() as EntryField[]; const fields = f.get('fields')?.toArray() as EntryField[];
names = [...names, ...getFieldsNames(fields, `${names[index]}.`)]; names = [...names, ...getFieldsNames(fields, `${names[index]}.`)];
} } else if (f.has('field')) {
if (f.has('field')) {
const field = f.get('field') as EntryField; const field = f.get('field') as EntryField;
names = [...names, ...getFieldsNames([field], `${names[index]}.`)]; names = [...names, ...getFieldsNames([field], `${names[index]}.`)];
} else if (f.has('types')) {
const types = f.get('types')?.toArray() as EntryField[];
names = [...names, ...getFieldsNames(types, `${names[index]}.`)];
} }
}); });
@ -219,9 +223,10 @@ export const selectField = (collection: Collection, key: string) => {
field = fields.find(f => f.get('name') === name); field = fields.find(f => f.get('name') === name);
if (field?.has('fields')) { if (field?.has('fields')) {
fields = field?.get('fields')?.toArray() as EntryField[]; fields = field?.get('fields')?.toArray() as EntryField[];
} } else if (field?.has('field')) {
if (field?.has('field')) {
fields = [field?.get('field') as EntryField]; fields = [field?.get('field') as EntryField];
} else if (field?.has('types')) {
fields = field?.get('types')?.toArray() as EntryField[];
} }
} }

View File

@ -208,8 +208,9 @@ const traverseFields = (
folderKey, folderKey,
config.get('slug'), config.get('slug'),
); );
let fieldFolder = null;
if (f.has('fields')) { if (f.has('fields')) {
return traverseFields( fieldFolder = traverseFields(
folderKey, folderKey,
config, config,
collection, collection,
@ -219,7 +220,7 @@ const traverseFields = (
folder, folder,
); );
} else if (f.has('field')) { } else if (f.has('field')) {
return traverseFields( fieldFolder = traverseFields(
folderKey, folderKey,
config, config,
collection, collection,
@ -228,6 +229,19 @@ const traverseFields = (
[f.get('field')!], [f.get('field')!],
folder, folder,
); );
} else if (f.has('types')) {
fieldFolder = traverseFields(
folderKey,
config,
collection,
entryMap,
field,
f.get('types')!.toArray(),
folder,
);
}
if (fieldFolder != null) {
return fieldFolder;
} }
} }

View File

@ -92,6 +92,7 @@ export type EntryDraft = StaticallyTypedRecord<{
export type EntryField = StaticallyTypedRecord<{ export type EntryField = StaticallyTypedRecord<{
field?: EntryField; field?: EntryField;
fields?: List<EntryField>; fields?: List<EntryField>;
types?: List<EntryField>;
widget: string; widget: string;
name: string; name: string;
default: string | null; default: string | null;