import React, { useState } from 'react';
import { FormattedMessage } from 'react-intl';
import { useSelector } from 'react-redux';
import {
ActionGroup,
Alert,
Button,
Form,
Grid,
GridItem,
Select,
SelectOption,
SelectVariant,
TextContent,
Text,
} from '@patternfly/react-core';
import { translate as __ } from 'foremanReact/common/I18n';
import PropTypes from 'prop-types';
import { useAPI } from 'foremanReact/common/hooks/API/APIHooks';
import { STATUS } from 'foremanReact/constants';
import api, { orgId } from '../../../../services/api';
import { ENVIRONMENT_PATHS_KEY } from '../../../../scenes/ContentViews/components/EnvironmentPaths/EnvironmentPathConstants';
import EnvironmentPaths from '../../../../scenes/ContentViews/components/EnvironmentPaths/EnvironmentPaths';
import ContentViewSelect from '../../../../scenes/ContentViews/components/ContentViewSelect/ContentViewSelect';
import ContentViewSelectOption from '../../../../scenes/ContentViews/components/ContentViewSelect/ContentViewSelectOption';
import { selectContentViewsStatus, selectJobInvocationPath } from '../selectors';
import { getCVPlaceholderText, shouldDisableCVSelect } from '../../../ContentViews/components/ContentViewSelect/helpers';
import { selectEnvironmentPaths } from '../../../ContentViews/components/EnvironmentPaths/EnvironmentPathSelectors';
const ENV_PATH_OPTIONS = { key: ENVIRONMENT_PATHS_KEY };
const ContentSourceSelect = ({
contentSources,
selections,
onToggle,
onSelect,
isOpen,
isDisabled,
onClear,
}) => (
{__('Content source')}
);
ContentSourceSelect.propTypes = {
contentSources: PropTypes.arrayOf(PropTypes.shape({})),
selections: PropTypes.string,
onToggle: PropTypes.func,
onSelect: PropTypes.func,
onClear: PropTypes.func,
isOpen: PropTypes.bool,
isDisabled: PropTypes.bool,
};
ContentSourceSelect.defaultProps = {
contentSources: [],
selections: null,
onToggle: undefined,
onSelect: undefined,
onClear: undefined,
isOpen: false,
isDisabled: false,
};
const ContentSourceForm = ({
handleSubmit,
environments,
handleEnvironment,
contentViews,
handleContentView,
contentViewName,
contentSources,
handleContentSource,
contentSourceId,
showCVOnlyAlert,
hostDetailsPath,
hostEditPath,
contentHosts,
isLoading,
hostsUpdated,
showTemplate,
}) => {
const pathsUrl = `/organizations/${orgId()}/environments/paths?permission_type=promotable${contentSourceId ? `&content_source_id=${contentSourceId}` : ''}`;
useAPI( // No TableWrapper here, so we can useAPI from Foreman
'get',
api.getApiUrl(pathsUrl),
ENV_PATH_OPTIONS,
);
const contentViewsStatus = useSelector(selectContentViewsStatus);
const environmentPathResponse = useSelector(selectEnvironmentPaths);
const jobInvocationPath = useSelector(selectJobInvocationPath);
const envList = environmentPathResponse?.results?.map(path => path.environments).flat();
const [csSelectOpen, setCSSelectOpen] = useState(false);
const [cvSelectOpen, setCVSelectOpen] = useState(false);
const hostCount = contentHosts.length;
const handleCSSelect = (_event, selection) => {
handleContentSource(typeof selection === 'number' ? selection.toString() : selection);
setCSSelectOpen(false);
};
const handleCVSelect = (_event, selection) => {
handleContentView(selection);
setCVSelectOpen(false);
};
const formIsValid = () => (!!environments &&
!!contentViewName &&
!!contentSourceId &&
hostCount !== 0);
const contentSourcesIsDisabled = (isLoading || contentSources.length === 0 ||
hostCount === 0);
const environmentIsDisabled = (isLoading ||
contentSourceId === '');
const viewIsDisabled = (isLoading || contentViews.length === 0 ||
contentSourceId === '');
const cvPlaceholderText = getCVPlaceholderText({
contentSourceId,
environments,
contentViewsStatus,
cvSelectOptions: contentViews,
});
const disableCVSelect = shouldDisableCVSelect({
contentSourceId,
environments,
contentViewsStatus,
cvSelectOptions: contentViews,
});
return (
);
};
ContentSourceForm.propTypes = {
handleSubmit: PropTypes.func.isRequired,
environments: PropTypes.arrayOf(PropTypes.shape({})),
handleEnvironment: PropTypes.func.isRequired,
contentViews: PropTypes.arrayOf(PropTypes.shape({})),
handleContentView: PropTypes.func.isRequired,
contentViewName: PropTypes.string,
contentSources: PropTypes.arrayOf(PropTypes.shape({})),
handleContentSource: PropTypes.func.isRequired,
contentSourceId: PropTypes.string,
showCVOnlyAlert: PropTypes.bool,
hostDetailsPath: PropTypes.string.isRequired,
hostEditPath: PropTypes.string.isRequired,
contentHosts: PropTypes.arrayOf(PropTypes.shape({})),
isLoading: PropTypes.bool,
hostsUpdated: PropTypes.bool,
showTemplate: PropTypes.func.isRequired,
};
ContentSourceForm.defaultProps = {
environments: [],
contentViews: [],
contentViewName: '',
contentSources: [],
contentSourceId: '',
showCVOnlyAlert: false,
contentHosts: [],
isLoading: false,
hostsUpdated: false,
};
export default ContentSourceForm;