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

338 lines
8.5 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 SuccessNotification from "./SuccessNotification";
type Props = {
plugin: Plugin,
pluginAction: string,
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
},
userLabelMarginSmall: {
2019-08-20 17:19:57 +02:00
minWidth: "5.5em"
},
userLabelMarginLarge: {
minWidth: "9em"
},
2019-08-20 17:19:57 +02:00
userFieldFlex: {
flexGrow: 4
}
};
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
};
}
onSuccess = () => {
2019-08-21 11:22:49 +02:00
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
}
};
createPluginActionLink = () => {
const { plugin, pluginAction } = this.props;
2019-08-21 11:22:49 +02:00
const { restart } = this.state;
let pluginActionLink = "";
if (pluginAction === "install") {
pluginActionLink = plugin._links.install.href;
} else if (pluginAction === "update") {
pluginActionLink = plugin._links.update.href;
} else if (pluginAction === "uninstall") {
pluginActionLink = plugin._links.uninstall.href;
}
return pluginActionLink + "?restart=" + restart.toString();
};
handlePluginAction = (e: Event) => {
2019-08-21 09:59:28 +02:00
this.setState({
loading: true
});
e.preventDefault();
2019-08-21 11:22:49 +02:00
apiClient
.post(this.createPluginActionLink())
.then(this.onSuccess)
2019-08-21 09:59:28 +02:00
.catch(error => {
this.setState({
loading: false,
error: error
});
});
};
footer = () => {
const { pluginAction, 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.${pluginAction}`;
2019-08-21 11:22:49 +02:00
if (restart) {
color = "warning";
label = `plugins.modal.${pluginAction}AndRestart`;
2019-08-21 11:22:49 +02:00
}
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.handlePluginAction}
2019-08-21 11:22:49 +02:00
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">
<SuccessNotification />
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;
const { plugin, pluginAction, 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,
pluginAction === "install"
? classes.userLabelMarginSmall
: classes.userLabelMarginLarge,
2019-08-20 17:19:57 +02:00
"field-label is-inline-flex"
)}
>
{t("plugins.modal.author")}:
</div>
<div
className={classNames(
classes.userFieldFlex,
"field-body is-inline-flex"
)}
>
{plugin.author}
</div>
</div>
{pluginAction === "install" && (
<div className="field is-horizontal">
<div
className={classNames(
classes.userLabelAlignment,
classes.userLabelMarginSmall,
"field-label is-inline-flex"
)}
>
{t("plugins.modal.version")}:
</div>
<div
className={classNames(
classes.userFieldFlex,
"field-body is-inline-flex"
)}
>
{plugin.version}
</div>
2019-08-20 17:19:57 +02:00
</div>
)}
{(pluginAction === "update" || pluginAction === "uninstall") && (
<div className="field is-horizontal">
<div
className={classNames(
classes.userLabelAlignment,
classes.userLabelMarginLarge,
"field-label is-inline-flex"
)}
>
{t("plugins.modal.currentVersion")}:
</div>
<div
className={classNames(
classes.userFieldFlex,
"field-body is-inline-flex"
)}
>
{plugin.version}
</div>
</div>
)}
{pluginAction === "update" && (
<div className="field is-horizontal">
<div
className={classNames(
classes.userLabelAlignment,
classes.userLabelMarginLarge,
"field-label is-inline-flex"
)}
>
{t("plugins.modal.newVersion")}:
</div>
<div
className={classNames(
classes.userFieldFlex,
"field-body is-inline-flex"
)}
>
{plugin.newVersion}
</div>
</div>
)}
2019-08-20 17:19:57 +02:00
{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
title={t(`plugins.modal.title.${pluginAction}`, {
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);