import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Col, Tabs, Tab, Form, FormGroup, FormControl, ControlLabel } from 'react-bootstrap';
import { bindMethods, Button, Icon, Modal, Spinner, OverlayTrigger, Tooltip } from 'patternfly-react';
import { isEqual } from 'lodash';
import TooltipButton from 'react-bootstrap-tooltip-button';
import { LoadingState } from '../../../move_to_pf/LoadingState';
import { Table } from '../../../move_to_foreman/components/common/table';
import { columns } from './ManifestHistoryTableSchema';
import ConfirmDialog from '../../../move_to_foreman/components/common/ConfirmDialog';
import DeleteManifestModalText from './DeleteManifestModalText';
import {
BLOCKING_FOREMAN_TASK_TYPES,
MANIFEST_TASKS_BULK_SEARCH_ID,
} from '../SubscriptionConstants';
class ManageManifestModal extends Component {
constructor(props) {
super(props);
this.state = {
showModal: props.showModal,
actionInProgress: props.taskInProgress,
showDeleteManifestModalDialog: false,
};
bindMethods(this, [
'hideModal',
'saveOrganization',
'uploadManifest',
'refreshManifest',
'deleteManifest',
'manifestExists',
'disabledTooltipText',
]);
}
componentDidMount() {
this.loadData();
}
static getDerivedStateFromProps(newProps, prevState) {
if (
!isEqual(newProps.showModal, prevState.showModal) ||
!isEqual(newProps.taskInProgress, prevState.actionInProgress)
) {
return {
showModal: newProps.showModal,
actionInProgress: newProps.taskInProgress,
};
}
return null;
}
componentDidUpdate(prevProp, prevState) {
const { actionInProgress } = this.state;
if (prevState.actionInProgress && !actionInProgress) {
this.props.loadOrganization();
}
}
loadData() {
this.props.loadManifestHistory();
}
hideModal() {
this.setState({ showModal: false, showDeleteManifestModalDialog: false });
this.props.onClose();
}
saveOrganization(event) {
this.props.saveOrganization({ redhat_repository_url: event.target.value });
}
uploadManifest(fileList) {
this.setState({ actionInProgress: true });
if (fileList.length > 0) {
this.props.uploadManifest(fileList[0]);
}
}
refreshManifest() {
this.props.refreshManifest();
this.setState({ actionInProgress: true });
}
deleteManifest() {
this.setState({ actionInProgress: true });
this.props.deleteManifest()
.then(() =>
this.props.bulkSearch({
search_id: MANIFEST_TASKS_BULK_SEARCH_ID,
type: 'all',
active_only: true,
action_types: BLOCKING_FOREMAN_TASK_TYPES,
}));
this.showDeleteManifestModal(false);
}
showDeleteManifestModal(show) {
this.setState({
showDeleteManifestModalDialog: show,
});
}
disabledTooltipText() {
if (this.state.actionInProgress) {
return __('This is disabled because a manifest task is in progress');
}
return __('This is disabled because no manifest exists');
}
manifestExists() {
const { organization } = this.props;
return organization.owner_details && organization.owner_details.upstreamConsumer;
}
render() {
const {
manifestHistory, organization, disableManifestActions, disabledReason,
} = this.props;
const { actionInProgress } = this.state;
const emptyStateData = () => ({
header: __('There is no Manifest History to display.'),
description: __('Import a Manifest using the manifest tab above.'),
documentation: {
title: __('Learn more about adding Subscription Manifests'),
url: 'http://redhat.com',
},
});
const getManifestName = () => {
let name = __('No Manifest Uploaded');
if (organization.owner_details && organization.owner_details.upstreamConsumer) {
const link = ['https://', organization.owner_details.upstreamConsumer.webUrl,
organization.owner_details.upstreamConsumer.uuid].join('/');
name = (
{organization.owner_details.upstreamConsumer.name}
);
}
return name;
};
return (
{__('Manage Manifest')}
);
}
}
ManageManifestModal.propTypes = {
uploadManifest: PropTypes.func.isRequired,
refreshManifest: PropTypes.func.isRequired,
deleteManifest: PropTypes.func.isRequired,
loadManifestHistory: PropTypes.func.isRequired,
organization: PropTypes.shape({}).isRequired,
disableManifestActions: PropTypes.bool,
disabledReason: PropTypes.string,
loadOrganization: PropTypes.func.isRequired,
saveOrganization: PropTypes.func.isRequired,
taskInProgress: PropTypes.bool.isRequired,
manifestHistory: PropTypes.shape({}).isRequired,
showModal: PropTypes.bool.isRequired,
onClose: PropTypes.func,
bulkSearch: PropTypes.func.isRequired,
};
ManageManifestModal.defaultProps = {
disableManifestActions: false,
disabledReason: '',
onClose() {},
};
export default ManageManifestModal;