import React from 'react'; import PropTypes from 'prop-types'; import { Title, Text, TextVariants, Form, Alert } from '@patternfly/react-core'; import { translate as __ } from 'foremanReact/common/I18n'; import { SelectField } from '../form/SelectField'; import { GroupedSelectField } from '../form/GroupedSelectField'; export const CategoryAndTemplate = ({ jobCategories, jobTemplates, setJobTemplate, selectedTemplateID, selectedCategory, setCategory, errors, }) => { const templatesGroups = {}; jobTemplates.forEach(template => { if (templatesGroups[template.provider_type]?.options) templatesGroups[template.provider_type].options.push({ label: template.name, value: template.id, }); else templatesGroups[template.provider_type] = { options: [{ label: template.name, value: template.id }], groupLabel: template.provider_type, }; }); const selectedTemplate = jobTemplates.find( template => template.id === selectedTemplateID )?.name; const onSelectCategory = newCategory => { setCategory(newCategory); setJobTemplate(null); }; const { categoryError, allTemplatesError, templateError } = errors; const isError = !!(categoryError || allTemplatesError || templateError); return ( <> {__('Category and Template')} {__('All fields are required.')}
{isError && ( {categoryError && ( {__('Categories list failed with:')} {categoryError} )} {allTemplatesError && ( {__('Templates list failed with:')} {allTemplatesError} )} {templateError && ( {__('Template failed with:')} {templateError} )} )} ); }; CategoryAndTemplate.propTypes = { jobCategories: PropTypes.array, jobTemplates: PropTypes.array, setJobTemplate: PropTypes.func.isRequired, selectedTemplateID: PropTypes.number, setCategory: PropTypes.func.isRequired, selectedCategory: PropTypes.string, errors: PropTypes.shape({ categoryError: PropTypes.string, allTemplatesError: PropTypes.string, templateError: PropTypes.string, }), }; CategoryAndTemplate.defaultProps = { jobCategories: [], jobTemplates: [], selectedTemplateID: null, selectedCategory: null, errors: {}, }; export default CategoryAndTemplate;