Files
SCM-Manager/scm-ui/src/admin/plugins/components/PluginModal.js

271 lines
6.2 KiB
JavaScript
Raw Normal View History

//@flow
import React from "react";
2019-08-20 17:19:57 +02:00
import { compose } from "redux";
import { translate } from "react-i18next";
2019-08-20 17:19:57 +02:00
import injectSheet from "react-jss";
import type { Plugin } from "@scm-manager/ui-types";
import {
2019-08-21 09:59:28 +02:00
apiClient,
Button,
ButtonGroup,
2019-08-21 11:22:49 +02:00
Checkbox,
ErrorNotification,
Modal,
2019-08-21 11:22:49 +02:00
Notification
} from "@scm-manager/ui-components";
2019-08-20 17:19:57 +02:00
import classNames from "classnames";
2019-08-22 08:24:01 +02:00
import waitForRestart from "./waitForRestart";
import InstallSuccessNotification from "./InstallSuccessNotification";
type Props = {
plugin: Plugin,
2019-08-21 15:07:56 +02:00
refresh: () => void,
onClose: () => void,
// context props
2019-08-20 17:19:57 +02:00
classes: any,
2019-08-21 09:59:28 +02:00
t: (key: string, params?: Object) => string
};
type State = {
2019-08-21 11:22:49 +02:00
success: boolean,
restart: boolean,
2019-08-21 09:59:28 +02:00
loading: boolean,
error?: Error
};
2019-08-20 17:19:57 +02:00
const styles = {
userLabelAlignment: {
textAlign: "left",
marginRight: 0,
minWidth: "5.5em"
},
userFieldFlex: {
flexGrow: 4
}
};
2019-08-21 11:22:49 +02:00
class PluginModal extends React.Component<Props, State> {
2019-08-21 09:59:28 +02:00
constructor(props: Props) {
super(props);
this.state = {
2019-08-21 11:22:49 +02:00
loading: false,
restart: false,
success: false
2019-08-21 09:59:28 +02:00
};
}
2019-08-21 11:22:49 +02:00
onInstallSuccess = () => {
const { restart } = this.state;
2019-08-21 15:07:56 +02:00
const { refresh, onClose } = this.props;
2019-08-21 11:22:49 +02:00
const newState = {
loading: false,
error: undefined
};
if (restart) {
2019-08-22 08:24:01 +02:00
waitForRestart()
.then(() => {
this.setState({
...newState,
success: true
});
})
.catch(error => {
this.setState({
loading: false,
success: false,
error
});
});
2019-08-21 11:22:49 +02:00
} else {
2019-08-21 15:07:56 +02:00
this.setState(newState, () => {
refresh();
onClose();
});
2019-08-21 11:22:49 +02:00
}
};
2019-08-21 09:59:28 +02:00
install = (e: Event) => {
2019-08-21 11:22:49 +02:00
const { restart } = this.state;
const { plugin } = this.props;
2019-08-21 09:59:28 +02:00
this.setState({
loading: true
});
e.preventDefault();
2019-08-21 11:22:49 +02:00
apiClient
.post(plugin._links.install.href + "?restart=" + restart.toString())
.then(this.onInstallSuccess)
2019-08-21 09:59:28 +02:00
.catch(error => {
this.setState({
loading: false,
error: error
});
});
};
footer = () => {
const { onClose, t } = this.props;
2019-08-21 11:22:49 +02:00
const { loading, error, restart, success } = this.state;
let color = "primary";
let label = "plugins.modal.install";
if (restart) {
color = "warning";
label = "plugins.modal.installAndRestart";
}
2019-08-21 09:59:28 +02:00
return (
2019-08-21 11:22:49 +02:00
<ButtonGroup>
<Button
label={t(label)}
color={color}
action={this.install}
loading={loading}
disabled={!!error || success}
/>
<Button label={t("plugins.modal.abort")} action={onClose} />
</ButtonGroup>
2019-08-21 09:59:28 +02:00
);
};
renderDependencies() {
2019-08-20 17:19:57 +02:00
const { plugin, classes, t } = this.props;
let dependencies = null;
if (plugin.dependencies && plugin.dependencies.length > 0) {
dependencies = (
2019-08-21 11:22:49 +02:00
<div className="media">
<Notification type="warning">
<strong>{t("plugins.modal.dependencyNotification")}</strong>
<ul className={classes.listSpacing}>
{plugin.dependencies.map((dependency, index) => {
return <li key={index}>{dependency}</li>;
})}
</ul>
</Notification>
</div>
);
}
return dependencies;
}
2019-08-21 11:22:49 +02:00
renderNotifications = () => {
const { t } = this.props;
const { restart, error, success } = this.state;
2019-08-21 09:59:28 +02:00
if (error) {
return (
2019-08-21 11:22:49 +02:00
<div className="media">
2019-08-21 09:59:28 +02:00
<ErrorNotification error={error} />
</div>
);
2019-08-21 11:22:49 +02:00
} else if (success) {
return (
<div className="media">
2019-08-22 08:24:01 +02:00
<InstallSuccessNotification />
2019-08-21 11:22:49 +02:00
</div>
);
} else if (restart) {
return (
<div className="media">
<Notification type="warning">
{t("plugins.modal.restartNotification")}
</Notification>
</div>
);
2019-08-21 09:59:28 +02:00
}
return null;
};
2019-08-21 11:22:49 +02:00
handleRestartChange = (value: boolean) => {
this.setState({
restart: value
});
};
2019-08-21 09:59:28 +02:00
2019-08-21 11:22:49 +02:00
render() {
const { restart } = this.state;
2019-08-21 09:59:28 +02:00
const { plugin, onClose, classes, t } = this.props;
const body = (
<>
2019-08-20 17:19:57 +02:00
<div className="media">
<div className="media-content">
2019-08-21 09:59:28 +02:00
<p>{plugin.description}</p>
2019-08-20 17:19:57 +02:00
</div>
</div>
<div className="media">
<div className="media-content">
<div className="field is-horizontal">
<div
className={classNames(
classes.userLabelAlignment,
"field-label is-inline-flex"
)}
>
{t("plugins.modal.author")}:
</div>
<div
className={classNames(
classes.userFieldFlex,
"field-body is-inline-flex"
)}
>
{plugin.author}
</div>
</div>
<div className="field is-horizontal">
<div
className={classNames(
classes.userLabelAlignment,
"field-label is-inline-flex"
)}
>
{t("plugins.modal.version")}:
</div>
<div
className={classNames(
classes.userFieldFlex,
"field-body is-inline-flex"
)}
>
{plugin.version}
</div>
</div>
{this.renderDependencies()}
</div>
</div>
<div className="media">
<div className="media-content">
<Checkbox
2019-08-21 11:22:49 +02:00
checked={restart}
2019-08-20 17:19:57 +02:00
label={t("plugins.modal.restart")}
2019-08-21 11:22:49 +02:00
onChange={this.handleRestartChange}
disabled={false}
2019-08-20 17:19:57 +02:00
/>
</div>
</div>
2019-08-21 11:22:49 +02:00
{this.renderNotifications()}
</>
);
return (
<Modal
2019-08-20 17:19:57 +02:00
title={t("plugins.modal.title", {
2019-08-21 09:59:28 +02:00
name: plugin.displayName ? plugin.displayName : plugin.name
2019-08-20 17:19:57 +02:00
})}
closeFunction={() => onClose()}
body={body}
2019-08-21 09:59:28 +02:00
footer={this.footer()}
active={true}
/>
);
}
}
2019-08-20 17:19:57 +02:00
export default compose(
injectSheet(styles),
translate("admin")
)(PluginModal);