2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2019-11-26 15:22:22 +01:00
|
|
|
import React, { FormEvent } from "react";
|
2019-10-23 15:47:08 +02:00
|
|
|
import { WithTranslation, withTranslation } from "react-i18next";
|
2019-10-20 16:59:02 +02:00
|
|
|
import { Links, Link } from "@scm-manager/ui-types";
|
2019-11-26 15:22:22 +01:00
|
|
|
import { apiClient, Level, SubmitButton, Loading, ErrorNotification } from "../";
|
2018-11-13 13:09:59 +01:00
|
|
|
|
|
|
|
|
type RenderProps = {
|
2019-10-19 16:38:07 +02:00
|
|
|
readOnly: boolean;
|
|
|
|
|
initialConfiguration: ConfigurationType;
|
|
|
|
|
onConfigurationChange: (p1: ConfigurationType, p2: boolean) => void;
|
2018-11-13 13:09:59 +01:00
|
|
|
};
|
|
|
|
|
|
2019-10-23 15:47:08 +02:00
|
|
|
type Props = WithTranslation & {
|
2019-10-19 16:38:07 +02:00
|
|
|
link: string;
|
|
|
|
|
render: (props: RenderProps) => any; // ???
|
2018-11-13 13:09:59 +01:00
|
|
|
};
|
|
|
|
|
|
2019-01-02 14:06:09 +01:00
|
|
|
type ConfigurationType = {
|
2019-10-19 16:38:07 +02:00
|
|
|
_links: Links;
|
|
|
|
|
} & object;
|
2018-11-13 13:09:59 +01:00
|
|
|
|
|
|
|
|
type State = {
|
2019-10-19 16:38:07 +02:00
|
|
|
error?: Error;
|
|
|
|
|
fetching: boolean;
|
|
|
|
|
modifying: boolean;
|
2019-10-20 16:59:02 +02:00
|
|
|
contentType?: string | null;
|
2019-10-19 16:38:07 +02:00
|
|
|
configChanged: boolean;
|
|
|
|
|
|
|
|
|
|
configuration?: ConfigurationType;
|
|
|
|
|
modifiedConfiguration?: ConfigurationType;
|
|
|
|
|
valid: boolean;
|
2018-11-13 13:09:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GlobalConfiguration uses the render prop pattern to encapsulate the logic for
|
|
|
|
|
* synchronizing the configuration with the backend.
|
|
|
|
|
*/
|
|
|
|
|
class Configuration extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
fetching: true,
|
|
|
|
|
modifying: false,
|
2019-01-02 14:06:09 +01:00
|
|
|
configChanged: false,
|
2019-10-20 16:59:02 +02:00
|
|
|
valid: false
|
2018-11-13 13:09:59 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
const { link } = this.props;
|
|
|
|
|
|
2019-01-02 14:06:09 +01:00
|
|
|
apiClient
|
|
|
|
|
.get(link)
|
2018-11-13 13:09:59 +01:00
|
|
|
.then(this.captureContentType)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(this.loadConfig)
|
|
|
|
|
.catch(this.handleError);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
captureContentType = (response: Response) => {
|
2019-10-20 16:59:02 +02:00
|
|
|
const contentType = response.headers.get("Content-Type");
|
2018-11-13 13:09:59 +01:00
|
|
|
this.setState({
|
2019-10-20 16:59:02 +02:00
|
|
|
contentType
|
2018-11-13 13:09:59 +01:00
|
|
|
});
|
|
|
|
|
return response;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getContentType = (): string => {
|
|
|
|
|
const { contentType } = this.state;
|
2019-10-20 16:59:02 +02:00
|
|
|
return contentType ? contentType : "application/json";
|
2018-11-13 13:09:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleError = (error: Error) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
error,
|
|
|
|
|
fetching: false,
|
2019-10-20 16:59:02 +02:00
|
|
|
modifying: false
|
2018-11-13 13:09:59 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
loadConfig = (configuration: ConfigurationType) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
configuration,
|
|
|
|
|
fetching: false,
|
2019-10-20 16:59:02 +02:00
|
|
|
error: undefined
|
2018-11-13 13:09:59 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
getModificationUrl = (): string | undefined => {
|
2018-11-13 13:09:59 +01:00
|
|
|
const { configuration } = this.state;
|
|
|
|
|
if (configuration) {
|
|
|
|
|
const links = configuration._links;
|
|
|
|
|
if (links && links.update) {
|
2019-10-20 16:59:02 +02:00
|
|
|
const link = links.update as Link;
|
|
|
|
|
return link.href;
|
2018-11-13 13:09:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
isReadOnly = (): boolean => {
|
|
|
|
|
const modificationUrl = this.getModificationUrl();
|
|
|
|
|
return !modificationUrl;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
configurationChanged = (configuration: ConfigurationType, valid: boolean) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
modifiedConfiguration: configuration,
|
2019-10-20 16:59:02 +02:00
|
|
|
valid
|
2018-11-13 13:09:59 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
modifyConfiguration = (event: FormEvent) => {
|
2018-11-13 13:09:59 +01:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
this.setState({
|
2021-05-03 19:04:08 +02:00
|
|
|
modifying: true,
|
|
|
|
|
error: undefined
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
2018-11-13 13:09:59 +01:00
|
|
|
|
2019-01-02 14:06:09 +01:00
|
|
|
const { modifiedConfiguration } = this.state;
|
2018-11-13 13:09:59 +01:00
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
const modificationUrl = this.getModificationUrl();
|
|
|
|
|
if (modificationUrl) {
|
2019-10-21 10:57:56 +02:00
|
|
|
apiClient
|
|
|
|
|
.put(modificationUrl, modifiedConfiguration, this.getContentType())
|
|
|
|
|
.then(() =>
|
|
|
|
|
this.setState({
|
|
|
|
|
modifying: false,
|
|
|
|
|
configChanged: true,
|
|
|
|
|
valid: false
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.catch(this.handleError);
|
2019-10-20 16:59:02 +02:00
|
|
|
} else {
|
|
|
|
|
this.setState({
|
|
|
|
|
error: new Error("no modification link available")
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-11-13 13:09:59 +01:00
|
|
|
};
|
|
|
|
|
|
2019-01-02 14:06:09 +01:00
|
|
|
renderConfigChangedNotification = () => {
|
|
|
|
|
if (this.state.configChanged) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="notification is-primary">
|
|
|
|
|
<button
|
|
|
|
|
className="delete"
|
2019-10-19 16:38:07 +02:00
|
|
|
onClick={() =>
|
|
|
|
|
this.setState({
|
2019-10-20 16:59:02 +02:00
|
|
|
configChanged: false
|
2019-10-19 16:38:07 +02:00
|
|
|
})
|
|
|
|
|
}
|
2019-01-02 14:06:09 +01:00
|
|
|
/>
|
2019-10-20 16:59:02 +02:00
|
|
|
{this.props.t("config.form.submit-success-notification")}
|
2019-01-02 14:06:09 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-13 13:09:59 +01:00
|
|
|
render() {
|
|
|
|
|
const { t } = this.props;
|
|
|
|
|
const { fetching, error, configuration, modifying, valid } = this.state;
|
|
|
|
|
|
2021-05-03 19:04:08 +02:00
|
|
|
if (fetching || !configuration) {
|
2018-11-13 13:09:59 +01:00
|
|
|
return <Loading />;
|
|
|
|
|
} else {
|
|
|
|
|
const readOnly = this.isReadOnly();
|
|
|
|
|
|
|
|
|
|
const renderProps: RenderProps = {
|
|
|
|
|
readOnly,
|
|
|
|
|
initialConfiguration: configuration,
|
2019-10-20 16:59:02 +02:00
|
|
|
onConfigurationChange: this.configurationChanged
|
2018-11-13 13:09:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2019-01-02 14:06:09 +01:00
|
|
|
<>
|
|
|
|
|
{this.renderConfigChangedNotification()}
|
|
|
|
|
<form onSubmit={this.modifyConfiguration}>
|
|
|
|
|
{this.props.render(renderProps)}
|
2021-05-03 19:04:08 +02:00
|
|
|
{error && (
|
|
|
|
|
<>
|
|
|
|
|
<hr />
|
|
|
|
|
<ErrorNotification error={error} />
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2019-01-02 14:06:09 +01:00
|
|
|
<hr />
|
2019-11-26 15:22:22 +01:00
|
|
|
<Level
|
|
|
|
|
right={<SubmitButton label={t("config.form.submit")} disabled={!valid || readOnly} loading={modifying} />}
|
|
|
|
|
/>
|
2019-01-02 14:06:09 +01:00
|
|
|
</form>
|
|
|
|
|
</>
|
2018-11-13 13:09:59 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-23 15:47:08 +02:00
|
|
|
export default withTranslation("config")(Configuration);
|