import React from 'react'; import { translate } from 'react-i18next'; import classNames from 'classnames'; import styled from 'styled-components'; import { Plugin } from '@scm-manager/ui-types'; import { apiClient, Button, ButtonGroup, Checkbox, ErrorNotification, Modal, Notification, } from '@scm-manager/ui-components'; import waitForRestart from './waitForRestart'; import SuccessNotification from './SuccessNotification'; import { PluginAction } from './PluginEntry'; type Props = { plugin: Plugin; pluginAction: string; refresh: () => void; onClose: () => void; // context props t: (key: string, params?: object) => string; }; type State = { success: boolean; restart: boolean; loading: boolean; error?: Error; }; 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; `; class PluginModal extends React.Component { constructor(props: Props) { super(props); this.state = { loading: false, restart: false, success: false, }; } onSuccess = () => { const { restart } = this.state; const { refresh, onClose } = this.props; const newState = { loading: false, error: undefined, }; if (restart) { waitForRestart() .then(() => { this.setState({ ...newState, success: true, }); }) .catch(error => { this.setState({ loading: false, success: false, error, }); }); } else { this.setState(newState, () => { refresh(); onClose(); }); } }; createPluginActionLink = () => { const { plugin, pluginAction } = this.props; const { restart } = this.state; let pluginActionLink = ''; if (pluginAction === PluginAction.INSTALL) { pluginActionLink = plugin._links.install.href; } else if (pluginAction === PluginAction.UPDATE) { pluginActionLink = plugin._links.update.href; } else if (pluginAction === PluginAction.UNINSTALL) { pluginActionLink = plugin._links.uninstall.href; } return pluginActionLink + '?restart=' + restart.toString(); }; handlePluginAction = (e: Event) => { this.setState({ loading: true, }); e.preventDefault(); apiClient .post(this.createPluginActionLink()) .then(this.onSuccess) .catch(error => { this.setState({ loading: false, success: false, error: error, }); }); }; footer = () => { const { pluginAction, onClose, t } = this.props; const { loading, error, restart, success } = this.state; let color = pluginAction === PluginAction.UNINSTALL ? 'warning' : 'primary'; let label = `plugins.modal.${pluginAction}`; if (restart) { color = 'warning'; label = `plugins.modal.${pluginAction}AndRestart`; } return (