/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import React, { ChangeEvent } from "react"; import { EuiFormRow, EuiFieldNumber, EuiFieldText, EuiSpacer } from "@elastic/eui"; import { RolloverAction, UIAction } from "../../../../../models/interfaces"; import { makeId } from "../../../../utils/helpers"; import { ActionType } from "../../utils/constants"; import EuiFormCustomLabel from "../EuiFormCustomLabel"; export default class RolloverUIAction implements UIAction { id: string; action: RolloverAction; type = ActionType.Rollover; constructor(action: RolloverAction, id: string = makeId()) { this.action = action; this.id = id; } content = () => `Rollover`; clone = (action: RolloverAction) => new RolloverUIAction(action, this.id); isValid = () => { const minIndexAge = this.action.rollover.min_index_age; const minDocCount = this.action.rollover.min_doc_count; const minSize = this.action.rollover.min_size; const minPrimaryShardSize = this.action.rollover.min_primary_shard_size; if (typeof minDocCount !== "undefined") { if (minDocCount <= 0) return false; } // for minIndexAge and minSize and minPrimaryShardSize just let them through and backend will fail the validation // TODO -> add validation for index age and size.. but involves replicating checks for byte strings and time strings return true; }; render = (action: UIAction, onChangeAction: (action: UIAction) => void) => { const rollover = action.action.rollover; return ( <> ) => { const minIndexAge = e.target.value; const rollover = { ...action.action.rollover }; if (minIndexAge) rollover.min_index_age = minIndexAge; else delete rollover.min_index_age; onChangeAction(this.clone({ rollover })); }} data-test-subj="action-render-rollover-min-index-age" /> ) => { const minDocCount = e.target.valueAsNumber; const rollover = { ...action.action.rollover }; if (!isNaN(minDocCount)) rollover.min_doc_count = minDocCount; else delete rollover.min_doc_count; onChangeAction(this.clone({ rollover })); }} data-test-subj="action-render-rollover-min-doc-count" /> ) => { const minSize = e.target.value; const rollover = { ...action.action.rollover }; if (minSize) rollover.min_size = minSize; else delete rollover.min_size; onChangeAction(this.clone({ rollover })); }} data-test-subj="action-render-rollover-min-size" /> ) => { const minPrimaryShardSize = e.target.value; const rollover = { ...action.action.rollover }; if (minPrimaryShardSize) rollover.min_primary_shard_size = minPrimaryShardSize; else delete rollover.min_primary_shard_size; onChangeAction(this.clone({ rollover })); }} data-test-subj="action-render-rollover-min-primary-shard-size" /> ); }; toAction = () => this.action; }