From ddc7a3d60b589e3848b383e58ea00436f25c52cc Mon Sep 17 00:00:00 2001 From: soupette Date: Wed, 5 May 2021 16:09:56 +0200 Subject: [PATCH] Clean files Signed-off-by: soupette --- packages/core/admin/admin/src/StrapiApp.js | 2 +- packages/core/admin/admin/src/app.js | 228 ------------------ .../core/admin/admin/src/configureStore.js | 60 ----- .../admin/admin/src/containers/App/index.js | 3 + .../admin/src/containers/App/selectors.js | 15 +- .../containers/App/tests/selectors.test.js | 44 +--- .../src/containers/LocaleToggle/index.js | 4 +- .../admin/src/containers/ProfilePage/index.js | 4 +- packages/core/admin/admin/src/i18n.js | 17 -- packages/core/admin/admin/src/plugins.js | 34 --- .../core/admin/admin/src/utils/checkStore.js | 21 -- .../core/admin/admin/src/utils/constants.js | 3 - .../core/admin/admin/src/utils/history.js | 6 - .../admin/admin/src/utils/injectReducer.js | 56 ----- .../core/admin/admin/src/utils/injectSaga.js | 76 ------ .../admin/admin/src/utils/reducerInjectors.js | 34 --- .../admin/admin/src/utils/sagaInjectors.js | 91 ------- 17 files changed, 12 insertions(+), 686 deletions(-) delete mode 100644 packages/core/admin/admin/src/app.js delete mode 100644 packages/core/admin/admin/src/configureStore.js delete mode 100644 packages/core/admin/admin/src/i18n.js delete mode 100644 packages/core/admin/admin/src/plugins.js delete mode 100644 packages/core/admin/admin/src/utils/checkStore.js delete mode 100644 packages/core/admin/admin/src/utils/constants.js delete mode 100644 packages/core/admin/admin/src/utils/history.js delete mode 100644 packages/core/admin/admin/src/utils/injectReducer.js delete mode 100644 packages/core/admin/admin/src/utils/injectSaga.js delete mode 100644 packages/core/admin/admin/src/utils/reducerInjectors.js delete mode 100644 packages/core/admin/admin/src/utils/sagaInjectors.js diff --git a/packages/core/admin/admin/src/StrapiApp.js b/packages/core/admin/admin/src/StrapiApp.js index f251b83476..c96aa241e5 100644 --- a/packages/core/admin/admin/src/StrapiApp.js +++ b/packages/core/admin/admin/src/StrapiApp.js @@ -10,7 +10,7 @@ import App from './containers/App'; import Fonts from './components/Fonts'; // TODO -import { translationMessages } from './i18n'; +import translationMessages from './translations'; // const App = () => 'todo'; diff --git a/packages/core/admin/admin/src/app.js b/packages/core/admin/admin/src/app.js deleted file mode 100644 index 6a2ee688eb..0000000000 --- a/packages/core/admin/admin/src/app.js +++ /dev/null @@ -1,228 +0,0 @@ -/** - * - * app.js - * - * Entry point of the application - */ - -// NOTE TO PLUGINS DEVELOPERS: -// If you modify this file by adding new options to a plugin entry point -// Here's the file: strapi/docs/3.0.0-beta.x/plugin-development/frontend-field-api.md -// Here's the file: strapi/docs/3.0.0-beta.x/guides/registering-a-field-in-admin.md -// Also the strapi-generate-plugins/files/admin/src/index.js needs to be updated -// IF THE DOC IS NOT UPDATED THE PULL REQUEST WILL NOT BE MERGED - -/* eslint-disable */ - -// import '@babel/polyfill'; -import 'sanitize.css/sanitize.css'; - -// Third party css library needed -import 'bootstrap/dist/css/bootstrap.css'; -import 'font-awesome/css/font-awesome.min.css'; -import '@fortawesome/fontawesome-free/css/all.css'; -import '@fortawesome/fontawesome-free/js/all.min.js'; - -import React from 'react'; -import ReactDOM from 'react-dom'; -import { Provider } from 'react-redux'; -import { BrowserRouter } from 'react-router-dom'; -// Strapi provider with the internal APIs -import { StrapiProvider } from '@strapi/helper-plugin'; -import { merge } from 'lodash'; -import Fonts from './components/Fonts'; -import { freezeApp, pluginLoaded, unfreezeApp, updatePlugin } from './containers/App/actions'; -import { showNotification } from './containers/NotificationProvider/actions'; -import { showNotification as showNewNotification } from './containers/NewNotification/actions'; - -import basename from './utils/basename'; -import injectReducer from './utils/injectReducer'; -import injectSaga from './utils/injectSaga'; -import Strapi from './utils/Strapi'; - -// Import root component -import App from './containers/App'; -// Import Language provider -import LanguageProvider from './containers/LanguageProvider'; - -import configureStore from './configureStore'; - -// Import i18n messages -import { translationMessages, languages } from './i18n'; - -import history from './utils/history'; - -import plugins from './plugins'; - -const strapi = Strapi(); - -const pluginsReducers = {}; -const pluginsToLoad = []; - -Object.keys(plugins).forEach(current => { - const registerPlugin = plugin => { - strapi.registerPlugin(plugin); - - return plugin; - }; - const currentPluginFn = plugins[current]; - - // By updating this by adding required methods - // to load a plugin you need to update this file - // strapi-generate-plugins/files/admin/src/index.js needs to be updated - const plugin = currentPluginFn({ - registerComponent: strapi.componentApi.registerComponent, - registerField: strapi.fieldApi.registerField, - registerPlugin, - middlewares: strapi.middlewares, - }); - - const pluginTradsPrefixed = languages.reduce((acc, lang) => { - const currentLocale = plugin.trads[lang]; - - if (currentLocale) { - const localeprefixedWithPluginId = Object.keys(currentLocale).reduce((acc2, current) => { - acc2[`${plugin.id}.${current}`] = currentLocale[current]; - - return acc2; - }, {}); - - acc[lang] = localeprefixedWithPluginId; - } - - return acc; - }, {}); - - // Retrieve all reducers - const pluginReducers = plugin.reducers || {}; - - Object.keys(pluginReducers).forEach(reducerName => { - pluginsReducers[reducerName] = pluginReducers[reducerName]; - }); - - try { - merge(translationMessages, pluginTradsPrefixed); - pluginsToLoad.push(plugin); - } catch (err) { - console.log({ err }); - } -}); - -const initialState = {}; -const store = configureStore(initialState, pluginsReducers, strapi); -const { dispatch } = store; - -// Load plugins, this will be removed in the v4, temporary fix until the plugin API -// https://plugin-api-rfc.vercel.app/plugin-api/admin.html -pluginsToLoad.forEach(plugin => { - const bootPlugin = plugin.boot; - - if (bootPlugin) { - bootPlugin(strapi); - } - - dispatch(pluginLoaded(plugin)); -}); - -const displayNotification = (message, status) => { - console.warn( - // Validate the text - 'Deprecated: Will be deleted.\nPlease use strapi.notification.toggle(config).\nDocs : https://strapi.io/documentation/developer-docs/latest/development/local-plugins-customization.html#strapi-notification' - ); - dispatch(showNotification(message, status)); -}; -const displayNewNotification = config => { - dispatch(showNewNotification(config)); -}; -const lockApp = data => { - dispatch(freezeApp(data)); -}; -const unlockApp = () => { - dispatch(unfreezeApp()); -}; - -const lockAppWithOverlay = () => { - const overlayblockerParams = { - children:
, - noGradient: true, - }; - - lockApp(overlayblockerParams); -}; - -window.strapi = Object.assign(window.strapi || {}, { - backendURL: BACKEND_URL === '/' ? window.location.origin : BACKEND_URL, - notification: { - // New notification api - toggle: config => { - displayNewNotification(config); - }, - success: message => { - displayNotification(message, 'success'); - }, - warning: message => { - displayNotification(message, 'warning'); - }, - error: message => { - displayNotification(message, 'error'); - }, - info: message => { - displayNotification(message, 'info'); - }, - }, - refresh: pluginId => ({ - translationMessages: translationMessagesUpdated => { - render(merge({}, translationMessages, translationMessagesUpdated)); - }, - leftMenuSections: leftMenuSectionsUpdated => { - store.dispatch(updatePlugin(pluginId, 'leftMenuSections', leftMenuSectionsUpdated)); - }, - }), - router: history, - languages, - currentLanguage: - window.localStorage.getItem('strapi-admin-language') || - window.navigator.language || - window.navigator.userLanguage || - 'en', - lockApp, - lockAppWithOverlay, - unlockApp, - injectReducer, - injectSaga, - store, -}); - -const MOUNT_NODE = document.getElementById('app') || document.createElement('div'); - -const render = messages => { - ReactDOM.render( - - - - - - - - - - , - MOUNT_NODE - ); -}; - -// Not needed we will use a plugin for this -// if (module.hot) { -// module.hot.accept(['./i18n', './containers/App'], () => { -// ReactDOM.unmountComponentAtNode(MOUNT_NODE); - -// render(translationMessages); -// }); -// } - -if (NODE_ENV !== 'test') { - render(translationMessages); -} - -// @Pierre Burgy exporting dispatch for the notifications... -export { dispatch }; diff --git a/packages/core/admin/admin/src/configureStore.js b/packages/core/admin/admin/src/configureStore.js deleted file mode 100644 index a0393e0d52..0000000000 --- a/packages/core/admin/admin/src/configureStore.js +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Create the store with dynamic reducers - */ - -import { createStore, applyMiddleware, compose } from 'redux'; -import { fromJS } from 'immutable'; -// import { routerMiddleware } from 'react-router-redux'; -import createSagaMiddleware from 'redux-saga'; -import createReducer from './reducers'; - -const sagaMiddleware = createSagaMiddleware(); - -export default function configureStore(initialState = {}, reducers, strapi) { - // Create the store with two middlewares - // 1. sagaMiddleware: Makes redux-sagas work - // 2. routerMiddleware: Syncs the location/URL path to the state - const middlewares = [sagaMiddleware]; - - strapi.middlewares.middlewares.forEach(middleware => { - middlewares.push(middleware()); - }); - - const enhancers = [applyMiddleware(...middlewares)]; - - // If Redux DevTools Extension is installed use it, otherwise use Redux compose - /* eslint-disable no-underscore-dangle */ - const composeEnhancers = - process.env.NODE_ENV !== 'production' && - typeof window === 'object' && - window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ - ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ - // TODO Try to remove when `react-router-redux` is out of beta, LOCATION_CHANGE should not be fired more than once after hot reloading - // Prevent recomputing reducers for `replaceReducer` - shouldHotReload: false, - name: 'Strapi - Dashboard', - }) - : compose; - /* eslint-enable */ - - const store = createStore( - createReducer(reducers), - fromJS(initialState), - composeEnhancers(...enhancers) - ); - - // Extensions - store.runSaga = sagaMiddleware.run; - store.injectedReducers = {}; // Reducer registry - store.injectedSagas = {}; // Saga registry - - // Make reducers hot reloadable, see http://mxs.is/googmo - /* istanbul ignore next */ - if (module.hot) { - module.hot.accept('./reducers', () => { - store.replaceReducer(createReducer(store.injectedReducers)); - }); - } - - return store; -} diff --git a/packages/core/admin/admin/src/containers/App/index.js b/packages/core/admin/admin/src/containers/App/index.js index 8fe5ee9932..207e6336c6 100644 --- a/packages/core/admin/admin/src/containers/App/index.js +++ b/packages/core/admin/admin/src/containers/App/index.js @@ -48,6 +48,9 @@ window.strapi = Object.assign(window.strapi || {}, { toggle: () => {}, }, backendURL: BACKEND_URL, + lockApp: () => console.log('todo lockApp'), + unlockApp: () => console.log('todo unlockApp'), + lockAppWithOverlay: () => console.log('todo unlockAppWithOverlay'), }); const queryClient = new QueryClient({ diff --git a/packages/core/admin/admin/src/containers/App/selectors.js b/packages/core/admin/admin/src/containers/App/selectors.js index 7cc1eecccc..184c6721c2 100644 --- a/packages/core/admin/admin/src/containers/App/selectors.js +++ b/packages/core/admin/admin/src/containers/App/selectors.js @@ -11,18 +11,5 @@ const selectApp = () => state => state.app; const makeSelectApp = () => createSelector(selectApp(), appState => appState.toJS()); -const makeSelectShowGlobalAppBlocker = () => - createSelector(selectApp(), appState => appState.get('showGlobalAppBlocker')); - -const makeSelectBlockApp = () => createSelector(selectApp(), appState => appState.get('blockApp')); - -const makeSelectOverlayBlockerProps = () => - createSelector(selectApp(), appState => appState.get('overlayBlockerData')); - export default makeSelectApp; -export { - selectApp, - makeSelectBlockApp, - makeSelectOverlayBlockerProps, - makeSelectShowGlobalAppBlocker, -}; +export { selectApp }; diff --git a/packages/core/admin/admin/src/containers/App/tests/selectors.test.js b/packages/core/admin/admin/src/containers/App/tests/selectors.test.js index 2c7ce43cfe..aa90a0bf26 100644 --- a/packages/core/admin/admin/src/containers/App/tests/selectors.test.js +++ b/packages/core/admin/admin/src/containers/App/tests/selectors.test.js @@ -1,11 +1,6 @@ import { fromJS } from 'immutable'; -import makeSelectApp, { - selectApp, - makeSelectBlockApp, - makeSelectOverlayBlockerProps, - makeSelectShowGlobalAppBlocker, -} from '../selectors'; +import makeSelectApp, { selectApp } from '../selectors'; describe(' selectors', () => { describe('selectApp', () => { @@ -29,41 +24,4 @@ describe(' selectors', () => { expect(makeSelectApp()(mockedState)).toEqual(appState.toJS()); }); }); - - describe('makeSelectBlockApp', () => { - it('should select the blockApp', () => { - const mockedState = fromJS({ - app: { - blockApp: true, - }, - }); - - expect(makeSelectBlockApp()(mockedState)).toEqual(true); - }); - }); - - describe('makeSelectOverlayBlockerProps', () => { - it('should select the overlayBlockerData', () => { - const overlayBlockerData = fromJS({ title: 'title' }); - const mockedState = { - app: { - overlayBlockerData, - }, - }; - - expect(makeSelectOverlayBlockerProps()(mockedState)).toEqual(overlayBlockerData); - }); - }); - - describe('makeSelectShowGlobalAppBlocker', () => { - it('should select the showGlobalAppBlocker', () => { - const mockedState = { - app: { - showGlobalAppBlocker: true, - }, - }; - - expect(makeSelectShowGlobalAppBlocker()(mockedState)).toEqual(true); - }); - }); }); diff --git a/packages/core/admin/admin/src/containers/LocaleToggle/index.js b/packages/core/admin/admin/src/containers/LocaleToggle/index.js index 488598565d..53dc250495 100644 --- a/packages/core/admin/admin/src/containers/LocaleToggle/index.js +++ b/packages/core/admin/admin/src/containers/LocaleToggle/index.js @@ -11,11 +11,13 @@ import { createStructuredSelector } from 'reselect'; import { bindActionCreators, compose } from 'redux'; import cn from 'classnames'; import { ButtonDropdown, DropdownItem, DropdownMenu, DropdownToggle } from 'reactstrap'; +import translationMessages, { languageNativeNames } from '../../translations'; import makeSelectLocale from '../LanguageProvider/selectors'; import { changeLocale } from '../LanguageProvider/actions'; -import { languages, languageNativeNames } from '../../i18n'; import Wrapper from './Wrapper'; +// TODO +const languages = Object.keys(translationMessages); export class LocaleToggle extends React.Component { // eslint-disable-line state = { isOpen: false }; diff --git a/packages/core/admin/admin/src/containers/ProfilePage/index.js b/packages/core/admin/admin/src/containers/ProfilePage/index.js index a2005a1e7b..c30e6b5230 100644 --- a/packages/core/admin/admin/src/containers/ProfilePage/index.js +++ b/packages/core/admin/admin/src/containers/ProfilePage/index.js @@ -4,7 +4,7 @@ import { Padded, Text } from '@buffetjs/core'; import { Col } from 'reactstrap'; import { get } from 'lodash'; import { useIntl } from 'react-intl'; -import { languages, languageNativeNames } from '../../i18n'; +import translationMessages, { languageNativeNames } from '../../translations'; import ContainerFluid from '../../components/ContainerFluid'; import PageTitle from '../../components/PageTitle'; import SizedInput from '../../components/SizedInput'; @@ -15,6 +15,8 @@ import useChangeLanguage from '../LanguageProvider/hooks/useChangeLanguage'; import ProfilePageLabel from './components'; import { form, schema } from './utils'; +const languages = Object.keys(translationMessages); + const ProfilePage = () => { const changeLanguage = useChangeLanguage(); const { formatMessage } = useIntl(); diff --git a/packages/core/admin/admin/src/i18n.js b/packages/core/admin/admin/src/i18n.js deleted file mode 100644 index e38be61f32..0000000000 --- a/packages/core/admin/admin/src/i18n.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * i18n.js - * - * This will setup the i18n language files and locale data for your plugin. - * - */ - -// NOTE TO PLUGINS DEVELOPERS: -// If you modify this file you also need to update the documentation accordingly -// Here's the file: strapi/docs/3.0.0-beta.x/admin-panel/customization.md#customize-the-strapi-admin-package -// IF THE DOC IS NOT UPDATED THE PULL REQUEST WILL NOT BE MERGED - -import translationMessages, { languageNativeNames } from './translations'; - -const languages = Object.keys(translationMessages); - -export { languages, translationMessages, languageNativeNames }; diff --git a/packages/core/admin/admin/src/plugins.js b/packages/core/admin/admin/src/plugins.js deleted file mode 100644 index db088509db..0000000000 --- a/packages/core/admin/admin/src/plugins.js +++ /dev/null @@ -1,34 +0,0 @@ -/* eslint-disable */ - -const injectReducer = require('./utils/injectReducer').default; -const useInjectReducer = require('./utils/injectReducer').useInjectReducer; -const injectSaga = require('./utils/injectSaga').default; -const useInjectSaga = require('./utils/injectSaga').useInjectSaga; -const { languages } = require('./i18n'); - -window.strapi = Object.assign(window.strapi || {}, { - backendURL: BACKEND_URL === '/' ? window.location.origin : BACKEND_URL, - languages, - currentLanguage: - window.localStorage.getItem('strapi-admin-language') || - window.navigator.language || - window.navigator.userLanguage || - 'en', - injectReducer, - injectSaga, - useInjectReducer, - useInjectSaga, -}); - -module.exports = { - // FIXME: - 'strapi-plugin-documentation': require('../../../../plugins/documentation/admin/src').default, - 'strapi-plugin-users-permissions': require('../../../../plugins/users-permissions/admin/src') - .default, - 'strapi-plugin-content-manager': require('../../../content-manager/admin/src').default, - 'strapi-plugin-content-type-builder': require('../../../content-type-builder/admin/src').default, - 'strapi-plugin-email': require('../../../email/admin/src').default, - 'strapi-plugin-upload': require('../../../upload/admin/src').default, - 'strapi-plugin-graphql': require('../../../../plugins/graphql/admin/src').default, - 'strapi-plugin-i18n': require('../../../../plugins/i18n/admin/src').default, -}; diff --git a/packages/core/admin/admin/src/utils/checkStore.js b/packages/core/admin/admin/src/utils/checkStore.js deleted file mode 100644 index 96f2b18759..0000000000 --- a/packages/core/admin/admin/src/utils/checkStore.js +++ /dev/null @@ -1,21 +0,0 @@ -import conformsTo from 'lodash/conformsTo'; -import isFunction from 'lodash/isFunction'; -import isObject from 'lodash/isObject'; -import invariant from 'invariant'; - -/** - * Validate the shape of redux store - */ -export default function checkStore(store) { - const shape = { - dispatch: isFunction, - subscribe: isFunction, - getState: isFunction, - replaceReducer: isFunction, - runSaga: isFunction, - injectedReducers: isObject, - injectedSagas: isObject, - }; - - invariant(conformsTo(store, shape), '(app/utils...) injectors: Expected a valid redux store'); -} diff --git a/packages/core/admin/admin/src/utils/constants.js b/packages/core/admin/admin/src/utils/constants.js deleted file mode 100644 index 97ece0f3b3..0000000000 --- a/packages/core/admin/admin/src/utils/constants.js +++ /dev/null @@ -1,3 +0,0 @@ -export const RESTART_ON_REMOUNT = '@@saga-injector/restart-on-remount'; -export const DAEMON = '@@saga-injector/daemon'; -export const ONCE_TILL_UNMOUNT = '@@saga-injector/once-till-unmount'; diff --git a/packages/core/admin/admin/src/utils/history.js b/packages/core/admin/admin/src/utils/history.js deleted file mode 100644 index f0fbc2175b..0000000000 --- a/packages/core/admin/admin/src/utils/history.js +++ /dev/null @@ -1,6 +0,0 @@ -import { createBrowserHistory } from 'history'; -import basename from './basename'; - -const history = createBrowserHistory({ basename }); - -export default history; diff --git a/packages/core/admin/admin/src/utils/injectReducer.js b/packages/core/admin/admin/src/utils/injectReducer.js deleted file mode 100644 index 4b1780ad3e..0000000000 --- a/packages/core/admin/admin/src/utils/injectReducer.js +++ /dev/null @@ -1,56 +0,0 @@ -/* eslint-disable */ -import React from 'react'; -import hoistNonReactStatics from 'hoist-non-react-statics'; -import { ReactReduxContext } from 'react-redux'; - -import getInjectors from './reducerInjectors'; - -/** - * Dynamically injects a reducer - * - * @param {string} key A key of the reducer - * @param {function} reducer A reducer that will be injected - * - */ -export default ({ key, reducer, pluginId }) => WrappedComponent => { - class ReducerInjector extends React.Component { - static WrappedComponent = WrappedComponent; - static displayName = `withReducer(${WrappedComponent.displayName || - WrappedComponent.name || - 'Component'})`; - - static contextType = ReactReduxContext; - - constructor(props, context) { - super(props, context); - const reducerName = pluginId ? `${pluginId}_${key}` : key; - - console.warn( - 'Warning: strapi.injectReducer will be removed in the next major release. \n Please update your code.' - ); - - getInjectors(context.store).injectReducer(reducerName, reducer); - } - - render() { - return ; - } - } - - return hoistNonReactStatics(ReducerInjector, WrappedComponent); -}; - -const useInjectReducer = ({ key, reducer, pluginId }) => { - const context = React.useContext(ReactReduxContext); - const reducerName = pluginId ? `${pluginId}_${key}` : key; - - React.useEffect(() => { - console.warn( - 'Warning: strapi.useInjectReducer will be removed in the next major release. \n Please update your code.' - ); - - getInjectors(context.store).injectReducer(reducerName, reducer); - }, []); -}; - -export { useInjectReducer }; diff --git a/packages/core/admin/admin/src/utils/injectSaga.js b/packages/core/admin/admin/src/utils/injectSaga.js deleted file mode 100644 index 72add5f5a0..0000000000 --- a/packages/core/admin/admin/src/utils/injectSaga.js +++ /dev/null @@ -1,76 +0,0 @@ -/* eslint-disable */ -import React from 'react'; -import hoistNonReactStatics from 'hoist-non-react-statics'; -import { ReactReduxContext } from 'react-redux'; - -import getInjectors from './sagaInjectors'; - -/** - * Dynamically injects a saga, passes component's props as saga arguments - * - * @param {string} key A key of the saga - * @param {function} saga A root saga that will be injected - * @param {string} [mode] By default (constants.RESTART_ON_REMOUNT) the saga will be started on component mount and - * cancelled with `task.cancel()` on component un-mount for improved performance. Another two options: - * - constants.DAEMON—starts the saga on component mount and never cancels it or starts again, - * - constants.ONCE_TILL_UNMOUNT—behaves like 'RESTART_ON_REMOUNT' but never runs it again. - * - */ -export default ({ key, saga, mode, pluginId }) => WrappedComponent => { - class InjectSaga extends React.Component { - static WrappedComponent = WrappedComponent; - static displayName = `withSaga(${WrappedComponent.displayName || - WrappedComponent.name || - 'Component'})`; - - static contextType = ReactReduxContext; - - constructor(props, context) { - super(props, context); - - this.injectors = getInjectors(context.store); - const sagaName = pluginId ? `${pluginId}_${key}` : key; - - console.warn( - 'Warning: strapi.injectSaga will be removed in the next major release. \n Please update your code.' - ); - - this.injectors.injectSaga(sagaName, { saga, mode }, this.props); - } - - componentWillUnmount() { - const { ejectSaga } = this.injectors; - const sagaName = pluginId ? `${pluginId}_${key}` : key; - - ejectSaga(sagaName); - } - - injectors = getInjectors(this.context.store); - - render() { - return ; - } - } - - return hoistNonReactStatics(InjectSaga, WrappedComponent); -}; - -const useInjectSaga = ({ key, saga, mode, pluginId }) => { - const context = React.useContext(ReactReduxContext); - const sagaName = pluginId ? `${pluginId}_${key}` : key; - - React.useEffect(() => { - const injectors = getInjectors(context.store); - injectors.injectSaga(sagaName, { saga, mode }); - - console.warn( - 'Warning: strapi.useInjectSaga will be removed in the next major release. \n Please update your code.' - ); - - return () => { - injectors.ejectSaga(sagaName); - }; - }, []); -}; - -export { useInjectSaga }; diff --git a/packages/core/admin/admin/src/utils/reducerInjectors.js b/packages/core/admin/admin/src/utils/reducerInjectors.js deleted file mode 100644 index d755afb8f9..0000000000 --- a/packages/core/admin/admin/src/utils/reducerInjectors.js +++ /dev/null @@ -1,34 +0,0 @@ -/* eslint-disable */ - -import invariant from 'invariant'; -import isEmpty from 'lodash/isEmpty'; -import isFunction from 'lodash/isFunction'; -import isString from 'lodash/isString'; - -import createReducer from '../reducers'; -import checkStore from './checkStore'; - -export function injectReducerFactory(store, isValid) { - return function injectReducer(key, reducer) { - if (!isValid) checkStore(store); - - invariant( - isString(key) && !isEmpty(key) && isFunction(reducer), - '(app/utils...) injectReducer: Expected `reducer` to be a reducer function' - ); - - // Check `store.injectedReducers[key] === reducer` for hot reloading when a key is the same but a reducer is different - if (Reflect.has(store.injectedReducers, key) && store.injectedReducers[key] === reducer) return; - - store.injectedReducers[key] = reducer; // eslint-disable-line no-param-reassign - store.replaceReducer(createReducer(store.injectedReducers)); - }; -} - -export default function getInjectors(store) { - checkStore(store); - - return { - injectReducer: injectReducerFactory(store, true), - }; -} diff --git a/packages/core/admin/admin/src/utils/sagaInjectors.js b/packages/core/admin/admin/src/utils/sagaInjectors.js deleted file mode 100644 index eb622ebd46..0000000000 --- a/packages/core/admin/admin/src/utils/sagaInjectors.js +++ /dev/null @@ -1,91 +0,0 @@ -/* eslint-disable */ - -import isEmpty from 'lodash/isEmpty'; -import isFunction from 'lodash/isFunction'; -import isString from 'lodash/isString'; -import invariant from 'invariant'; -import conformsTo from 'lodash/conformsTo'; - -import checkStore from './checkStore'; -import { DAEMON, ONCE_TILL_UNMOUNT, RESTART_ON_REMOUNT } from './constants'; - -const allowedModes = [RESTART_ON_REMOUNT, DAEMON, ONCE_TILL_UNMOUNT]; - -const checkKey = key => - invariant( - isString(key) && !isEmpty(key), - '(app/utils...) injectSaga: Expected `key` to be a non empty string' - ); - -const checkDescriptor = descriptor => { - const shape = { - saga: isFunction, - mode: mode => isString(mode) && allowedModes.includes(mode), - }; - invariant( - conformsTo(descriptor, shape), - '(app/utils...) injectSaga: Expected a valid saga descriptor' - ); -}; - -export function injectSagaFactory(store, isValid) { - return function injectSaga(key, descriptor = {}, args) { - if (!isValid) checkStore(store); - - const newDescriptor = { - ...descriptor, - mode: descriptor.mode || RESTART_ON_REMOUNT, - }; - const { saga, mode } = newDescriptor; - - checkKey(key); - checkDescriptor(newDescriptor); - - let hasSaga = Reflect.has(store.injectedSagas, key); - - if (process.env.NODE_ENV !== 'production') { - const oldDescriptor = store.injectedSagas[key]; - // enable hot reloading of daemon and once-till-unmount sagas - if (hasSaga && oldDescriptor.saga !== saga) { - oldDescriptor.task.cancel(); - hasSaga = false; - } - } - - if (!hasSaga || (hasSaga && mode !== DAEMON && mode !== ONCE_TILL_UNMOUNT)) { - store.injectedSagas[key] = { - ...newDescriptor, - task: store.runSaga(saga, args), - }; // eslint-disable-line no-param-reassign - } - }; -} - -export function ejectSagaFactory(store, isValid) { - return function ejectSaga(key) { - if (!isValid) checkStore(store); - - checkKey(key); - - if (Reflect.has(store.injectedSagas, key)) { - const descriptor = store.injectedSagas[key]; - if (descriptor.mode !== DAEMON) { - descriptor.task.cancel(); - // Clean up in production; in development we need `descriptor.saga` for hot reloading - if (process.env.NODE_ENV === 'production') { - // Need some value to be able to detect `ONCE_TILL_UNMOUNT` sagas in `injectSaga` - store.injectedSagas[key] = 'done'; // eslint-disable-line no-param-reassign - } - } - } - }; -} - -export default function getInjectors(store) { - checkStore(store); - - return { - injectSaga: injectSagaFactory(store, true), - ejectSaga: ejectSagaFactory(store, true), - }; -}