/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import React, { Component } from "react"; import { EuiComboBox, EuiComboBoxOptionOption, EuiFormRow, EuiSpacer, EuiSwitch } from "@elastic/eui"; import { AliasAction, AliasActionItem, AliasActions, UIAction } from "../../../../../../models/interfaces"; import AliasUIAction, { MAX_ALIAS_ACTIONS } from "./AliasUIAction"; import { inputLimitText } from "../../../../CreatePolicy/utils/helpers"; export interface AliasUIActionComponentProps { action: AliasAction; clone: (action: AliasAction) => AliasUIAction; errors: { [key in AliasActions]: string | undefined }; onChangeAction: (uiAction: UIAction) => void; selectedItems: { [key in AliasActions]: EuiComboBoxOptionOption[] }; } export interface AliasUIActionComponentState { addAliasToggle: boolean; removeAliasToggle: boolean; } export default class AliasUIActionComponent extends Component { constructor(props: AliasUIActionComponentProps) { super(props); const { selectedItems } = props; this.state = { addAliasToggle: selectedItems.add?.length > 0, removeAliasToggle: selectedItems.remove?.length > 0, }; } componentDidMount() { // TODO: Implement functionality to retrieve, and populate the combo boxes with any pre-existing aliases. } onCreateOption = (value: string, options: EuiComboBoxOptionOption[], aliasAction: AliasActions) => { const { action, clone, onChangeAction } = this.props; options.push({ label: value }); const aliasActions = action.alias.actions.concat(this.parseToAliasActionItems(options, aliasAction)); onChangeAction(clone({ ...action, alias: { actions: aliasActions } })); }; parseToAliasActionItems = (options: EuiComboBoxOptionOption[], aliasActionType = AliasActions.ADD) => { return options.map((option) => ({ [aliasActionType]: { alias: option.label } })) as AliasActionItem[]; }; onAddAliasChange = (options = []) => { const { action, clone, selectedItems, onChangeAction } = this.props; const parsedOptions = this.parseToAliasActionItems(options, AliasActions.ADD); const parseSelectedItems = this.parseToAliasActionItems(selectedItems.remove || [], AliasActions.REMOVE); onChangeAction( clone({ ...action, alias: { // Consolidating the changed options with the existing options in the other combo box actions: parsedOptions.concat(parseSelectedItems), }, }) ); }; onRemoveAliasChange = (options) => { const { action, clone, selectedItems, onChangeAction } = this.props; const parsedOptions = this.parseToAliasActionItems(options, AliasActions.REMOVE); const parseSelectedItems = this.parseToAliasActionItems(selectedItems.add || [], AliasActions.ADD); onChangeAction( clone({ ...action, alias: { // Consolidating the changed options with the existing options in the other combo box actions: parsedOptions.concat(parseSelectedItems), }, }) ); }; render() { const { errors, selectedItems } = this.props; const { addAliasToggle, removeAliasToggle } = this.state; return ( <> { // If the user disables the combo box while there are entries in it, clear the inputs if (addAliasToggle && selectedItems.add?.length > 0) this.onAddAliasChange([]); this.setState({ ...this.state, addAliasToggle: e.target.checked }); }} data-test-subj={"add-alias-toggle"} /> {addAliasToggle && ( <> <> this.onCreateOption(searchValue, options, AliasActions.ADD)) } onChange={(options) => this.onAddAliasChange(options)} isInvalid={errors.add !== undefined} data-test-subj={"add-alias-combo-box"} /> {inputLimitText(selectedItems.add?.length, MAX_ALIAS_ACTIONS, "alias", "aliases")} )} { // If the user disables the combo box while there are entries in it, clear the inputs if (removeAliasToggle && selectedItems.remove?.length > 0) this.onRemoveAliasChange([]); this.setState({ ...this.state, removeAliasToggle: e.target.checked }); }} data-test-subj={"remove-alias-toggle"} /> {removeAliasToggle && ( <> <> this.onCreateOption(searchValue, options, AliasActions.REMOVE)) } onChange={(options) => this.onRemoveAliasChange(options)} isInvalid={errors.remove !== undefined} data-test-subj={"remove-alias-combo-box"} /> {inputLimitText(selectedItems.remove?.length, MAX_ALIAS_ACTIONS, "alias", "aliases")} )} ); } }