Fix broken preset (#12469) (#16119)

This commit is contained in:
Jan Arends
2022-10-24 20:42:54 +02:00
committed by GitHub
parent efe7dce5bc
commit 51bb1f6464
4 changed files with 27 additions and 10 deletions

View File

@@ -97,11 +97,15 @@ export default defineDisplay({
types: ['alias', 'string', 'uuid', 'integer', 'bigInteger', 'json'],
localTypes: ['m2m', 'm2o', 'o2m', 'translations', 'm2a', 'file', 'files'],
fields: (options: Options | null, { field, collection }) => {
const { junctionCollection, relatedCollection, path } = getRelatedCollection(collection, field);
const fieldsStore = useFieldsStore();
const primaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(relatedCollection);
const relatedCollectionData = getRelatedCollection(collection, field);
if (!relatedCollection) return [];
if (!relatedCollectionData) return [];
const fieldsStore = useFieldsStore();
const { junctionCollection, relatedCollection, path } = relatedCollectionData;
const primaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(relatedCollection);
const fields = options?.template
? adjustFieldsForDisplays(getFieldsFromTemplate(options.template), junctionCollection ?? relatedCollection)

View File

@@ -17,7 +17,7 @@ export default defineDisplay({
handler: (values, options, { collection, field }) => {
if (!field || !collection || !Array.isArray(values)) return values;
const relatedCollections = getRelatedCollection(collection, field.field);
const relatedCollection = getRelatedCollection(collection, field.field);
const fieldsStore = useFieldsStore();
const relationsStore = useRelationsStore();
@@ -34,9 +34,11 @@ export default defineDisplay({
(relation) => relation.collection === junction.collection && relation.field === junction.meta?.junction_field
);
const primaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(relatedCollections.relatedCollection);
if (!relatedCollection) return values;
if (!relatedCollections || !primaryKeyField || !relation?.related_collection) return values;
const primaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(relatedCollection.relatedCollection);
if (!primaryKeyField || !relation?.related_collection) return values;
const relatedPrimaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(relation.related_collection);
@@ -61,7 +63,7 @@ export default defineDisplay({
const fields = fieldKeys.map((fieldKey) => {
return {
key: fieldKey,
field: fieldsStore.getField(relatedCollections.relatedCollection, fieldKey),
field: fieldsStore.getField(relatedCollection.relatedCollection, fieldKey),
};
});

View File

@@ -79,3 +79,11 @@ test('Returns M2O from related_collection rather than collection', () => {
relatedCollection: 'articles',
});
});
test('Returns null if no relation exists in the relationsStore', () => {
const relationsStore = useRelationsStore();
(relationsStore.getRelationsForField as Mock).mockReturnValue([]);
expect(getRelatedCollection('users', 'favorite_article')).toEqual(null);
});

View File

@@ -14,12 +14,15 @@ export interface RelatedCollectionData {
*
* @param collection - Name of the current parent collection
* @param field - Name of the relational field in the current collection
* @returns Related collection name(s)
* @returns Related collection name(s) or null if no related collection exists in the relationsStore
*/
export function getRelatedCollection(collection: string, field: string): RelatedCollectionData {
export function getRelatedCollection(collection: string, field: string): RelatedCollectionData | null {
const relationsStore = useRelationsStore();
const relations: Relation[] = relationsStore.getRelationsForField(collection, field);
if (relations.length === 0) return null;
const localType = getLocalTypeForField(collection, field);
const o2mTypes = ['o2m', 'm2m', 'm2a', 'translations', 'files'];