import * as React from "react"; import { WithTranslation, withTranslation } from "react-i18next"; import { PendingPlugins, PluginCollection } from "@scm-manager/ui-types"; import { Button, ButtonGroup, ErrorNotification, Modal } from "@scm-manager/ui-components"; import SuccessNotification from "./SuccessNotification"; type Props = WithTranslation & { onClose: () => void; actionType: string; pendingPlugins?: PendingPlugins; installedPlugins?: PluginCollection; refresh: () => void; execute: () => Promise; description: string; label: string; children?: React.Node; }; type State = { loading: boolean; success: boolean; error?: Error; }; class PluginActionModal extends React.Component { constructor(props: Props) { super(props); this.state = { loading: false, success: false }; } renderNotifications = () => { const { children } = this.props; const { error, success } = this.state; if (error) { return ; } else if (success) { return ; } else { return children; } }; executeAction = () => { this.setState({ loading: true }); this.props .execute() .then(() => { this.setState({ success: true, loading: false }); }) .catch(error => { this.setState({ success: false, loading: false, error: error }); }); }; renderModalContent = () => { return ( <> {this.renderUpdatable()} {this.renderInstallQueue()} {this.renderUpdateQueue()} {this.renderUninstallQueue()} ); }; renderUpdatable = () => { const { installedPlugins, t } = this.props; return ( <> {installedPlugins && installedPlugins._embedded && installedPlugins._embedded.plugins && ( <> {t("plugins.modal.updateQueue")}
    {installedPlugins._embedded.plugins .filter(plugin => plugin._links && plugin._links.update) .map(plugin => (
  • {plugin.name}
  • ))}
)} ); }; renderInstallQueue = () => { const { pendingPlugins, t } = this.props; return ( <> {pendingPlugins && pendingPlugins._embedded && pendingPlugins._embedded.new.length > 0 && ( <> {t("plugins.modal.installQueue")}
    {pendingPlugins._embedded.new.map(plugin => (
  • {plugin.name}
  • ))}
)} ); }; renderUpdateQueue = () => { const { pendingPlugins, t } = this.props; return ( <> {pendingPlugins && pendingPlugins._embedded && pendingPlugins._embedded.update.length > 0 && ( <> {t("plugins.modal.updateQueue")}
    {pendingPlugins._embedded.update.map(plugin => (
  • {plugin.name}
  • ))}
)} ); }; renderUninstallQueue = () => { const { pendingPlugins, t } = this.props; return ( <> {pendingPlugins && pendingPlugins._embedded && pendingPlugins._embedded.uninstall.length > 0 && ( <> {t("plugins.modal.uninstallQueue")}
    {pendingPlugins._embedded.uninstall.map(plugin => (
  • {plugin.name}
  • ))}
)} ); }; renderBody = () => { return ( <>

{this.props.description}

{this.renderModalContent()}
{this.renderNotifications()}
); }; renderFooter = () => { const { onClose, t } = this.props; const { loading, error, success } = this.state; return (