import React, { useState, useEffect } from 'react';
import { useSelector, shallowEqual, useDispatch } from 'react-redux';
import { useParams } from 'react-router-dom';
import {
Grid,
GridItem,
TextContent,
Text,
TextVariants,
Button,
Flex,
FlexItem,
Dropdown,
DropdownItem,
KebabToggle,
DropdownPosition,
} from '@patternfly/react-core';
import { STATUS } from 'foremanReact/constants';
import { ExternalLinkAltIcon } from '@patternfly/react-icons';
import { translate as __ } from 'foremanReact/common/I18n';
import { selectIsPollingTask, selectIsPollingTaskComplete } from '../../Tasks/TaskSelectors';
import getContentViewDetails from './ContentViewDetailActions';
import Loading from '../../../components/Loading';
import ContentViewInfo from './ContentViewInfo';
import ContentViewVersionsRoutes from './Versions';
import ContentViewFilterRoutes from './Filters';
import ContentViewRepositories from './Repositories/ContentViewRepositories';
import ContentViewComponents from './ComponentContentViews/ContentViewComponents';
import ContentViewHistories from './Histories/ContentViewHistories';
import { selectCVDetails, selectCVDetailStatus, selectCVDetailError } from './ContentViewDetailSelectors';
import RoutedTabs from '../../../components/RoutedTabs';
import ContentViewIcon from '../components/ContentViewIcon';
import CVBreadCrumb from '../components/CVBreadCrumb';
import PublishContentViewWizard from '../Publish/PublishContentViewWizard';
import { hasPermission } from '../helpers';
import CopyContentViewModal from '../Copy/CopyContentViewModal';
import ContentViewDeleteWizard from '../Delete/ContentViewDeleteWizard';
import EmptyStateMessage from '../../../components/Table/EmptyStateMessage';
import { CONTENT_VIEW_NEEDS_PUBLISH_RESET, cvVersionTaskPollingKey } from '../ContentViewsConstants';
import { clearPollTaskData, stopPollingTask } from '../../Tasks/TaskActions';
import { truncate } from '../../../utils/helpers';
export default () => {
const { id } = useParams();
const cvId = Number(id);
const contentViewVersionTaskKey = cvVersionTaskPollingKey(cvId);
const details = useSelector(state => selectCVDetails(state, cvId), shallowEqual);
const [isPublishModalOpen, setIsPublishModalOpen] = useState(false);
const [dropDownOpen, setDropdownOpen] = useState(false);
const [copying, setCopying] = useState(false);
const [deleting, setDeleting] = useState(false);
const dispatch = useDispatch();
const status = useSelector(state => selectCVDetailStatus(state, cvId), shallowEqual);
const error = useSelector(state => selectCVDetailError(state, cvId), shallowEqual);
const taskNeedsToBeStopped = useSelector(state =>
selectIsPollingTaskComplete(state, contentViewVersionTaskKey));
const isTaskRunning = useSelector(state =>
selectIsPollingTask(state, contentViewVersionTaskKey));
useEffect(() => {
if (taskNeedsToBeStopped) {
dispatch(stopPollingTask(contentViewVersionTaskKey));
dispatch(clearPollTaskData(contentViewVersionTaskKey));
}
return () => {
// This stops the polling task if you leave contentView/XXX route.
if (isTaskRunning && !taskNeedsToBeStopped) {
dispatch(stopPollingTask(contentViewVersionTaskKey));
dispatch(clearPollTaskData(contentViewVersionTaskKey));
}
};
}, [contentViewVersionTaskKey, dispatch, isTaskRunning, taskNeedsToBeStopped]);
useEffect(() => {
dispatch(getContentViewDetails(cvId));
dispatch({ type: CONTENT_VIEW_NEEDS_PUBLISH_RESET });
}, [cvId, dispatch]);
if (status === STATUS.PENDING) return ();
if (status === STATUS.ERROR) return ();
const dropDownItems = [
{
setCopying(true);
}}
>
{__('Copy')}
,
{
setDeleting(true);
}}
>
{__('Delete')}
,
];
const {
name, composite, permissions, environments, versions,
generated_for: generatedFor, import_only: importOnly,
} = details;
const generatedContentView = generatedFor !== 'none';
const detailsTab = {
key: 'details',
title: __('Details'),
content: ,
};
const versionsTab = {
key: 'versions',
title: __('Versions'),
content: ,
};
const contentViewsTab = {
key: 'contentviews',
title: __('Content views'),
content: ,
};
const repositoriesTab = {
key: 'repositories',
title: __('Repositories'),
content: ,
};
const filtersTab = {
key: 'filters',
title: __('Filters'),
content: ,
};
const historyTab = {
key: 'history',
title: __('History'),
content: ,
};
/* eslint-disable no-nested-ternary */
const tabs = [
detailsTab,
versionsTab,
...(composite ? [contentViewsTab] :
((importOnly || generatedContentView) ?
[repositoriesTab] : [repositoriesTab, filtersTab])),
historyTab,
];
/* eslint-enable no-nested-ternary */
return (
<>
{hasPermission(permissions, 'publish_content_views') &&
{isPublishModalOpen &&
{
if (step3) dispatch(getContentViewDetails(cvId));
setIsPublishModalOpen(false);
}}
aria-label="publish_content_view_modal"
/>}
}
}
isOpen={dropDownOpen}
isPlain
dropdownItems={dropDownItems}
/>
{
copying &&
}
{
deleting &&
}
>
);
};