fix duplicate loading of plugins

This commit is contained in:
Sebastian Sdorra
2018-11-10 14:54:20 +01:00
parent 451a170cb2
commit 6f36031fcf
2 changed files with 36 additions and 17 deletions

View File

@@ -27,13 +27,32 @@ type Props = {
t: string => string t: string => string
}; };
class Index extends Component<Props> { type State = {
pluginsLoaded: boolean
};
class Index extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
pluginsLoaded: false
};
}
componentDidMount() { componentDidMount() {
this.props.fetchIndexResources(); this.props.fetchIndexResources();
} }
pluginLoaderCallback = () => {
this.setState({
pluginsLoaded: true
});
};
render() { render() {
const { indexResources, loading, error, t } = this.props; const { indexResources, loading, error, t } = this.props;
const { pluginsLoaded } = this.state;
if (error) { if (error) {
return ( return (
@@ -47,7 +66,7 @@ class Index extends Component<Props> {
return <Loading />; return <Loading />;
} else { } else {
return ( return (
<PluginLoader> <PluginLoader loaded={ pluginsLoaded } callback={ this.pluginLoaderCallback }>
<App /> <App />
</PluginLoader> </PluginLoader>
); );

View File

@@ -5,12 +5,13 @@ import { getUiPluginsLink } from "../modules/indexResource";
import { connect } from "react-redux"; import { connect } from "react-redux";
type Props = { type Props = {
loaded: boolean,
children: React.Node, children: React.Node,
link: string link: string,
callback: () => void
}; };
type State = { type State = {
finished: boolean,
message: string message: string
}; };
@@ -23,18 +24,20 @@ class PluginLoader extends React.Component<Props, State> {
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
this.state = { this.state = {
finished: false,
message: "booting" message: "booting"
}; };
} }
componentDidMount() { componentDidMount() {
const { loaded } = this.props;
if (!loaded) {
this.setState({ this.setState({
message: "loading plugin information" message: "loading plugin information"
}); });
this.getPlugins(this.props.link); this.getPlugins(this.props.link);
} }
}
getPlugins = (link: string): Promise<any> => { getPlugins = (link: string): Promise<any> => {
return apiClient return apiClient
@@ -43,11 +46,7 @@ class PluginLoader extends React.Component<Props, State> {
.then(JSON.parse) .then(JSON.parse)
.then(pluginCollection => pluginCollection._embedded.plugins) .then(pluginCollection => pluginCollection._embedded.plugins)
.then(this.loadPlugins) .then(this.loadPlugins)
.then(() => { .then(this.props.callback);
this.setState({
finished: true
});
});
}; };
loadPlugins = (plugins: Plugin[]) => { loadPlugins = (plugins: Plugin[]) => {
@@ -87,8 +86,9 @@ class PluginLoader extends React.Component<Props, State> {
}; };
render() { render() {
const { message, finished } = this.state; const { loaded } = this.props;
if (finished) { const { message } = this.state;
if (loaded) {
return <div>{this.props.children}</div>; return <div>{this.props.children}</div>;
} }
return <Loading message={message} />; return <Loading message={message} />;