-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api-headless-cms): gracefully fall back to applicable fields (#4203)
- Loading branch information
Showing
8 changed files
with
86 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 17 additions & 38 deletions
55
packages/api-headless-cms/src/crud/contentModel/fields/descriptionField.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,29 @@ | ||
import { CmsModelField } from "~/types"; | ||
import { getBaseFieldType } from "~/utils/getBaseFieldType"; | ||
import WebinyError from "@webiny/error"; | ||
import { getApplicableFieldById } from "./getApplicableFieldById"; | ||
|
||
const isFieldApplicable = (field: CmsModelField) => { | ||
return getBaseFieldType(field) === "long-text" && !field.multipleValues; | ||
}; | ||
|
||
/** | ||
* Try finding the requested field, and return its `fieldId`. | ||
* If not defined, or not applicable, fall back to the first applicable field. | ||
*/ | ||
export const getContentModelDescriptionFieldId = ( | ||
fields: CmsModelField[], | ||
descriptionFieldId?: string | null | ||
): string | null | undefined => { | ||
/** | ||
* If there are no fields defined, we will just set as null. | ||
*/ | ||
) => { | ||
if (fields.length === 0) { | ||
return null; | ||
} | ||
/** | ||
* If description field is not defined, let us find possible one. | ||
*/ | ||
if (!descriptionFieldId) { | ||
const descriptionField = fields.find(field => { | ||
return getBaseFieldType(field) === "long-text" && !field.multipleValues; | ||
}); | ||
return descriptionField?.fieldId || null; | ||
} | ||
const target = fields.find( | ||
field => field.fieldId === descriptionFieldId && getBaseFieldType(field) === "long-text" | ||
); | ||
if (!target) { | ||
throw new WebinyError( | ||
`Field selected for the description field does not exist in the model.`, | ||
"VALIDATION_ERROR", | ||
{ | ||
fieldId: descriptionFieldId, | ||
fields | ||
} | ||
); | ||
} | ||
if (target.multipleValues) { | ||
throw new WebinyError( | ||
`Fields that accept multiple values cannot be used as the entry description.`, | ||
"ENTRY_TITLE_FIELD_TYPE", | ||
{ | ||
storageId: target.storageId, | ||
fieldId: target.fieldId, | ||
type: target.type | ||
} | ||
); | ||
|
||
const target = getApplicableFieldById(fields, descriptionFieldId, isFieldApplicable); | ||
|
||
if (target) { | ||
return target.fieldId; | ||
} | ||
|
||
return target.fieldId; | ||
const descriptionField = fields.find(isFieldApplicable); | ||
return descriptionField ? descriptionField.fieldId : null; | ||
}; |
13 changes: 13 additions & 0 deletions
13
packages/api-headless-cms/src/crud/contentModel/fields/getApplicableFieldById.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { CmsModelField } from "~/types"; | ||
|
||
export const getApplicableFieldById = ( | ||
fields: CmsModelField[], | ||
id: string | null | undefined, | ||
isApplicable: (field: CmsModelField) => boolean | ||
) => { | ||
if (!id) { | ||
return undefined; | ||
} | ||
|
||
return fields.find(field => field.fieldId === id && isApplicable(field)); | ||
}; |
60 changes: 15 additions & 45 deletions
60
packages/api-headless-cms/src/crud/contentModel/fields/imageField.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,27 @@ | ||
import { CmsModelField } from "~/types"; | ||
import { getBaseFieldType } from "~/utils/getBaseFieldType"; | ||
import WebinyError from "@webiny/error"; | ||
import { getApplicableFieldById } from "./getApplicableFieldById"; | ||
|
||
const isFieldApplicable = (field: CmsModelField) => { | ||
return Boolean( | ||
getBaseFieldType(field) === "file" && !field.multipleValues && field.settings?.imagesOnly | ||
); | ||
}; | ||
|
||
export const getContentModelImageFieldId = ( | ||
fields: CmsModelField[], | ||
imageFieldId?: string | null | ||
): string | null | undefined => { | ||
/** | ||
* If there are no fields defined, we will just set as null. | ||
*/ | ||
) => { | ||
if (fields.length === 0) { | ||
return null; | ||
} | ||
/** | ||
* If image field is not defined, let us find possible one. | ||
*/ | ||
if (!imageFieldId) { | ||
const imageField = fields.find(field => { | ||
return ( | ||
getBaseFieldType(field) === "file" && | ||
!field.multipleValues && | ||
field.settings?.imagesOnly | ||
); | ||
}); | ||
return imageField?.fieldId || null; | ||
} | ||
const target = fields.find( | ||
field => | ||
field.fieldId === imageFieldId && | ||
getBaseFieldType(field) === "file" && | ||
field.settings?.imagesOnly | ||
); | ||
if (!target) { | ||
throw new WebinyError( | ||
`Field selected for the image field does not exist in the model.`, | ||
"VALIDATION_ERROR", | ||
{ | ||
fieldId: imageFieldId, | ||
fields | ||
} | ||
); | ||
} | ||
if (target.multipleValues) { | ||
throw new WebinyError( | ||
`Fields that accept multiple values cannot be used as the entry image.`, | ||
"ENTRY_TITLE_FIELD_TYPE", | ||
{ | ||
storageId: target.storageId, | ||
fieldId: target.fieldId, | ||
type: target.type | ||
} | ||
); | ||
|
||
const target = getApplicableFieldById(fields, imageFieldId, isFieldApplicable); | ||
|
||
if (target) { | ||
return target.fieldId; | ||
} | ||
|
||
return target.fieldId; | ||
const imageField = fields.find(isFieldApplicable); | ||
return imageField ? imageField.fieldId : null; | ||
}; |
Oops, something went wrong.