Files
SCM-Manager/scm-ui/src/config/components/form/ConfigForm.js

218 lines
5.9 KiB
JavaScript
Raw Normal View History

2018-08-13 16:33:56 +02:00
// @flow
import React from "react";
import { translate } from "react-i18next";
import { SubmitButton, Notification } from "@scm-manager/ui-components";
import type { Config } from "@scm-manager/ui-types";
2018-08-13 16:33:56 +02:00
import ProxySettings from "./ProxySettings";
import GeneralSettings from "./GeneralSettings";
import BaseUrlSettings from "./BaseUrlSettings";
import AdminSettings from "./AdminSettings";
2018-08-16 16:41:17 +02:00
import LoginAttempt from "./LoginAttempt";
2018-08-13 16:33:56 +02:00
type Props = {
submitForm: Config => void,
config?: Config,
loading?: boolean,
t: string => string,
2019-03-04 16:47:44 +01:00
configReadPermission: boolean,
configUpdatePermission: boolean
2018-08-13 16:33:56 +02:00
};
type State = {
config: Config,
2018-08-16 16:41:17 +02:00
showNotification: boolean,
2018-08-21 08:40:49 +02:00
error: {
loginAttemptLimitTimeout: boolean,
loginAttemptLimit: boolean
},
changed: boolean
2018-08-13 16:33:56 +02:00
};
class ConfigForm extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
config: {
proxyPassword: null,
proxyPort: 0,
proxyServer: "",
proxyUser: null,
enableProxy: false,
realmDescription: "",
enableRepositoryArchive: false,
disableGroupingGrid: false,
dateFormat: "",
anonymousAccessEnabled: false,
adminGroups: [],
adminUsers: [],
baseUrl: "",
forceBaseUrl: false,
loginAttemptLimit: 0,
proxyExcludes: [],
skipFailedAuthenticators: false,
pluginUrl: "",
loginAttemptLimitTimeout: 0,
enabledXsrfProtection: true,
defaultNamespaceStrategy: "",
_links: {}
},
2018-08-16 16:41:17 +02:00
showNotification: false,
2018-08-21 08:40:49 +02:00
error: {
loginAttemptLimitTimeout: false,
loginAttemptLimit: false
},
changed: false
2018-08-13 16:33:56 +02:00
};
}
componentDidMount() {
const { config, configUpdatePermission } = this.props;
2018-08-13 16:33:56 +02:00
if (config) {
this.setState({ ...this.state, config: { ...config } });
}
if (!configUpdatePermission) {
this.setState({ ...this.state, showNotification: true });
2018-08-13 16:33:56 +02:00
}
}
submit = (event: Event) => {
event.preventDefault();
this.setState({
changed: false
});
2018-08-13 16:33:56 +02:00
this.props.submitForm(this.state.config);
};
render() {
2019-03-04 16:47:44 +01:00
const {
loading,
t,
configReadPermission,
configUpdatePermission
} = this.props;
const config = this.state.config;
let noPermissionNotification = null;
2019-03-04 16:47:44 +01:00
if (!configReadPermission) {
return (
<Notification
type={"danger"}
children={t("config-form.no-read-permission-notification")}
/>
);
}
if (this.state.showNotification) {
noPermissionNotification = (
<Notification
type={"info"}
2019-03-04 16:47:44 +01:00
children={t("config-form.no-write-permission-notification")}
onClose={() => this.onClose()}
/>
);
}
2018-08-13 16:33:56 +02:00
return (
<form onSubmit={this.submit}>
{noPermissionNotification}
2018-08-13 16:33:56 +02:00
<GeneralSettings
realmDescription={config.realmDescription}
enableRepositoryArchive={config.enableRepositoryArchive}
disableGroupingGrid={config.disableGroupingGrid}
dateFormat={config.dateFormat}
anonymousAccessEnabled={config.anonymousAccessEnabled}
skipFailedAuthenticators={config.skipFailedAuthenticators}
pluginUrl={config.pluginUrl}
enabledXsrfProtection={config.enabledXsrfProtection}
defaultNamespaceStrategy={config.defaultNamespaceStrategy}
onChange={(isValid, changedValue, name) =>
this.onChange(isValid, changedValue, name)
}
hasUpdatePermission={configUpdatePermission}
2018-08-13 16:33:56 +02:00
/>
<hr />
2018-08-16 16:41:17 +02:00
<LoginAttempt
loginAttemptLimit={config.loginAttemptLimit}
loginAttemptLimitTimeout={config.loginAttemptLimitTimeout}
onChange={(isValid, changedValue, name) =>
this.onChange(isValid, changedValue, name)
}
hasUpdatePermission={configUpdatePermission}
/>
<hr />
2018-08-13 16:33:56 +02:00
<BaseUrlSettings
baseUrl={config.baseUrl}
forceBaseUrl={config.forceBaseUrl}
onChange={(isValid, changedValue, name) =>
this.onChange(isValid, changedValue, name)
}
hasUpdatePermission={configUpdatePermission}
2018-08-13 16:33:56 +02:00
/>
<hr />
<AdminSettings
adminGroups={config.adminGroups}
adminUsers={config.adminUsers}
onChange={(isValid, changedValue, name) =>
this.onChange(isValid, changedValue, name)
}
hasUpdatePermission={configUpdatePermission}
/>
<hr />
2018-08-13 16:33:56 +02:00
<ProxySettings
proxyPassword={config.proxyPassword ? config.proxyPassword : ""}
proxyPort={config.proxyPort}
proxyServer={config.proxyServer ? config.proxyServer : ""}
proxyUser={config.proxyUser ? config.proxyUser : ""}
enableProxy={config.enableProxy}
2018-08-16 11:18:28 +02:00
proxyExcludes={config.proxyExcludes}
2018-08-13 16:33:56 +02:00
onChange={(isValid, changedValue, name) =>
this.onChange(isValid, changedValue, name)
}
hasUpdatePermission={configUpdatePermission}
2018-08-13 16:33:56 +02:00
/>
<hr />
2018-08-13 16:33:56 +02:00
<SubmitButton
loading={loading}
label={t("config-form.submit")}
disabled={
!configUpdatePermission || this.hasError() || !this.state.changed
}
2018-08-13 16:33:56 +02:00
/>
</form>
);
}
onChange = (isValid: boolean, changedValue: any, name: string) => {
2018-08-16 16:41:17 +02:00
this.setState({
...this.state,
config: {
...this.state.config,
[name]: changedValue
2018-08-21 08:40:49 +02:00
},
error: {
...this.state.error,
[name]: !isValid
},
changed: true
2018-08-16 16:41:17 +02:00
});
};
2018-08-21 08:40:49 +02:00
hasError = () => {
return (
this.state.error.loginAttemptLimit ||
this.state.error.loginAttemptLimitTimeout
);
2018-08-13 16:33:56 +02:00
};
onClose = () => {
this.setState({
...this.state,
showNotification: false
});
};
2018-08-13 16:33:56 +02:00
}
export default translate("config")(ConfigForm);