/* * 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 { isEmpty } from 'lodash'; import React, { Dispatch, Fragment, SetStateAction, useState } from 'react'; import { appendElementToArray, removeElementFromArray, updateElementInArrayHandler, } from '../../utils/array-state-utils'; import { PanelWithHeader } from '../../utils/panel-with-header'; import { DocLinks, LIMIT_WIDTH_INPUT_CLASS } from '../../constants'; function generateBackendRolesPanels( backendRoles: string[], setBackendRoles: Dispatch>, emptyRoleIndex: number, roleEmptyErrorMessage: string, setRoleEmptyErrorMessage: Dispatch> ) { const panels = backendRoles.map((backendRole, arrayIndex) => { return ( { updateElementInArrayHandler(setBackendRoles, [arrayIndex])(e.target.value); setRoleEmptyErrorMessage(''); }} placeholder="Type in backend role" /> removeElementFromArray(setBackendRoles, [], arrayIndex)} > Remove ); }); return <>{panels}; } export function BackendRolePanel(props: { state: string[]; setState: Dispatch>; }) { const { state, setState } = props; const [roleEmptyErrorMessage, setRoleEmptyErrorMessage] = useState(''); const [emptyRoleIndex, setEmptyRoleIndex] = useState(-1); // Show one empty row if there is no data. if (isEmpty(state)) { setState(['']); } return ( {generateBackendRolesPanels( state, setState, emptyRoleIndex, roleEmptyErrorMessage, setRoleEmptyErrorMessage )} { if (state.indexOf('') !== -1) { setRoleEmptyErrorMessage('Type a backend role before adding a new one'); setEmptyRoleIndex(state.indexOf('')); } else { setRoleEmptyErrorMessage(''); appendElementToArray(setState, [], ''); } }} > Add another backend role ); }