Skip to content

Commit

Permalink
Fix schema loading
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Nov 1, 2022
1 parent 2b7b3c7 commit bc3cc06
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 135 deletions.
12 changes: 2 additions & 10 deletions src/blocks/Listing/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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),
},
],
};
Expand Down
2 changes: 0 additions & 2 deletions src/blocks/Listing/item-templates/ItemTemplates.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,3 @@ const BasicItem = (props) => {
export const DefaultItemLayout = (props) => {
return <BasicItem {...props} />;
};

// DefaultItemLayout.schemaEnhancer = universalItemSchemaEnhancer;
3 changes: 0 additions & 3 deletions src/blocks/Listing/item-templates/SearchItemTemplate.jsx
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -60,5 +59,3 @@ const BasicItem = (props) => {
export const SearchItemLayout = (props) => {
return <BasicItem {...props} />;
};

// SearchItemLayout.schemaEnhancer = universalItemSchemaEnhancer;
217 changes: 103 additions & 114 deletions src/blocks/Listing/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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;
};

Expand Down
14 changes: 11 additions & 3 deletions src/components/UniversalCard/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineMessages } from 'react-intl';
import {
DefaultCardModelSchema,
schemaEnhancerFactory,
addTypeSelect,
} from '@eeacms/volto-listing-block/schema-utils';

const messages = defineMessages({
Expand All @@ -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',
Expand All @@ -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],
Expand All @@ -38,7 +46,7 @@ export default function universalCardSchemaEnhancer(args) {
itemModel: {
title: 'Card model',
widget: 'object',
schema: DefaultCardModelSchema,
schema: itemModelSchema,
},
},
};
Expand Down
7 changes: 4 additions & 3 deletions src/schema-utils.js
Original file line number Diff line number Diff line change
@@ -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];
Expand Down Expand Up @@ -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);

Expand All @@ -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 = {
Expand Down

0 comments on commit bc3cc06

Please sign in to comment.