2017-12-21 04:56:45 -06:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
2018-09-14 02:41:37 -05:00
|
|
|
import { Provider } from 'react-redux';
|
2018-04-26 04:58:42 -05:00
|
|
|
|
2017-12-21 04:56:45 -06:00
|
|
|
import coreModule from 'app/core/core_module';
|
2018-11-05 09:54:48 -06:00
|
|
|
import { store } from 'app/store/store';
|
2018-05-30 06:13:29 -05:00
|
|
|
import { ContextSrv } from 'app/core/services/context_srv';
|
2019-02-05 10:04:48 -06:00
|
|
|
import { provideTheme } from 'app/core/utils/ConfigProvider';
|
2017-12-21 04:56:45 -06:00
|
|
|
|
|
|
|
function WrapInProvider(store, Component, props) {
|
|
|
|
return (
|
2018-09-14 02:41:37 -05:00
|
|
|
<Provider store={store}>
|
|
|
|
<Component {...props} />
|
|
|
|
</Provider>
|
2017-12-21 04:56:45 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @ngInject */
|
2019-02-13 04:14:53 -06:00
|
|
|
export function reactContainer($route, $location, $injector, $rootScope, contextSrv: ContextSrv) {
|
2017-12-21 04:56:45 -06:00
|
|
|
return {
|
|
|
|
restrict: 'E',
|
2017-12-27 06:15:27 -06:00
|
|
|
template: '',
|
2017-12-21 04:56:45 -06:00
|
|
|
link(scope, elem) {
|
2018-05-30 06:13:29 -05:00
|
|
|
// Check permissions for this component
|
|
|
|
const { roles } = $route.current.locals;
|
|
|
|
if (roles && roles.length) {
|
|
|
|
if (!roles.some(r => contextSrv.hasRole(r))) {
|
|
|
|
$location.url('/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let { component } = $route.current.locals;
|
2018-04-27 04:49:11 -05:00
|
|
|
// Dynamic imports return whole module, need to extract default export
|
|
|
|
if (component.default) {
|
|
|
|
component = component.default;
|
|
|
|
}
|
2018-05-30 06:13:29 -05:00
|
|
|
|
2019-01-31 02:44:12 -06:00
|
|
|
const props = {
|
|
|
|
$injector: $injector,
|
|
|
|
$rootScope: $rootScope,
|
|
|
|
$scope: scope,
|
2019-02-04 06:49:14 -06:00
|
|
|
routeInfo: $route.current.$$route.routeInfo,
|
2019-01-31 02:44:12 -06:00
|
|
|
};
|
2017-12-21 04:56:45 -06:00
|
|
|
|
2019-02-05 05:10:42 -06:00
|
|
|
document.body.classList.add('is-react');
|
|
|
|
|
2019-02-05 10:04:48 -06:00
|
|
|
ReactDOM.render(WrapInProvider(store, provideTheme(component), props), elem[0]);
|
2017-12-21 04:56:45 -06:00
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
scope.$on('$destroy', () => {
|
2019-02-05 05:10:42 -06:00
|
|
|
document.body.classList.remove('is-react');
|
2017-12-21 04:56:45 -06:00
|
|
|
ReactDOM.unmountComponentAtNode(elem[0]);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
coreModule.directive('reactContainer', reactContainer);
|