From bc3cc0647af0dc8050058b83a55131739d848031 Mon Sep 17 00:00:00 2001 From: Tiberiu Ichim Date: Tue, 1 Nov 2022 13:53:26 +0200 Subject: [PATCH] Fix schema loading --- src/blocks/Listing/index.js | 12 +- .../Listing/item-templates/ItemTemplates.jsx | 2 - .../item-templates/SearchItemTemplate.jsx | 3 - src/blocks/Listing/schema.js | 217 +++++++++--------- src/components/UniversalCard/schema.js | 14 +- src/schema-utils.js | 7 +- 6 files changed, 120 insertions(+), 135 deletions(-) diff --git a/src/blocks/Listing/index.js b/src/blocks/Listing/index.js index a5633827..8621164f 100644 --- a/src/blocks/Listing/index.js +++ b/src/blocks/Listing/index.js @@ -1,4 +1,3 @@ -import { compose } from 'redux'; import { addStyling } from '@plone/volto/helpers'; import UniversalCard from '@eeacms/volto-listing-block/components/UniversalCard/UniversalCard'; import { composeSchema } from '@eeacms/volto-listing-block/schema-utils'; @@ -118,21 +117,14 @@ const applyConfig = (config) => { isDefault: true, title: 'Listing Item', template: DefaultItemLayout, - schemaEnhancer: composeSchema( - setItemModelSchema, - setCardStylingSchema, - DefaultItemLayout.schemaEnhancer, - ), + schemaEnhancer: composeSchema(setItemModelSchema, setCardStylingSchema), }, { id: 'searchItem', isDefault: false, title: 'Search Item', template: SearchItemLayout, - schemaEnhancer: composeSchema( - setCardStylingSchema, - SearchItemLayout.schemaEnhancer, - ), + schemaEnhancer: composeSchema(setItemModelSchema, setCardStylingSchema), }, ], }; diff --git a/src/blocks/Listing/item-templates/ItemTemplates.jsx b/src/blocks/Listing/item-templates/ItemTemplates.jsx index 8caad376..ad00e704 100644 --- a/src/blocks/Listing/item-templates/ItemTemplates.jsx +++ b/src/blocks/Listing/item-templates/ItemTemplates.jsx @@ -78,5 +78,3 @@ const BasicItem = (props) => { export const DefaultItemLayout = (props) => { return ; }; - -// DefaultItemLayout.schemaEnhancer = universalItemSchemaEnhancer; diff --git a/src/blocks/Listing/item-templates/SearchItemTemplate.jsx b/src/blocks/Listing/item-templates/SearchItemTemplate.jsx index 2a9cff19..c3a603fd 100644 --- a/src/blocks/Listing/item-templates/SearchItemTemplate.jsx +++ b/src/blocks/Listing/item-templates/SearchItemTemplate.jsx @@ -1,6 +1,5 @@ import cx from 'classnames'; import PreviewImage from '@eeacms/volto-listing-block/PreviewImage'; -// import { universalItemSchemaEnhancer } from '@eeacms/volto-listing-block/components/UniversalItem/schema'; const BodyText = ({ item, hasDescription }) => { return ( @@ -60,5 +59,3 @@ const BasicItem = (props) => { export const SearchItemLayout = (props) => { return ; }; - -// SearchItemLayout.schemaEnhancer = universalItemSchemaEnhancer; diff --git a/src/blocks/Listing/schema.js b/src/blocks/Listing/schema.js index 978e7111..ba8405d5 100644 --- a/src/blocks/Listing/schema.js +++ b/src/blocks/Listing/schema.js @@ -10,7 +10,6 @@ const ALIGN_INFO_MAP = { export const setBasicStylingSchema = (args) => { const { schema, intl } = args; - console.log('setBasicStylingSchema', args); schema.properties.styles.schema = { fieldsets: [ { @@ -93,130 +92,120 @@ const CallToActionSchema = ({ formData }) => { export const setCardModelSchema = (args) => { const { formData, schema } = args; + console.log('setCardModelSchema', args); - const CardSchema = { - fieldsets: [ - { - id: 'cardDesigner', - title: 'Card', - fields: [ - 'hasDate', - 'hasDescription', - ...(formData?.itemModel?.hasDescription ? ['maxDescription'] : []), - 'hasMetaType', - 'hasLabel', - 'hasTags', - 'callToAction', - ], - }, - ], - properties: { - hasDate: { - title: 'Publication date', - type: 'boolean', - }, - hasDescription: { - title: 'Description', - type: 'boolean', - }, - maxDescription: { - title: 'Description max lines', - description: - "Limit description to a maximum number of lines by adding trailing '...'", - type: 'number', - default: 2, - minimum: 0, - maximum: 5, - }, - hasMetaType: { - title: 'Show portal type', - type: 'boolean', - }, - hasLabel: { - title: 'Show new/archived label', - type: 'boolean', - }, - hasTags: { - title: 'Show tags', - type: 'boolean', - }, - callToAction: { - widget: 'object', - schema: CallToActionSchema({ formData }), - }, + + const itemModelSchema = schema.properties.itemModel.schema; + itemModelSchema.fieldsets[0].fields = [ + ...itemModelSchema.fieldsets[0].fields, + 'hasDate', + 'hasDescription', + ...(formData?.itemModel?.hasDescription ? ['maxDescription'] : []), + 'hasMetaType', + 'hasLabel', + 'hasTags', + 'callToAction', + ]; + itemModelSchema.properties = { + ...itemModelSchema.properties, + hasDate: { + title: 'Publication date', + type: 'boolean', + }, + hasDescription: { + title: 'Description', + type: 'boolean', + }, + maxDescription: { + title: 'Description max lines', + description: + "Limit description to a maximum number of lines by adding trailing '...'", + type: 'number', + default: 2, + minimum: 0, + maximum: 5, + }, + hasMetaType: { + title: 'Show portal type', + type: 'boolean', + }, + hasLabel: { + title: 'Show new/archived label', + type: 'boolean', + }, + hasTags: { + title: 'Show tags', + type: 'boolean', + }, + callToAction: { + widget: 'object', + schema: CallToActionSchema({ formData }), }, - required: [], }; - - schema.properties.itemModel.schema = CardSchema; return schema; }; export const setItemModelSchema = (args) => { const { formData, schema } = args; + const itemModelSchema = schema.properties.itemModel.schema; + console.log('setItemModelSchema', args); - const ItemSchema = { - fieldsets: [ - { - id: 'itemDesigner', - title: 'Item', - fields: [ - 'hasDate', - 'hasDescription', - 'maxDescription', - 'hasImage', - ...(formData.itemModel?.hasImage ? ['imageOnRightSide'] : []), - // 'hasMetaType', - // 'hasLabel', - // 'hasTags', - // 'callToAction', - ], - }, - ], - properties: { - hasDate: { - title: 'Publication date', - type: 'boolean', - }, - hasDescription: { - title: 'Description', - type: 'boolean', - default: true, - }, - maxDescription: { - title: 'Description max lines', - description: - "Limit description to a maximum number of lines by adding trailing '...'", - type: 'number', - default: 2, - minimum: 0, - maximum: 5, - }, - hasImage: { - title: 'Image', - type: 'boolean', - default: true, - }, - imageOnRightSide: { - title: 'Image on Right (Default is Left)', - type: 'boolean', - }, - // hasMetaType: { - // title: 'Show portal type', - // type: 'boolean', - // }, - // hasLabel: { - // title: 'Show new/archived label', - // type: 'boolean', - // }, - // hasTags: { - // title: 'Show tags', - // type: 'boolean', - // }, + + itemModelSchema.fieldsets[0].fields = [ + ...itemModelSchema.fieldsets[0].fields, + 'hasDate', + 'hasDescription', + 'maxDescription', + 'hasImage', + ...(formData.itemModel?.hasImage ? ['imageOnRightSide'] : []), + // 'hasMetaType', + // 'hasLabel', + // 'hasTags', + // 'callToAction', + ]; + itemModelSchema.properties = { + ...itemModelSchema.properties, + + hasDate: { + title: 'Publication date', + type: 'boolean', }, - required: [], + hasDescription: { + title: 'Description', + type: 'boolean', + default: true, + }, + maxDescription: { + title: 'Description max lines', + description: + "Limit description to a maximum number of lines by adding trailing '...'", + type: 'number', + default: 2, + minimum: 0, + maximum: 5, + }, + hasImage: { + title: 'Image', + type: 'boolean', + default: true, + }, + imageOnRightSide: { + title: 'Image on Right (Default is Left)', + type: 'boolean', + }, + // hasMetaType: { + // title: 'Show portal type', + // type: 'boolean', + // }, + // hasLabel: { + // title: 'Show new/archived label', + // type: 'boolean', + // }, + // hasTags: { + // title: 'Show tags', + // type: 'boolean', + // }, }; - schema.properties.itemModel.schema = ItemSchema; return schema; }; diff --git a/src/components/UniversalCard/schema.js b/src/components/UniversalCard/schema.js index bfd4838d..bae3fcb6 100644 --- a/src/components/UniversalCard/schema.js +++ b/src/components/UniversalCard/schema.js @@ -2,6 +2,7 @@ import { defineMessages } from 'react-intl'; import { DefaultCardModelSchema, schemaEnhancerFactory, + addTypeSelect, } from '@eeacms/volto-listing-block/schema-utils'; const messages = defineMessages({ @@ -12,13 +13,13 @@ const messages = defineMessages({ }); export default function universalCardSchemaEnhancer(args) { - console.log('universalCardSchemaEnhancer', args); const props = { ...args }; const { schema } = props; props.formData = props.formData || props.data; + const extensionName = 'cardTemplates'; const enhancer = schemaEnhancerFactory({ - extensionName: 'cardTemplates', + extensionName, messages, blockType: 'listing', extensionField: '@type', @@ -30,6 +31,13 @@ export default function universalCardSchemaEnhancer(args) { fields: ['itemModel'], }); + const itemModelSchema = addTypeSelect({ + ...args, + schema: DefaultCardModelSchema, + extensionName, + messages, + }); + const baseSchema = { ...schema, fieldsets: [...schema.fieldsets], @@ -38,7 +46,7 @@ export default function universalCardSchemaEnhancer(args) { itemModel: { title: 'Card model', widget: 'object', - schema: DefaultCardModelSchema, + schema: itemModelSchema, }, }, }; diff --git a/src/schema-utils.js b/src/schema-utils.js index 690c54c5..8511bbdf 100644 --- a/src/schema-utils.js +++ b/src/schema-utils.js @@ -1,7 +1,8 @@ import { cloneDeep } from 'lodash'; import config from '@plone/volto/registry'; -const addTypeSelect = ({ intl, schema, extensionName, messages }) => { +export const addTypeSelect = ({ intl, schema, extensionName, messages }) => { + schema = cloneDeep(schema); const field = '@type'; const extensions = config.blocks.blocksConfig.listing.extensions; const variations = extensions[extensionName]; @@ -31,7 +32,7 @@ export const schemaEnhancerFactory = ({ const extensions = blockConfig.extensions; const templates = extensions[extensionName]; - const activeItemName = formData?.[extensionField]; + const activeItemName = formData?.itemModel?.[extensionField]; // TODO: don't hardcode itemModel let activeItem = templates?.find((item) => item.id === activeItemName); if (!activeItem) activeItem = templates?.find((item) => item.isDefault); @@ -41,7 +42,7 @@ export const schemaEnhancerFactory = ({ ? schemaEnhancer({ schema: cloneDeep(originalSchema), formData, intl }) : cloneDeep(originalSchema); - return addTypeSelect({ schema, intl, extensionName, messages }); + return schema; }; export const DefaultCardModelSchema = {