Files
SCM-Manager/scm-ui/src/config/containers/GlobalConfig.js

101 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-08-09 10:18:12 +02:00
import React from "react";
import { translate } from "react-i18next";
import Title from "../../components/layout/Title";
2018-08-09 12:09:50 +02:00
import {
fetchConfig,
getFetchConfigFailure,
2018-08-13 16:35:43 +02:00
isFetchConfigPending,
getConfig,
modifyConfig,
isModifyConfigPending
2018-08-09 12:09:50 +02:00
} from "../modules/config";
import connect from "react-redux/es/connect/connect";
import ErrorPage from "../../components/ErrorPage";
2018-08-13 16:35:43 +02:00
import type { Config } from "../types/Config";
import ConfigForm from "../components/form/ConfigForm";
2018-08-09 12:09:50 +02:00
import Loading from "../../components/Loading";
2018-08-13 16:35:43 +02:00
import type { User } from "../../users/types/User";
import type { History } from "history";
2018-08-09 10:18:12 +02:00
type Props = {
2018-08-09 12:09:50 +02:00
loading: boolean,
error: Error,
2018-08-13 16:35:43 +02:00
config: Config,
// dispatch functions
modifyConfig: (config: User, callback?: () => void) => void,
2018-08-09 10:18:12 +02:00
// context objects
2018-08-09 12:09:50 +02:00
t: string => string,
2018-08-13 16:35:43 +02:00
fetchConfig: void => void,
history: History
2018-08-09 10:18:12 +02:00
};
class GlobalConfig extends React.Component<Props> {
2018-08-13 16:35:43 +02:00
configModified = (config: Config) => () => {
this.props.history.push(`/config`);
};
2018-08-09 12:09:50 +02:00
componentDidMount() {
this.props.fetchConfig();
}
2018-08-13 16:35:43 +02:00
modifyConfig = (config: Config) => {
this.props.modifyConfig(config, this.configModified(config));
};
2018-08-09 10:18:12 +02:00
render() {
2018-08-13 16:35:43 +02:00
const { t, error, loading, config } = this.props;
2018-08-09 12:09:50 +02:00
if (error) {
return (
<ErrorPage
title={t("global-config.error-title")}
subtitle={t("global-config.error-subtitle")}
error={error}
/>
);
}
if (loading) {
return <Loading />;
}
2018-08-09 10:18:12 +02:00
2018-08-13 16:35:43 +02:00
return (
<div>
<Title title={t("global-config.title")} />
<ConfigForm
submitForm={config => this.modifyConfig(config)}
config={config}
loading={loading}
/>
</div>
);
2018-08-09 10:18:12 +02:00
}
}
2018-08-09 12:09:50 +02:00
const mapDispatchToProps = dispatch => {
return {
fetchConfig: () => {
dispatch(fetchConfig());
2018-08-13 16:35:43 +02:00
},
modifyConfig: (config: Config, callback?: () => void) => {
dispatch(modifyConfig(config, callback));
2018-08-09 12:09:50 +02:00
}
};
};
const mapStateToProps = state => {
2018-08-13 16:35:43 +02:00
const loading = isFetchConfigPending(state) || isModifyConfigPending(state); //TODO: Button lädt so nicht, sondern gesamte Seite
2018-08-09 12:09:50 +02:00
const error = getFetchConfigFailure(state);
2018-08-13 16:35:43 +02:00
const config = getConfig(state);
2018-08-09 12:09:50 +02:00
return {
loading,
2018-08-13 16:35:43 +02:00
error,
config
2018-08-09 12:09:50 +02:00
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(translate("config")(GlobalConfig));