diff --git a/packages/core/admin/admin/src/StrapiApp.js b/packages/core/admin/admin/src/StrapiApp.js index a783122841..3ae47008ab 100644 --- a/packages/core/admin/admin/src/StrapiApp.js +++ b/packages/core/admin/admin/src/StrapiApp.js @@ -5,7 +5,7 @@ import { QueryClientProvider, QueryClient } from 'react-query'; import { ThemeProvider } from 'styled-components'; import { LibraryProvider, StrapiProvider } from '@strapi/helper-plugin'; import configureStore from './core/store/configureStore'; -import { Library, Middlewares, Plugin, Reducers } from './core/apis'; +import { Plugin } from './core/apis'; import basename from './utils/basename'; import App from './pages/App'; import LanguageProvider from './components/LanguageProvider'; @@ -16,8 +16,6 @@ import GlobalStyle from './components/GlobalStyle'; import Notifications from './components/Notifications'; import themes from './themes'; -import reducers from './reducers'; - // TODO import translations from './translations'; @@ -36,33 +34,29 @@ const queryClient = new QueryClient({ const appLocales = Object.keys(translations); class StrapiApp { - constructor({ appPlugins }) { + constructor({ appPlugins, library, middlewares, reducers }) { this.appPlugins = appPlugins || {}; - this.library = Library(); - this.middlewares = Middlewares(); + this.library = library; + this.middlewares = middlewares; this.plugins = {}; - this.reducers = Reducers({ appReducers: reducers }); + this.reducers = reducers; this.translations = translations; } - addComponent = component => { - this.library.components.add(component); - }; - addComponents = components => { - components.map(compo => this.library.components.add(compo)); - }; - - addField = field => { - this.library.fields.add(field); + if (Array.isArray(components)) { + components.map(compo => this.library.components.add(compo)); + } else { + this.library.components.add(components); + } }; addFields = fields => { - fields.map(field => this.library.fields.add(field)); - }; - - addMiddleware = middleware => { - this.middlewares.add(middleware); + if (Array.isArray(fields)) { + fields.map(field => this.library.fields.add(field)); + } else { + this.library.fields.add(fields); + } }; addMiddlewares = middlewares => { @@ -71,10 +65,6 @@ class StrapiApp { }); }; - addReducer = reducer => { - this.reducers.add(reducer); - }; - addReducers = reducers => { Object.keys(reducers).forEach(reducerName => { this.reducers.add(reducerName, reducers[reducerName]); @@ -84,13 +74,9 @@ class StrapiApp { async initialize() { Object.keys(this.appPlugins).forEach(plugin => { this.appPlugins[plugin].register({ - addComponent: this.addComponent, addComponents: this.addComponents, - addField: this.addField, addFields: this.addFields, - addMiddleware: this.addMiddleware, addMiddlewares: this.addMiddlewares, - addReducer: this.addReducer, addReducers: this.addReducers, registerPlugin: this.registerPlugin, }); @@ -108,7 +94,7 @@ class StrapiApp { } getPlugin = pluginId => { - return this.plugins[pluginId] || null; + return this.plugins[pluginId]; }; // FIXME @@ -185,4 +171,5 @@ class StrapiApp { } } -export default ({ appPlugins }) => new StrapiApp({ appPlugins }); +export default ({ appPlugins, library, middlewares, reducers }) => + new StrapiApp({ appPlugins, library, middlewares, reducers }); diff --git a/packages/core/admin/admin/src/core/apis/Library.js b/packages/core/admin/admin/src/core/apis/Library.js deleted file mode 100644 index 36ed1cecfa..0000000000 --- a/packages/core/admin/admin/src/core/apis/Library.js +++ /dev/null @@ -1,13 +0,0 @@ -// @HichamELBSI, @mfrachet if you have a better naming for our components and fields -// store I will be happy to change the current name -import Components from './Components'; -import Fields from './Fields'; - -class Library { - constructor() { - this.components = Components(); - this.fields = Fields(); - } -} - -export default () => new Library(); diff --git a/packages/core/admin/admin/src/core/apis/index.js b/packages/core/admin/admin/src/core/apis/index.js index d5c7647948..6a75fb511d 100644 --- a/packages/core/admin/admin/src/core/apis/index.js +++ b/packages/core/admin/admin/src/core/apis/index.js @@ -1,4 +1,5 @@ -export { default as Library } from './Library'; +export { default as Fields } from './Fields'; +export { default as Components } from './Components'; export { default as Middlewares } from './Middlewares'; export { default as Plugin } from './Plugin'; export { default as Reducers } from './Reducers'; diff --git a/packages/core/admin/admin/src/index.js b/packages/core/admin/admin/src/index.js index e5eb0ddf93..1bd858f339 100644 --- a/packages/core/admin/admin/src/index.js +++ b/packages/core/admin/admin/src/index.js @@ -1,8 +1,16 @@ import ReactDOM from 'react-dom'; import StrapiApp from './StrapiApp'; +import { Components, Fields, Middlewares, Reducers } from './core/apis'; import plugins from './plugins'; +import appReducers from './reducers'; -const app = StrapiApp({ appPlugins: plugins }); +const library = { + components: Components(), + fields: Fields(), +}; +const middlewares = Middlewares(); +const reducers = Reducers({ appReducers }); +const app = StrapiApp({ appPlugins: plugins, library, middlewares, reducers }); const MOUNT_NODE = document.getElementById('app'); diff --git a/packages/core/admin/admin/src/tests/StrapiApp.test.js b/packages/core/admin/admin/src/tests/StrapiApp.test.js index 4fbd691722..a0d280f7c1 100644 --- a/packages/core/admin/admin/src/tests/StrapiApp.test.js +++ b/packages/core/admin/admin/src/tests/StrapiApp.test.js @@ -1,9 +1,13 @@ import { render } from '@testing-library/react'; import StrapiApp from '../StrapiApp'; +import appReducers from '../reducers'; describe('ADMIN | StrapiApp', () => { it('should render the app without plugins', () => { - const app = StrapiApp({}); + const library = { fields: {}, components: {} }; + const middlewares = { middlewares: [] }; + const reducers = { reducers: appReducers }; + const app = StrapiApp({ middlewares, reducers, library }); expect(render(app.render())).toMatchInlineSnapshot(` Object { diff --git a/packages/core/upload/admin/src/index.js b/packages/core/upload/admin/src/index.js index 2ef8cd3b4c..298ee31fd4 100644 --- a/packages/core/upload/admin/src/index.js +++ b/packages/core/upload/admin/src/index.js @@ -26,9 +26,9 @@ const name = pluginPkg.strapi.name; export default { register(app) { // TODO update doc and guides - app.addComponent({ name: 'media-library', Component: InputModalStepper }); + app.addComponents({ name: 'media-library', Component: InputModalStepper }); // TODO update guide - app.addField({ type: 'media', Component: InputMedia }); + app.addFields({ type: 'media', Component: InputMedia }); app.addReducers(reducers);