Skip to content

Commit

Permalink
fix: remove schema if content type is not the same as for the schema (#…
Browse files Browse the repository at this point in the history
…184)

* fix: remove schema if content type is not the same as for the schema

* clean up

* fix tests

* Automated release 1.25.1

* bump version

* Automated release 1.26.0

---------

Co-authored-by: EEA Jenkins <@users.noreply.github.com>
  • Loading branch information
razvanMiu authored Dec 13, 2023
1 parent 6656833 commit f3a6a0d
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 4 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ All notable changes to this project will be documented in this file. Dates are d

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

### [1.25.0](https://github.com/eea/volto-eea-website-theme/compare/1.24.3...1.25.0) - 12 December 2023
### [1.26.0](https://github.com/eea/volto-eea-website-theme/compare/1.25.0...1.26.0) - 13 December 2023

#### :bug: Bug Fixes

- fix: customize SidebarPopup volto component [Miu Razvan - [`0bdf736`](https://github.com/eea/volto-eea-website-theme/commit/0bdf736045509938ad562b26c8c87f040c7994e4)]
- fix: remove schema if content type is not the same as for the schema [Miu Razvan - [`0056fa9`](https://github.com/eea/volto-eea-website-theme/commit/0056fa9d68efb7d2f118c9767d756efbfb100f21)]

#### :hammer_and_wrench: Others

- bump version [Miu Razvan - [`69ee513`](https://github.com/eea/volto-eea-website-theme/commit/69ee51378cf6f5dddab0d5c54c3f5e4942756562)]
- fix tests [Miu Razvan - [`aa3d5c9`](https://github.com/eea/volto-eea-website-theme/commit/aa3d5c9aee1a49f7cdeb760c074f66029504c4de)]
- clean up [Miu Razvan - [`2a3f3dc`](https://github.com/eea/volto-eea-website-theme/commit/2a3f3dc14203b5641ee325256503527d550e9c5b)]
### [1.25.0](https://github.com/eea/volto-eea-website-theme/compare/1.24.3...1.25.0) - 13 December 2023

#### :house: Documentation changes

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eeacms/volto-eea-website-theme",
"version": "1.25.0",
"version": "1.26.0",
"description": "@eeacms/volto-eea-website-theme: Volto add-on",
"main": "src/index.js",
"author": "European Environment Agency: IDM2 A-Team",
Expand Down
1 change: 1 addition & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './schema';
5 changes: 5 additions & 0 deletions src/actions/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function removeSchema() {
return {
type: 'REMOVE_SCHEMA',
};
}
24 changes: 24 additions & 0 deletions src/components/theme/AppExtras/RemoveSchema.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useEffect } from 'react';
import { connect } from 'react-redux';
import { removeSchema } from '@eeacms/volto-eea-website-theme/actions';

function RemoveSchema({ removeSchema, content, schema }) {
useEffect(() => {
if (
schema.schema &&
(!content || (content && schema.contentType !== content['@type']))
) {
removeSchema();
}
}, [removeSchema, content, schema]);

return null;
}

export default connect(
(state) => ({
content: state.content.data,
schema: state.schema,
}),
{ removeSchema },
)(RemoveSchema);
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import contentBoxSVG from './icons/content-box.svg';
import okMiddleware from './middleware/ok';
import voltoCustomMiddleware from './middleware/voltoCustom';
import installSlate from './slate';
import installReducers from './reducers';
import installMiddlewares from './middleware';

import * as eea from './config';
import React from 'react';
Expand Down Expand Up @@ -493,7 +495,10 @@ const applyConfig = (config) => {
});

// Custom blocks: Title
return [installCustomTitle].reduce((acc, apply) => apply(acc), config);
return [installCustomTitle, installMiddlewares, installReducers].reduce(
(acc, apply) => apply(acc),
config,
);
};

export default applyConfig;
8 changes: 8 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ jest.mock(
jest.mock('@plone/volto/components', () => ({
Icon: 'MockedIcon',
}));
jest.mock(
'@eeacms/volto-eea-website-theme/components/theme/AppExtras/RemoveSchema',
() => 'MockedRemoveSchema',
);

global.__SERVER__ = true;

describe('applyConfig', () => {
it('should update the config settings with EEA specific settings', () => {
const originalConfig = {
addonReducers: {},
blocks: {
blocksConfig: {
title: {
Expand Down Expand Up @@ -139,6 +144,7 @@ describe('applyConfig', () => {
{ match: '', component: 'MockedDraftBackground' },
{ match: '', component: 'MockedSubsiteClass' },
{ match: '', component: BaseTag },
{ match: '*', component: 'MockedRemoveSchema' },
]);
expect(config.settings.available_colors).toEqual(eea.colors);
expect(config.settings.hasLanguageDropdown).toBe(false);
Expand Down Expand Up @@ -183,6 +189,7 @@ describe('applyConfig', () => {

it('should update the config settings with EEA specific settings', () => {
const originalConfig = {
addonReducers: {},
blocks: {
blocksConfig: {
title: {
Expand Down Expand Up @@ -296,6 +303,7 @@ describe('applyConfig', () => {
{ match: '', component: 'MockedDraftBackground' },
{ match: '', component: 'MockedSubsiteClass' },
{ match: '', component: BaseTag },
{ match: '*', component: 'MockedRemoveSchema' },
]);
expect(config.settings.available_colors).toEqual(eea.colors);
expect(config.settings.hasLanguageDropdown).toBe(false);
Expand Down
11 changes: 11 additions & 0 deletions src/middleware/apiEnhancer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function apiEnhencar(middlewares) {
return [
() => (next) => (action) => {
if (action.request) {
return next({ ...action, _request: action.request });
}
return next(action);
},
...middlewares,
];
}
10 changes: 10 additions & 0 deletions src/middleware/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import apiEnhancer from './apiEnhancer';

export default function applyConfig(config) {
config.settings.storeExtenders = [
...(config.settings.storeExtenders || []),
apiEnhancer,
];

return config;
}
17 changes: 17 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import RemoveSchema from '@eeacms/volto-eea-website-theme/components/theme/AppExtras/RemoveSchema';

import schema from './schema';

export default function applyConfig(config) {
config.addonReducers.schema = schema;

config.settings.appExtras = [
...(config.settings.appExtras || []),
{
match: '*',
component: RemoveSchema,
},
];

return config;
}
48 changes: 48 additions & 0 deletions src/reducers/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import voltoSchema from '@plone/volto/reducers/schema/schema';

const initialState = {
contentUrl: null,
contentType: null,
error: null,
loaded: false,
loading: false,
schema: null,
post: {
loaded: false,
loading: false,
error: null,
},
update: {
loaded: false,
loading: false,
error: null,
},
put: {
loaded: false,
loading: false,
error: null,
},
};

export default function schema(state = initialState, action = {}) {
if (action.type === 'REMOVE_SCHEMA') {
return {
...state,
error: null,
loading: false,
loaded: true,
schema: null,
};
}

if (action.type === 'GET_SCHEMA_SUCCESS') {
const [contentUrl, contentType] = action._request.path.split('/@types/');
return {
...voltoSchema(state, action),
contentUrl,
contentType,
};
}

return voltoSchema(state, action);
}

0 comments on commit f3a6a0d

Please sign in to comment.