mirror of
https://github.com/strapi/strapi.git
synced 2025-12-14 08:44:16 +00:00
Created PluginDispatcher component
This commit is contained in:
parent
51836b1471
commit
0f554234d8
@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* PluginDispatcher
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
import React, { memo } from 'react';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
import { get } from 'lodash';
|
||||||
|
|
||||||
|
import Helmet from 'react-helmet';
|
||||||
|
import BlockerComponent from 'components/BlockerComponent';
|
||||||
|
import ErrorBoundary from 'components/ErrorBoundary';
|
||||||
|
|
||||||
|
export function PluginDispatcher(props) {
|
||||||
|
const {
|
||||||
|
global: { plugins },
|
||||||
|
match: {
|
||||||
|
params: { pluginId },
|
||||||
|
},
|
||||||
|
} = props;
|
||||||
|
|
||||||
|
const pluginToRender = get(plugins, pluginId, null);
|
||||||
|
|
||||||
|
if (!pluginToRender) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { mainComponent, name, preventComponentRendering } = pluginToRender;
|
||||||
|
const blockerComponentProps = pluginToRender.blockerComponentProps;
|
||||||
|
let PluginEntryComponent = preventComponentRendering
|
||||||
|
? BlockerComponent
|
||||||
|
: mainComponent;
|
||||||
|
|
||||||
|
if (preventComponentRendering && pluginToRender.blockerComponent) {
|
||||||
|
PluginEntryComponent = pluginToRender.blockerComponent;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Helmet title={`Stapi - ${name}`} />
|
||||||
|
<ErrorBoundary>
|
||||||
|
<PluginEntryComponent {...props} {...blockerComponentProps} />
|
||||||
|
</ErrorBoundary>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
PluginDispatcher.defaultProps = {};
|
||||||
|
PluginDispatcher.propTypes = {
|
||||||
|
global: PropTypes.object.isRequired,
|
||||||
|
match: PropTypes.object.isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default memo(PluginDispatcher);
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { shallow } from 'enzyme';
|
||||||
|
import BlockerComponent from 'components/BlockerComponent';
|
||||||
|
import { PluginDispatcher } from '../index';
|
||||||
|
|
||||||
|
const BlockerComponent2 = () => <div>BlockerComponent</div>;
|
||||||
|
const Email = () => <div>Email Plugin</div>;
|
||||||
|
|
||||||
|
describe('<PluginDispatcher />', () => {
|
||||||
|
it('Should return null if the params does not match the pluginId', () => {
|
||||||
|
const props = {
|
||||||
|
global: { plugins: {} },
|
||||||
|
match: { params: { pluginId: 'email' } },
|
||||||
|
};
|
||||||
|
|
||||||
|
const rendered = shallow(<PluginDispatcher {...props} />);
|
||||||
|
|
||||||
|
expect(rendered.children()).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should return the BlockerComponent if the plugin preventRendering prop is true', () => {
|
||||||
|
const props = {
|
||||||
|
global: {
|
||||||
|
plugins: {
|
||||||
|
email: {
|
||||||
|
mainComponent: Email,
|
||||||
|
preventComponentRendering: true,
|
||||||
|
blockerComponent: null,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
match: { params: { pluginId: 'email' } },
|
||||||
|
};
|
||||||
|
|
||||||
|
const renderedComponent = shallow(<PluginDispatcher {...props} />);
|
||||||
|
|
||||||
|
expect(renderedComponent.find(BlockerComponent)).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should return a custom BlockerComponent if the plugin preventRendering prop is true and a custom blocker is given', () => {
|
||||||
|
const props = {
|
||||||
|
global: {
|
||||||
|
plugins: {
|
||||||
|
email: {
|
||||||
|
mainComponent: Email,
|
||||||
|
preventComponentRendering: true,
|
||||||
|
blockerComponent: BlockerComponent2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
match: { params: { pluginId: 'email' } },
|
||||||
|
};
|
||||||
|
|
||||||
|
const renderedComponent = shallow(<PluginDispatcher {...props} />);
|
||||||
|
|
||||||
|
expect(renderedComponent.find(BlockerComponent2)).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should return the plugin's mainComponent if all conditions are met", () => {
|
||||||
|
const props = {
|
||||||
|
global: {
|
||||||
|
plugins: {
|
||||||
|
email: {
|
||||||
|
mainComponent: Email,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
match: { params: { pluginId: 'email' } },
|
||||||
|
};
|
||||||
|
|
||||||
|
const renderedComponent = shallow(<PluginDispatcher {...props} />);
|
||||||
|
|
||||||
|
expect(renderedComponent.find(Email)).toHaveLength(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -60,9 +60,7 @@ const store = strapi.store;
|
|||||||
|
|
||||||
// Define the plugin root component
|
// Define the plugin root component
|
||||||
function Comp(props) {
|
function Comp(props) {
|
||||||
return (
|
return <LoadableApp {...props} />;
|
||||||
<LoadableApp {...props} />
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hot reloadable translation json files
|
// Hot reloadable translation json files
|
||||||
@ -73,14 +71,30 @@ if (module.hot) {
|
|||||||
if (strapi) {
|
if (strapi) {
|
||||||
System.import('./i18n').then(result => {
|
System.import('./i18n').then(result => {
|
||||||
const translationMessagesUpdated = result.translationMessages;
|
const translationMessagesUpdated = result.translationMessages;
|
||||||
strapi
|
strapi.refresh(pluginId).translationMessages(translationMessagesUpdated);
|
||||||
.refresh(pluginId)
|
|
||||||
.translationMessages(translationMessagesUpdated);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Require the Initializer component
|
||||||
|
const initializer = (() => {
|
||||||
|
try {
|
||||||
|
return require('../../../../admin/src/initializer.js'); // eslint-disable-line import/no-unresolved
|
||||||
|
} catch (err) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
// Require the plugin's lifecycle
|
||||||
|
const lifecycles = (() => {
|
||||||
|
try {
|
||||||
|
return require('../../../../admin/src/lifecycles.js'); // eslint-disable-line import/no-unresolved
|
||||||
|
} catch (err) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
// Register the plugin.
|
// Register the plugin.
|
||||||
strapi.registerPlugin({
|
strapi.registerPlugin({
|
||||||
blockerComponent: null,
|
blockerComponent: null,
|
||||||
@ -89,8 +103,10 @@ strapi.registerPlugin({
|
|||||||
description: pluginDescription,
|
description: pluginDescription,
|
||||||
icon: pluginPkg.strapi.icon,
|
icon: pluginPkg.strapi.icon,
|
||||||
id: pluginId,
|
id: pluginId,
|
||||||
|
initializer,
|
||||||
injectedComponents,
|
injectedComponents,
|
||||||
layout,
|
layout,
|
||||||
|
lifecycles,
|
||||||
leftMenuLinks: [],
|
leftMenuLinks: [],
|
||||||
mainComponent: Comp,
|
mainComponent: Comp,
|
||||||
name: pluginPkg.strapi.name,
|
name: pluginPkg.strapi.name,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user