mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-15 09:46:16 +01:00
only fetch index Resources once (and not twice)
This commit is contained in:
@@ -9,12 +9,15 @@ import { Loading, ErrorPage } from "@scm-manager/ui-components";
|
||||
import {
|
||||
fetchIndexResources,
|
||||
getFetchIndexResourcesFailure,
|
||||
getLinks,
|
||||
isFetchIndexResourcesPending
|
||||
} from "../modules/indexResource";
|
||||
import PluginLoader from "./PluginLoader";
|
||||
|
||||
type Props = {
|
||||
error: Error,
|
||||
loading: boolean,
|
||||
indexResources: any,
|
||||
|
||||
// dispatcher functions
|
||||
fetchIndexResources: () => void,
|
||||
@@ -29,11 +32,9 @@ class Index extends Component<Props> {
|
||||
}
|
||||
|
||||
render() {
|
||||
const { loading, error, t } = this.props;
|
||||
const { indexResources, loading, error, t } = this.props;
|
||||
|
||||
if (loading) {
|
||||
return <Loading />;
|
||||
} else if (error) {
|
||||
if (error) {
|
||||
return (
|
||||
<ErrorPage
|
||||
title={t("app.error.title")}
|
||||
@@ -41,8 +42,15 @@ class Index extends Component<Props> {
|
||||
error={error}
|
||||
/>
|
||||
);
|
||||
} else {
|
||||
return <App />;
|
||||
}
|
||||
else if (loading || !indexResources) {
|
||||
return <Loading />;
|
||||
} else {
|
||||
return (
|
||||
<PluginLoader>
|
||||
<App />
|
||||
</PluginLoader>
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,9 +64,11 @@ const mapDispatchToProps = (dispatch: any) => {
|
||||
const mapStateToProps = state => {
|
||||
const loading = isFetchIndexResourcesPending(state);
|
||||
const error = getFetchIndexResourcesFailure(state);
|
||||
const indexResources = getLinks(state);
|
||||
return {
|
||||
loading,
|
||||
error
|
||||
error,
|
||||
indexResources
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
// @flow
|
||||
import * as React from "react";
|
||||
import { apiClient, Loading } from "@scm-manager/ui-components";
|
||||
import { callFetchIndexResources } from "../modules/indexResource";
|
||||
import { getUiPluginsLink } from "../modules/indexResource";
|
||||
import { connect } from "react-redux";
|
||||
|
||||
type Props = {
|
||||
children: React.Node
|
||||
children: React.Node,
|
||||
link: string
|
||||
};
|
||||
|
||||
type State = {
|
||||
@@ -31,10 +33,7 @@ class PluginLoader extends React.Component<Props, State> {
|
||||
message: "loading plugin information"
|
||||
});
|
||||
|
||||
callFetchIndexResources().then(response => {
|
||||
const link = response._links.uiPlugins.href;
|
||||
this.getPlugins(link);
|
||||
});
|
||||
this.getPlugins(this.props.link);
|
||||
}
|
||||
|
||||
getPlugins = (link: string): Promise<any> => {
|
||||
@@ -96,4 +95,11 @@ class PluginLoader extends React.Component<Props, State> {
|
||||
}
|
||||
}
|
||||
|
||||
export default PluginLoader;
|
||||
const mapStateToProps = state => {
|
||||
const link = getUiPluginsLink(state);
|
||||
return {
|
||||
link
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps)(PluginLoader);
|
||||
|
||||
@@ -37,9 +37,7 @@ ReactDOM.render(
|
||||
<I18nextProvider i18n={i18n}>
|
||||
{/* ConnectedRouter will use the store from Provider automatically */}
|
||||
<ConnectedRouter history={history}>
|
||||
<PluginLoader>
|
||||
<Index />
|
||||
</PluginLoader>
|
||||
</ConnectedRouter>
|
||||
</I18nextProvider>
|
||||
</Provider>,
|
||||
|
||||
@@ -5,7 +5,12 @@ import * as types from "./types";
|
||||
import { apiClient, UNAUTHORIZED_ERROR } from "@scm-manager/ui-components";
|
||||
import { isPending } from "./pending";
|
||||
import { getFailure } from "./failure";
|
||||
import { callFetchIndexResources, fetchIndexResources } from "./indexResource";
|
||||
import {
|
||||
callFetchIndexResources,
|
||||
FETCH_INDEXRESOURCES_SUCCESS,
|
||||
fetchIndexResources,
|
||||
fetchIndexResourcesSuccess
|
||||
} from "./indexResource";
|
||||
|
||||
// Action
|
||||
|
||||
@@ -154,15 +159,13 @@ export const login = (
|
||||
return callFetchIndexResources();
|
||||
})
|
||||
.then(response => {
|
||||
dispatch(fetchIndexResourcesSuccess(response));
|
||||
const meLink = response._links.me.href;
|
||||
return callFetchMe(meLink);
|
||||
})
|
||||
.then(me => {
|
||||
dispatch(loginSuccess(me));
|
||||
})
|
||||
.then(() => {
|
||||
dispatch(fetchIndexResources());
|
||||
})
|
||||
.catch(err => {
|
||||
dispatch(loginFailure(err));
|
||||
});
|
||||
|
||||
@@ -32,6 +32,10 @@ import reducer, {
|
||||
import configureMockStore from "redux-mock-store";
|
||||
import thunk from "redux-thunk";
|
||||
import fetchMock from "fetch-mock";
|
||||
import {
|
||||
FETCH_INDEXRESOURCES_PENDING,
|
||||
FETCH_INDEXRESOURCES_SUCCESS
|
||||
} from "./indexResource";
|
||||
|
||||
const me = { name: "tricia", displayName: "Tricia McMillian" };
|
||||
|
||||
@@ -193,9 +197,19 @@ describe("auth actions", () => {
|
||||
status: 401
|
||||
});
|
||||
|
||||
fetchMock.getOnce("/api/v2/", {
|
||||
_links: {
|
||||
login: {
|
||||
login: "/login"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const expectedActions = [
|
||||
{ type: LOGOUT_PENDING },
|
||||
{ type: LOGOUT_SUCCESS }
|
||||
{ type: LOGOUT_SUCCESS },
|
||||
{ type: FETCH_INDEXRESOURCES_PENDING },
|
||||
{ type: FETCH_INDEXRESOURCES_SUCCESS }
|
||||
];
|
||||
|
||||
const store = mockStore({});
|
||||
|
||||
Reference in New Issue
Block a user