/* * Copyright OpenSearch Contributors * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://www.apache.org/licenses/LICENSE-2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ import { EuiButton, EuiFieldText, EuiFlexGroup, EuiFlexItem, EuiSpacer, EuiFormRow, } from '@elastic/eui'; import { map, isEmpty } from 'lodash'; import React, { Dispatch, Fragment, SetStateAction } from 'react'; import { UserAttributes } from '../../types'; import { appendElementToArray, removeElementFromArray, updateElementInArrayHandler, } from '../../utils/array-state-utils'; import { PanelWithHeader } from '../../utils/panel-with-header'; import { UserAttributeStateClass } from './types'; import { FormRow } from '../../utils/form-row'; import { DocLinks } from '../../constants'; export function buildAttributeState(attributesDict: UserAttributes): UserAttributeStateClass[] { return map(attributesDict, (v, k) => ({ key: k.toString(), value: v, })); } export function unbuildAttributeState(attributesList: UserAttributeStateClass[]): UserAttributes { return attributesList.reduce( (attributes: UserAttributes, { key, value }: UserAttributeStateClass) => ({ ...attributes, [key]: value, }), {} ); } function getEmptyAttribute() { return { key: '', value: '' }; } function generateAttributesPanels( userAttributes: UserAttributeStateClass[], setAttributes: Dispatch> ) { const panels = userAttributes.map((userAttribute, arrayIndex) => { const onValueChangeHandler = (attributeToUpdate: string) => updateElementInArrayHandler(setAttributes, [arrayIndex, attributeToUpdate]); return ( onValueChangeHandler('key')(e.target.value)} placeholder="Type in variable name" /> onValueChangeHandler('value')(e.target.value)} placeholder="Type in value" /> removeElementFromArray(setAttributes, [], arrayIndex)} > Remove ); }); return <>{panels}; } export function AttributePanel(props: { state: UserAttributeStateClass[]; setState: Dispatch>; }) { const { state, setState } = props; // Show one empty row if there is no data. if (isEmpty(state)) { setState([getEmptyAttribute()]); } return ( {generateAttributesPanels(state, setState)} { appendElementToArray(setState, [], getEmptyAttribute()); }} > Add another attribute ); }