mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 14:35:45 +01:00
handle error and loading state
This commit is contained in:
@@ -1,7 +1,11 @@
|
|||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
|
"navigation-title": "Navigation"
|
||||||
|
},
|
||||||
|
"global-config": {
|
||||||
"title": "Configuration",
|
"title": "Configuration",
|
||||||
"navigation-title": "Navigation",
|
"navigation-label": "Global Configuration",
|
||||||
"globalConfig-label": "Global Configuration"
|
"error-title": "Error",
|
||||||
|
"error-subtitle": "Unknown Config Error"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ class Config extends React.Component<Props> {
|
|||||||
<div className="column">
|
<div className="column">
|
||||||
<Navigation>
|
<Navigation>
|
||||||
<Section label={t("config.navigation-title")}>
|
<Section label={t("config.navigation-title")}>
|
||||||
<NavLink to={`${url}`} label={t("config.globalConfig-label")} />
|
<NavLink to={`${url}`} label={t("global-config.navigation-label")} />
|
||||||
</Section>
|
</Section>
|
||||||
</Navigation>
|
</Navigation>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,18 +1,69 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { translate } from "react-i18next";
|
import { translate } from "react-i18next";
|
||||||
import Title from "../../components/layout/Title";
|
import Title from "../../components/layout/Title";
|
||||||
|
import {
|
||||||
|
fetchConfig,
|
||||||
|
getFetchConfigFailure,
|
||||||
|
isFetchConfigPending
|
||||||
|
} from "../modules/config";
|
||||||
|
import connect from "react-redux/es/connect/connect";
|
||||||
|
import ErrorPage from "../../components/ErrorPage";
|
||||||
|
import Loading from "../../components/Loading";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
|
loading: boolean,
|
||||||
|
error: Error,
|
||||||
|
|
||||||
// context objects
|
// context objects
|
||||||
t: string => string
|
t: string => string,
|
||||||
|
fetchConfig: void => void
|
||||||
};
|
};
|
||||||
|
|
||||||
class GlobalConfig extends React.Component<Props> {
|
class GlobalConfig extends React.Component<Props> {
|
||||||
render() {
|
componentDidMount() {
|
||||||
const { t } = this.props;
|
this.props.fetchConfig();
|
||||||
|
}
|
||||||
|
|
||||||
return <Title title={t("config.title")} />;
|
render() {
|
||||||
|
const { t, error, loading } = this.props;
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return (
|
||||||
|
<ErrorPage
|
||||||
|
title={t("global-config.error-title")}
|
||||||
|
subtitle={t("global-config.error-subtitle")}
|
||||||
|
error={error}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loading) {
|
||||||
|
return <Loading />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return <Title title={t("global-config.title")} />;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default translate("config")(GlobalConfig);
|
const mapDispatchToProps = dispatch => {
|
||||||
|
return {
|
||||||
|
fetchConfig: () => {
|
||||||
|
dispatch(fetchConfig());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const mapStateToProps = state => {
|
||||||
|
const loading = isFetchConfigPending(state);
|
||||||
|
const error = getFetchConfigFailure(state);
|
||||||
|
|
||||||
|
return {
|
||||||
|
loading,
|
||||||
|
error
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default connect(
|
||||||
|
mapStateToProps,
|
||||||
|
mapDispatchToProps
|
||||||
|
)(translate("config")(GlobalConfig));
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
import { apiClient } from "../../apiclient";
|
import { apiClient } from "../../apiclient";
|
||||||
import * as types from "../../modules/types";
|
import * as types from "../../modules/types";
|
||||||
import type { Action } from "../../types/Action";
|
import type { Action } from "../../types/Action";
|
||||||
|
import { isPending } from "../../modules/pending";
|
||||||
|
import { getFailure } from "../../modules/failure";
|
||||||
|
|
||||||
export const FETCH_CONFIG = "scm/groups/FETCH_CONFIG";
|
export const FETCH_CONFIG = "scm/groups/FETCH_CONFIG";
|
||||||
export const FETCH_CONFIG_PENDING = `${FETCH_CONFIG}_${types.PENDING_SUFFIX}`;
|
export const FETCH_CONFIG_PENDING = `${FETCH_CONFIG}_${types.PENDING_SUFFIX}`;
|
||||||
@@ -69,3 +71,13 @@ function reducer(state: any = {}, action: any = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default reducer;
|
export default reducer;
|
||||||
|
|
||||||
|
// selectors
|
||||||
|
|
||||||
|
export function isFetchConfigPending(state: Object) {
|
||||||
|
return isPending(state, FETCH_CONFIG);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getFetchConfigFailure(state: Object) {
|
||||||
|
return getFailure(state, FETCH_CONFIG);
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,11 +4,14 @@ import thunk from "redux-thunk";
|
|||||||
import fetchMock from "fetch-mock";
|
import fetchMock from "fetch-mock";
|
||||||
|
|
||||||
import reducer, {
|
import reducer, {
|
||||||
|
FETCH_CONFIG,
|
||||||
FETCH_CONFIG_PENDING,
|
FETCH_CONFIG_PENDING,
|
||||||
FETCH_CONFIG_SUCCESS,
|
FETCH_CONFIG_SUCCESS,
|
||||||
FETCH_CONFIG_FAILURE,
|
FETCH_CONFIG_FAILURE,
|
||||||
fetchConfig,
|
fetchConfig,
|
||||||
fetchConfigSuccess
|
fetchConfigSuccess,
|
||||||
|
getFetchConfigFailure,
|
||||||
|
isFetchConfigPending
|
||||||
} from "./config";
|
} from "./config";
|
||||||
|
|
||||||
const CONFIG_URL = "/scm/api/rest/v2/config";
|
const CONFIG_URL = "/scm/api/rest/v2/config";
|
||||||
@@ -114,3 +117,31 @@ describe("config reducer", () => {
|
|||||||
expect(newState.config.entries).toBe(config);
|
expect(newState.config.entries).toBe(config);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("selector tests", () => {
|
||||||
|
it("should return true, when fetch config is pending", () => {
|
||||||
|
const state = {
|
||||||
|
pending: {
|
||||||
|
[FETCH_CONFIG]: true
|
||||||
|
}
|
||||||
|
};
|
||||||
|
expect(isFetchConfigPending(state)).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return false, when fetch config is not pending", () => {
|
||||||
|
expect(isFetchConfigPending({})).toEqual(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return error when fetch config did fail", () => {
|
||||||
|
const state = {
|
||||||
|
failure: {
|
||||||
|
[FETCH_CONFIG]: error
|
||||||
|
}
|
||||||
|
};
|
||||||
|
expect(getFetchConfigFailure(state)).toEqual(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return undefined when fetch config did not fail", () => {
|
||||||
|
expect(getFetchConfigFailure({})).toBe(undefined);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user