From e2a37d67a52eca9b50d5aaf269e4f2fecea5f047 Mon Sep 17 00:00:00 2001 From: Eduard Heimbuch Date: Mon, 16 Sep 2019 17:55:07 +0200 Subject: [PATCH] create abstract pluginModal dialog for all plugin actions to avoid duplicate code --- .../admin/plugins/components/PluginEntry.js | 15 +- .../{InstallPluginModal.js => PluginModal.js} | 128 ++++++-- .../plugins/components/UpdatePluginModal.js | 288 ------------------ 3 files changed, 108 insertions(+), 323 deletions(-) rename scm-ui/src/admin/plugins/components/{InstallPluginModal.js => PluginModal.js} (61%) delete mode 100644 scm-ui/src/admin/plugins/components/UpdatePluginModal.js diff --git a/scm-ui/src/admin/plugins/components/PluginEntry.js b/scm-ui/src/admin/plugins/components/PluginEntry.js index 2c04f098e5..0d17af0475 100644 --- a/scm-ui/src/admin/plugins/components/PluginEntry.js +++ b/scm-ui/src/admin/plugins/components/PluginEntry.js @@ -5,8 +5,13 @@ import type { Plugin } from "@scm-manager/ui-types"; import { CardColumn } from "@scm-manager/ui-components"; import PluginAvatar from "./PluginAvatar"; import classNames from "classnames"; -import InstallPluginModal from "./InstallPluginModal"; -import UpdatePluginModal from "./UpdatePluginModal"; +import PluginModal from "./PluginModal"; + + +const PluginAction = { + INSTALL: "install", + UPDATE: "update" +}; type Props = { plugin: Plugin, @@ -103,16 +108,18 @@ class PluginEntry extends React.Component { const { plugin, refresh } = this.props; if (this.isInstallable()) { return ( - ); } else if (this.isUpdatable()) { return ( - diff --git a/scm-ui/src/admin/plugins/components/InstallPluginModal.js b/scm-ui/src/admin/plugins/components/PluginModal.js similarity index 61% rename from scm-ui/src/admin/plugins/components/InstallPluginModal.js rename to scm-ui/src/admin/plugins/components/PluginModal.js index 48f7058e66..024a9cf32e 100644 --- a/scm-ui/src/admin/plugins/components/InstallPluginModal.js +++ b/scm-ui/src/admin/plugins/components/PluginModal.js @@ -19,6 +19,7 @@ import SuccessNotification from "./SuccessNotification"; type Props = { plugin: Plugin, + pluginAction: string, refresh: () => void, onClose: () => void, @@ -37,15 +38,20 @@ type State = { const styles = { userLabelAlignment: { textAlign: "left", - marginRight: 0, + marginRight: 0 + }, + userLabelMarginSmall: { minWidth: "5.5em" }, + userLabelMarginLarge: { + minWidth: "9em" + }, userFieldFlex: { flexGrow: 4 } }; -class InstallPluginModal extends React.Component { +class PluginModal extends React.Component { constructor(props: Props) { super(props); this.state = { @@ -55,7 +61,7 @@ class InstallPluginModal extends React.Component { }; } - onInstallSuccess = () => { + onSuccess = () => { const { restart } = this.state; const { refresh, onClose } = this.props; @@ -87,16 +93,28 @@ class InstallPluginModal extends React.Component { } }; - install = (e: Event) => { + createPluginActionLink = () => { + const { plugin, pluginAction } = this.props; const { restart } = this.state; - const { plugin } = this.props; + + let pluginActionLink = ""; + + if (pluginAction === "install") { + pluginActionLink = plugin._links.install.href; + } else if (pluginAction === "update") { + pluginActionLink = plugin._links.update.href; + } + return pluginActionLink + "?restart=" + restart.toString(); + }; + + handlePluginAction = (e: Event) => { this.setState({ loading: true }); e.preventDefault(); apiClient - .post(plugin._links.install.href + "?restart=" + restart.toString()) - .then(this.onInstallSuccess) + .post(this.createPluginActionLink()) + .then(this.onSuccess) .catch(error => { this.setState({ loading: false, @@ -106,21 +124,21 @@ class InstallPluginModal extends React.Component { }; footer = () => { - const { onClose, t } = this.props; + const { pluginAction, onClose, t } = this.props; const { loading, error, restart, success } = this.state; let color = "primary"; - let label = "plugins.modal.install"; + let label = `plugins.modal.${pluginAction}`; if (restart) { color = "warning"; - label = "plugins.modal.installAndRestart"; + label = `plugins.modal.${pluginAction}AndRestart`; } return (