Files
SCM-Manager/scm-ui/src/containers/PluginLoader.js

115 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-08-24 12:43:10 +02:00
// @flow
import * as React from "react";
import { apiClient, Loading } from "@scm-manager/ui-components";
import {
callFetchIndexResources,
getUiPluginsLink
} from "../modules/indexResource";
import { connect } from "react-redux";
2018-08-24 12:43:10 +02:00
type Props = {
children: React.Node,
link: string
2018-08-24 12:43:10 +02:00
};
type State = {
finished: boolean,
message: string
};
type Plugin = {
2018-08-30 11:40:53 +02:00
name: string,
2018-08-24 12:43:10 +02:00
bundles: string[]
};
class PluginLoader extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
finished: false,
message: "booting"
};
}
componentDidMount() {
this.setState({
message: "loading plugin information"
});
callFetchIndexResources().then(response => {
const link = response._links.uiPlugins.href;
this.getPlugins(link);
});
}
getPlugins = (link: string) => {
2018-08-24 12:43:10 +02:00
apiClient
.get(link)
2018-08-24 12:43:10 +02:00
.then(response => response.text())
.then(JSON.parse)
.then(pluginCollection => pluginCollection._embedded.plugins)
2018-08-24 12:43:10 +02:00
.then(this.loadPlugins)
.then(() => {
this.setState({
finished: true
});
});
};
2018-08-24 12:43:10 +02:00
loadPlugins = (plugins: Plugin[]) => {
this.setState({
message: "loading plugins"
});
const promises = [];
for (let plugin of plugins) {
promises.push(this.loadPlugin(plugin));
}
return Promise.all(promises);
};
loadPlugin = (plugin: Plugin) => {
this.setState({
message: `loading ${plugin.name}`
});
const promises = [];
for (let bundle of plugin.bundles) {
2018-08-27 15:47:02 +02:00
promises.push(this.loadBundle(bundle));
2018-08-24 12:43:10 +02:00
}
return Promise.all(promises);
};
loadBundle = (bundle: string) => {
return fetch(bundle)
.then(response => {
return response.text();
})
.then(script => {
// TODO is this safe???
2018-08-31 10:47:42 +02:00
// eslint-disable-next-line no-eval
2018-08-30 11:56:33 +02:00
eval(script); // NOSONAR
2018-08-24 12:43:10 +02:00
});
};
render() {
const { message, finished } = this.state;
if (finished) {
return <div>{this.props.children}</div>;
}
return <Loading message={message} />;
}
}
const mapStateToProps = state => {
const link = getUiPluginsLink(state);
return {
link
};
};
export default connect(
mapStateToProps,
null
)(PluginLoader);