/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. * * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ import React, { Fragment, useState } from 'react'; import { EuiFormRow, EuiSelect, EuiAccordion, EuiFlexGroup, EuiFlexItem, EuiTitle, EuiButton, EuiFieldText, EuiCheckbox, EuiButtonIcon, } from '@elastic/eui'; import './styles.scss'; import { Field, FieldProps } from 'formik'; import { required, isInvalid, getError, validateFeatureName, } from '../../../../utils/utils'; import { get } from 'lodash'; import { FEATURE_TYPE_OPTIONS } from '../../utils/constants'; import { FEATURE_TYPE } from '../../../../models/interfaces'; import { formikToSimpleAggregation } from '../../utils/helpers'; import { AggregationSelector } from '../AggregationSelector'; import { CustomAggregation } from '../CustomAggregation'; interface FeatureAccordionProps { onDelete(): void; index: number; feature: any; handleChange(event: React.ChangeEvent): void; displayMode?: string; } export const FeatureAccordion = (props: FeatureAccordionProps) => { const initialIsOpen = get(props.feature, 'newFeature', false); const [showSubtitle, setShowSubtitle] = useState(!initialIsOpen); const simpleAggDescription = (feature: any) => ( Field: {get(feature, 'aggregationOf.0.label')} Aggregation method: {feature.aggregationBy} State: {feature.featureEnabled ? 'Enabled' : 'Disabled'} ); const customAggDescription = (feature: any) => ( Custom expression State: {feature.featureEnabled ? 'Enabled' : 'Disabled'} ); const showFeatureDescription = (feature: any) => { return feature && feature.featureType === FEATURE_TYPE.SIMPLE ? simpleAggDescription(feature) : customAggDescription(feature); }; const featureButtonContent = (feature: any, index: number) => { if (props.displayMode === 'flyout') { return (
{feature.featureName ? feature.featureName : 'Add feature'}
{showSubtitle ? showFeatureDescription(feature) : null}
); } return (
{feature.featureName ? feature.featureName : 'Add feature'}
{showSubtitle ? showFeatureDescription(feature) : null}
); }; const deleteAction = (onClick: any) => { if (props.displayMode === 'flyout') { return ( ); } else { return ( Delete ); } }; return ( { isOpen ? setShowSubtitle(false) : setShowSubtitle(true); }} > {({ field, form }: FieldProps) => ( )} {({ field, form }: FieldProps) => ( )} {({ field, form }: FieldProps) => ( { props.handleChange(e); if ( e.currentTarget.value === FEATURE_TYPE.CUSTOM && !get(form.errors, `featureList.${props.index}`) ) { const aggregationQuery = formikToSimpleAggregation( props.feature ); form.setFieldValue( `featureList.${props.index}.aggregationQuery`, JSON.stringify(aggregationQuery, null, 4) ); } }} /> {field.value === FEATURE_TYPE.SIMPLE ? ( ) : ( )} )} ); };