From efdc57055ddac46a3c0eb81f276ae2836928b603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maren=20S=C3=BCwer?= Date: Mon, 15 Oct 2018 13:46:42 +0200 Subject: [PATCH] only fetch index Resources once (and not twice) --- scm-ui/src/containers/Index.js | 24 +++++++++++++++++------- scm-ui/src/containers/PluginLoader.js | 20 +++++++++++++------- scm-ui/src/index.js | 2 -- scm-ui/src/modules/auth.js | 11 +++++++---- scm-ui/src/modules/auth.test.js | 16 +++++++++++++++- 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/scm-ui/src/containers/Index.js b/scm-ui/src/containers/Index.js index 0e79b1487e..55d5fc1ab8 100644 --- a/scm-ui/src/containers/Index.js +++ b/scm-ui/src/containers/Index.js @@ -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 { } render() { - const { loading, error, t } = this.props; + const { indexResources, loading, error, t } = this.props; - if (loading) { - return ; - } else if (error) { + if (error) { return ( { error={error} /> ); - } else { - return ; + } + else if (loading || !indexResources) { + return ; + } else { + return ( + + + + ); } } } @@ -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 }; }; diff --git a/scm-ui/src/containers/PluginLoader.js b/scm-ui/src/containers/PluginLoader.js index e7aeff69c2..8e44a1d427 100644 --- a/scm-ui/src/containers/PluginLoader.js +++ b/scm-ui/src/containers/PluginLoader.js @@ -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 { 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 => { @@ -96,4 +95,11 @@ class PluginLoader extends React.Component { } } -export default PluginLoader; +const mapStateToProps = state => { + const link = getUiPluginsLink(state); + return { + link + }; +}; + +export default connect(mapStateToProps)(PluginLoader); diff --git a/scm-ui/src/index.js b/scm-ui/src/index.js index c00048be36..3ecd38e6d0 100644 --- a/scm-ui/src/index.js +++ b/scm-ui/src/index.js @@ -37,9 +37,7 @@ ReactDOM.render( {/* ConnectedRouter will use the store from Provider automatically */} - - , diff --git a/scm-ui/src/modules/auth.js b/scm-ui/src/modules/auth.js index 9a466b4802..47461106c0 100644 --- a/scm-ui/src/modules/auth.js +++ b/scm-ui/src/modules/auth.js @@ -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)); }); diff --git a/scm-ui/src/modules/auth.test.js b/scm-ui/src/modules/auth.test.js index 1f5efb31a7..99695bb4d4 100644 --- a/scm-ui/src/modules/auth.test.js +++ b/scm-ui/src/modules/auth.test.js @@ -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({});