2019-08-20 15:50:11 +02:00
|
|
|
//@flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { translate } from "react-i18next";
|
2019-10-09 16:17:02 +02:00
|
|
|
import classNames from "classnames";
|
|
|
|
|
import styled from "styled-components";
|
2019-08-20 15:50:11 +02:00
|
|
|
import type { Plugin } from "@scm-manager/ui-types";
|
|
|
|
|
import {
|
2019-08-21 09:59:28 +02:00
|
|
|
apiClient,
|
2019-08-20 15:50:11 +02:00
|
|
|
Button,
|
|
|
|
|
ButtonGroup,
|
2019-08-21 11:22:49 +02:00
|
|
|
Checkbox,
|
|
|
|
|
ErrorNotification,
|
2019-08-20 15:50:11 +02:00
|
|
|
Modal,
|
2019-08-21 11:22:49 +02:00
|
|
|
Notification
|
2019-08-20 15:50:11 +02:00
|
|
|
} from "@scm-manager/ui-components";
|
2019-08-22 08:24:01 +02:00
|
|
|
import waitForRestart from "./waitForRestart";
|
2019-09-16 14:31:55 +02:00
|
|
|
import SuccessNotification from "./SuccessNotification";
|
2019-09-18 09:30:32 +02:00
|
|
|
import { PluginAction } from "./PluginEntry";
|
2019-08-20 15:50:11 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
plugin: Plugin,
|
2019-09-16 17:55:07 +02:00
|
|
|
pluginAction: string,
|
2019-08-21 15:07:56 +02:00
|
|
|
refresh: () => void,
|
2019-08-20 15:50:11 +02:00
|
|
|
onClose: () => void,
|
|
|
|
|
|
|
|
|
|
// context props
|
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 15:50:11 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-09 16:17:02 +02:00
|
|
|
const ListParent = styled.div`
|
|
|
|
|
margin-right: 0;
|
|
|
|
|
min-width: ${props =>
|
|
|
|
|
props.pluginAction === PluginAction.INSTALL ? "5.5em" : "10em"};
|
|
|
|
|
text-align: left;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const ListChild = styled.div`
|
|
|
|
|
flex-grow: 4;
|
|
|
|
|
`;
|
2019-08-20 17:19:57 +02:00
|
|
|
|
2019-09-16 17:55:07 +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-09-16 17:55:07 +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
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-16 17:55:07 +02:00
|
|
|
createPluginActionLink = () => {
|
|
|
|
|
const { plugin, pluginAction } = this.props;
|
2019-08-21 11:22:49 +02:00
|
|
|
const { restart } = this.state;
|
2019-09-16 17:55:07 +02:00
|
|
|
|
|
|
|
|
let pluginActionLink = "";
|
|
|
|
|
|
2019-09-18 09:30:32 +02:00
|
|
|
if (pluginAction === PluginAction.INSTALL) {
|
2019-09-16 17:55:07 +02:00
|
|
|
pluginActionLink = plugin._links.install.href;
|
2019-09-18 09:30:32 +02:00
|
|
|
} else if (pluginAction === PluginAction.UPDATE) {
|
2019-09-16 17:55:07 +02:00
|
|
|
pluginActionLink = plugin._links.update.href;
|
2019-09-18 09:30:32 +02:00
|
|
|
} else if (pluginAction === PluginAction.UNINSTALL) {
|
2019-09-17 16:25:24 +02:00
|
|
|
pluginActionLink = plugin._links.uninstall.href;
|
2019-09-16 17:55:07 +02:00
|
|
|
}
|
|
|
|
|
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
|
2019-09-16 17:55:07 +02:00
|
|
|
.post(this.createPluginActionLink())
|
|
|
|
|
.then(this.onSuccess)
|
2019-08-21 09:59:28 +02:00
|
|
|
.catch(error => {
|
|
|
|
|
this.setState({
|
|
|
|
|
loading: false,
|
|
|
|
|
error: error
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
footer = () => {
|
2019-09-16 17:55:07 +02:00
|
|
|
const { pluginAction, onClose, t } = this.props;
|
2019-08-21 11:22:49 +02:00
|
|
|
const { loading, error, restart, success } = this.state;
|
|
|
|
|
|
2019-09-18 16:15:29 +02:00
|
|
|
let color = pluginAction === PluginAction.UNINSTALL ? "warning" : "primary";
|
2019-09-16 17:55:07 +02:00
|
|
|
let label = `plugins.modal.${pluginAction}`;
|
2019-08-21 11:22:49 +02:00
|
|
|
if (restart) {
|
|
|
|
|
color = "warning";
|
2019-09-16 17:55:07 +02:00
|
|
|
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}
|
2019-09-16 17:55:07 +02:00
|
|
|
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
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-20 15:50:11 +02:00
|
|
|
renderDependencies() {
|
2019-10-09 16:17:02 +02:00
|
|
|
const { plugin, t } = this.props;
|
2019-08-20 15:50:11 +02:00
|
|
|
|
|
|
|
|
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>
|
2019-10-09 16:17:02 +02:00
|
|
|
<ul>
|
2019-08-21 11:22:49 +02:00
|
|
|
{plugin.dependencies.map((dependency, index) => {
|
|
|
|
|
return <li key={index}>{dependency}</li>;
|
|
|
|
|
})}
|
|
|
|
|
</ul>
|
|
|
|
|
</Notification>
|
|
|
|
|
</div>
|
2019-08-20 15:50:11 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
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-09-16 14:31:55 +02:00
|
|
|
<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;
|
2019-10-09 16:17:02 +02:00
|
|
|
const { plugin, pluginAction, onClose, t } = this.props;
|
2019-08-20 15:50:11 +02:00
|
|
|
|
|
|
|
|
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">
|
2019-10-09 16:17:02 +02:00
|
|
|
<ListParent
|
|
|
|
|
className={classNames("field-label", "is-inline-flex")}
|
|
|
|
|
pluginAction={pluginAction}
|
2019-08-20 17:19:57 +02:00
|
|
|
>
|
|
|
|
|
{t("plugins.modal.author")}:
|
2019-10-09 16:17:02 +02:00
|
|
|
</ListParent>
|
|
|
|
|
<ListChild className={classNames("field-body", "is-inline-flex")}>
|
2019-08-20 17:19:57 +02:00
|
|
|
{plugin.author}
|
2019-10-09 16:17:02 +02:00
|
|
|
</ListChild>
|
2019-08-20 17:19:57 +02:00
|
|
|
</div>
|
2019-09-18 09:30:32 +02:00
|
|
|
{pluginAction === PluginAction.INSTALL && (
|
2019-09-16 17:55:07 +02:00
|
|
|
<div className="field is-horizontal">
|
2019-10-09 16:17:02 +02:00
|
|
|
<ListParent
|
|
|
|
|
className={classNames("field-label", "is-inline-flex")}
|
|
|
|
|
pluginAction={pluginAction}
|
2019-09-16 17:55:07 +02:00
|
|
|
>
|
|
|
|
|
{t("plugins.modal.version")}:
|
2019-10-09 16:17:02 +02:00
|
|
|
</ListParent>
|
|
|
|
|
<ListChild
|
|
|
|
|
className={classNames("field-body", "is-inline-flex")}
|
2019-09-16 17:55:07 +02:00
|
|
|
>
|
|
|
|
|
{plugin.version}
|
2019-10-09 16:17:02 +02:00
|
|
|
</ListChild>
|
2019-08-20 17:19:57 +02:00
|
|
|
</div>
|
2019-09-16 17:55:07 +02:00
|
|
|
)}
|
2019-09-18 09:30:32 +02:00
|
|
|
{(pluginAction === PluginAction.UPDATE ||
|
|
|
|
|
pluginAction === PluginAction.UNINSTALL) && (
|
2019-09-17 16:25:24 +02:00
|
|
|
<div className="field is-horizontal">
|
2019-10-09 16:17:02 +02:00
|
|
|
<ListParent
|
|
|
|
|
className={classNames("field-label", "is-inline-flex")}
|
2019-09-17 16:25:24 +02:00
|
|
|
>
|
|
|
|
|
{t("plugins.modal.currentVersion")}:
|
2019-10-09 16:17:02 +02:00
|
|
|
</ListParent>
|
|
|
|
|
<ListChild
|
|
|
|
|
className={classNames("field-body", "is-inline-flex")}
|
2019-09-17 16:25:24 +02:00
|
|
|
>
|
|
|
|
|
{plugin.version}
|
2019-10-09 16:17:02 +02:00
|
|
|
</ListChild>
|
2019-09-17 16:25:24 +02:00
|
|
|
</div>
|
|
|
|
|
)}
|
2019-09-18 09:30:32 +02:00
|
|
|
{pluginAction === PluginAction.UPDATE && (
|
2019-09-17 16:25:24 +02:00
|
|
|
<div className="field is-horizontal">
|
2019-10-09 16:17:02 +02:00
|
|
|
<ListParent
|
|
|
|
|
className={classNames("field-label", "is-inline-flex")}
|
2019-09-17 16:25:24 +02:00
|
|
|
>
|
|
|
|
|
{t("plugins.modal.newVersion")}:
|
2019-10-09 16:17:02 +02:00
|
|
|
</ListParent>
|
|
|
|
|
<ListChild
|
|
|
|
|
className={classNames("field-body", "is-inline-flex")}
|
2019-09-17 16:25:24 +02:00
|
|
|
>
|
|
|
|
|
{plugin.newVersion}
|
2019-10-09 16:17:02 +02:00
|
|
|
</ListChild>
|
2019-09-17 16:25:24 +02:00
|
|
|
</div>
|
2019-09-16 17:55:07 +02:00
|
|
|
)}
|
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()}
|
2019-08-20 15:50:11 +02:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Modal
|
2019-09-16 17:55:07 +02:00
|
|
|
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
|
|
|
})}
|
2019-08-20 15:50:11 +02:00
|
|
|
closeFunction={() => onClose()}
|
|
|
|
|
body={body}
|
2019-08-21 09:59:28 +02:00
|
|
|
footer={this.footer()}
|
2019-08-20 15:50:11 +02:00
|
|
|
active={true}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 16:17:02 +02:00
|
|
|
export default translate("admin")(PluginModal);
|