Skip to content

Commit

Permalink
Simplify form management (opensearch-project#299)
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Ohlsen <[email protected]>
  • Loading branch information
ohltyler authored Aug 19, 2024
1 parent 60de152 commit fb3dbba
Show file tree
Hide file tree
Showing 37 changed files with 36 additions and 185 deletions.
1 change: 0 additions & 1 deletion common/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export type SearchIndexConfig = {

export type IngestConfig = {
enabled: IConfigField;
source: {};
pipelineName: IConfigField;
enrich: ProcessorsConfig;
index: IndexConfig;
Expand Down
4 changes: 4 additions & 0 deletions common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ export function getCharacterLimitedString(
: input
: '';
}

export function customStringify(jsonObj: {}): string {
return JSON.stringify(jsonObj, undefined, 2);
}
3 changes: 2 additions & 1 deletion public/pages/workflow_detail/components/export_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import {
CREATE_WORKFLOW_LINK,
Workflow,
customStringify,
getCharacterLimitedString,
} from '../../../../common';
import { reduceToTemplate } from '../../../utils';
Expand Down Expand Up @@ -62,7 +63,7 @@ export function ExportModal(props: ExportModalProps) {
if (props.workflow) {
const workflowTemplate = reduceToTemplate(props.workflow);
if (selectedOption === EXPORT_OPTION.JSON) {
setFormattedConfig(JSON.stringify(workflowTemplate, undefined, 2));
setFormattedConfig(customStringify(workflowTemplate));
} else if (selectedOption === EXPORT_OPTION.YAML) {
setFormattedConfig(yaml.dump(workflowTemplate));
}
Expand Down
17 changes: 0 additions & 17 deletions public/pages/workflow_detail/resizable_workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
uiConfigToFormik,
uiConfigToSchema,
} from '../../utils';
import { AppState, setDirty, useAppDispatch } from '../../store';
import { WorkflowInputs } from './workflow_inputs';
import { Workspace } from './workspace';
import { Tools } from './tools';
Expand All @@ -49,11 +48,6 @@ const TOOLS_PANEL_ID = 'tools_panel_id';
* panels - the ReactFlow workspace panel and the selected component details panel.
*/
export function ResizableWorkspace(props: ResizableWorkspaceProps) {
const dispatch = useAppDispatch();

// Overall workspace state
const { isDirty } = useSelector((state: AppState) => state.form);

// Workflow state
const [workflow, setWorkflow] = useState<Workflow | undefined>(
props.workflow
Expand Down Expand Up @@ -135,16 +129,6 @@ export function ResizableWorkspace(props: ResizableWorkspaceProps) {
}
}, [uiConfig]);

/**
* Function to pass down to the Formik <Form> components as a listener to propagate
* form changes to this parent component to re-enable save button, etc.
*/
function onFormChange() {
if (!isDirty) {
dispatch(setDirty());
}
}

return isValidWorkflow ? (
<Formik
enableReinitialize={true}
Expand Down Expand Up @@ -185,7 +169,6 @@ export function ResizableWorkspace(props: ResizableWorkspaceProps) {
>
<WorkflowInputs
workflow={props.workflow}
onFormChange={onFormChange}
uiConfig={uiConfig}
setUiConfig={setUiConfig}
setIngestResponse={setIngestResponse}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ interface ConfigFieldListProps {
configId: string;
configFields: IConfigField[];
baseConfigPath: string; // the base path of the nested config, if applicable. e.g., 'ingest.enrich'
onFormChange: () => void;
}

const CONFIG_FIELD_SPACER_SIZE = 'm';
Expand All @@ -42,7 +41,6 @@ export function ConfigFieldList(props: ConfigFieldListProps) {
label={camelCaseToTitleString(field.id)}
fieldPath={`${props.baseConfigPath}.${props.configId}.${field.id}`}
showError={true}
onFormChange={props.onFormChange}
/>
<EuiSpacer size={CONFIG_FIELD_SPACER_SIZE} />
</EuiFlexItem>
Expand All @@ -55,7 +53,6 @@ export function ConfigFieldList(props: ConfigFieldListProps) {
<SelectField
field={field}
fieldPath={`${props.baseConfigPath}.${props.configId}.${field.id}`}
onFormChange={props.onFormChange}
/>
<EuiSpacer size={CONFIG_FIELD_SPACER_SIZE} />
</EuiFlexItem>
Expand All @@ -68,7 +65,6 @@ export function ConfigFieldList(props: ConfigFieldListProps) {
<BooleanField
label={camelCaseToTitleString(field.id)}
fieldPath={`${props.baseConfigPath}.${props.configId}.${field.id}`}
onFormChange={props.onFormChange}
enabledOption={{
id: 'true',
label: 'True',
Expand All @@ -91,7 +87,6 @@ export function ConfigFieldList(props: ConfigFieldListProps) {
label={camelCaseToTitleString(field.id)}
fieldPath={`${props.baseConfigPath}.${props.configId}.${field.id}`}
showError={true}
onFormChange={props.onFormChange}
/>
<EuiSpacer size={CONFIG_FIELD_SPACER_SIZE} />
</EuiFlexItem>
Expand All @@ -104,7 +99,6 @@ export function ConfigFieldList(props: ConfigFieldListProps) {
<JsonField
label={camelCaseToTitleString(field.id)}
fieldPath={`${props.baseConfigPath}.${props.configId}.${field.id}`}
onFormChange={props.onFormChange}
/>
<EuiSpacer size={CONFIG_FIELD_SPACER_SIZE} />
</EuiFlexItem>
Expand All @@ -118,7 +112,6 @@ export function ConfigFieldList(props: ConfigFieldListProps) {
validate={false}
label={camelCaseToTitleString(field.id)}
fieldPath={`${props.baseConfigPath}.${props.configId}.${field.id}`}
onFormChange={props.onFormChange}
/>
<EuiSpacer size={CONFIG_FIELD_SPACER_SIZE} />
</EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import {
} from '@elastic/eui';
import { JsonField } from '../input_fields';

interface AdvancedSettingsProps {
onFormChange: () => void;
}
interface AdvancedSettingsProps {}

/**
* Input component for configuring ingest-side advanced settings
Expand All @@ -30,14 +28,12 @@ export function AdvancedSettings(props: AdvancedSettingsProps) {
<JsonField
label="Index mappings"
fieldPath={'ingest.index.mappings'}
onFormChange={props.onFormChange}
/>
</EuiFlexItem>
<EuiFlexItem>
<JsonField
label="Index settings"
fieldPath={'ingest.index.settings'}
onFormChange={props.onFormChange}
/>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { PROCESSOR_CONTEXT, WorkflowConfig } from '../../../../../common';
import { ProcessorsTitle } from '../../../../general_components';

interface EnrichDataProps {
onFormChange: () => void;
uiConfig: WorkflowConfig;
setUiConfig: (uiConfig: WorkflowConfig) => void;
}
Expand All @@ -27,7 +26,6 @@ export function EnrichData(props: EnrichDataProps) {
/>
<EuiFlexItem>
<ProcessorsList
onFormChange={props.onFormChange}
uiConfig={props.uiConfig}
setUiConfig={props.setUiConfig}
context={PROCESSOR_CONTEXT.INGEST}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import { EuiFlexGroup, EuiFlexItem, EuiTitle } from '@elastic/eui';
import { TextField } from '../input_fields';
import { AdvancedSettings } from './advanced_settings';

interface IngestDataProps {
onFormChange: () => void;
}
interface IngestDataProps {}

/**
* Input component for configuring the data ingest (the OpenSearch index)
Expand All @@ -24,14 +22,10 @@ export function IngestData(props: IngestDataProps) {
</EuiTitle>
</EuiFlexItem>
<EuiFlexItem>
<TextField
label="Index name"
fieldPath={'ingest.index.name'}
onFormChange={props.onFormChange}
/>
<TextField label="Index name" fieldPath={'ingest.index.name'} />
</EuiFlexItem>
<EuiFlexItem>
<AdvancedSettings onFormChange={props.onFormChange} />
<AdvancedSettings />
</EuiFlexItem>
</EuiFlexGroup>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { IngestData } from './ingest_data';
import { WorkflowConfig } from '../../../../../common';

interface IngestInputsProps {
onFormChange: () => void;
setIngestDocs: (docs: string) => void;
uiConfig: WorkflowConfig;
setUiConfig: (uiConfig: WorkflowConfig) => void;
Expand All @@ -24,26 +23,19 @@ export function IngestInputs(props: IngestInputsProps) {
return (
<EuiFlexGroup direction="column">
<EuiFlexItem grow={false}>
<SourceData
setIngestDocs={props.setIngestDocs}
onFormChange={props.onFormChange}
/>
<SourceData setIngestDocs={props.setIngestDocs} />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiHorizontalRule margin="none" />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EnrichData
onFormChange={props.onFormChange}
uiConfig={props.uiConfig}
setUiConfig={props.setUiConfig}
/>
<EnrichData uiConfig={props.uiConfig} setUiConfig={props.setUiConfig} />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiHorizontalRule margin="none" />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<IngestData onFormChange={props.onFormChange} />
<IngestData />
</EuiFlexItem>
</EuiFlexGroup>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { WorkspaceFormValues } from '../../../../../common';

interface SourceDataProps {
setIngestDocs: (docs: string) => void;
onFormChange: () => void;
}

/**
Expand Down Expand Up @@ -87,9 +86,6 @@ export function SourceData(props: SourceDataProps) {
label="Documents"
fieldPath={'ingest.docs'}
helpText="Documents should be formatted as a valid JSON array."
// when ingest doc values change, don't update the form
// since we initially only support running ingest once per configuration
onFormChange={() => {}}
editorHeight="25vh"
readOnly={false}
/>
Expand Down Expand Up @@ -127,9 +123,6 @@ export function SourceData(props: SourceDataProps) {
label="Documents"
fieldPath={'ingest.docs'}
helpText="Documents should be formatted as a valid JSON array."
// when ingest doc values change, don't update the form
// since we initially only support running ingest once per configuration
onFormChange={() => {}}
editorHeight="25vh"
readOnly={true}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { camelCaseToTitleString } from '../../../../utils';

interface BooleanFieldProps {
fieldPath: string; // the full path in string-form to the field (e.g., 'ingest.enrich.processors.text_embedding_processor.inputField')
onFormChange: () => void;
enabledOption: EuiRadioGroupOption;
disabledOption: EuiRadioGroupOption;
label?: string;
Expand Down Expand Up @@ -60,7 +59,6 @@ export function BooleanField(props: BooleanFieldProps) {
}
onChange={(id) => {
form.setFieldValue(field.name, !field.value);
props.onFormChange();
}}
/>
</EuiCompressedFormRow>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import {
EuiLink,
EuiText,
} from '@elastic/eui';
import { WorkspaceFormValues } from '../../../../../common';
import { WorkspaceFormValues, customStringify } from '../../../../../common';
import { camelCaseToTitleString } from '../../../../utils';

interface JsonFieldProps {
fieldPath: string; // the full path in string-form to the field (e.g., 'ingest.enrich.processors.text_embedding_processor.inputField')
onFormChange: () => void;
validate?: boolean;
label?: string;
helpLink?: string;
Expand Down Expand Up @@ -85,13 +84,12 @@ export function JsonField(props: JsonFieldProps) {
try {
form.setFieldValue(
field.name,
JSON.stringify(JSON.parse(jsonStr), undefined, 2)
customStringify(JSON.parse(jsonStr))
);
} catch (error) {
form.setFieldValue(field.name, jsonStr);
} finally {
form.setFieldTouched(field.name);
props.onFormChange();
}
}}
readOnly={props.readOnly || false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ interface MapArrayFieldProps {
helpText?: string;
keyPlaceholder?: string;
valuePlaceholder?: string;
onFormChange: () => void;
onMapAdd?: (curArray: MapArrayFormValue) => void;
onMapDelete?: (idxToDelete: number) => void;
keyOptions?: any[];
Expand All @@ -51,7 +50,6 @@ export function MapArrayField(props: MapArrayFieldProps) {
function addMap(curMapArray: MapArrayFormValue): void {
setFieldValue(props.fieldPath, [...curMapArray, []]);
setFieldTouched(props.fieldPath, true);
props.onFormChange();
if (props.onMapAdd) {
props.onMapAdd(curMapArray);
}
Expand All @@ -66,7 +64,6 @@ export function MapArrayField(props: MapArrayFieldProps) {
updatedMapArray.splice(entryIndexToDelete, 1);
setFieldValue(props.fieldPath, updatedMapArray);
setFieldTouched(props.fieldPath, true);
props.onFormChange();
if (props.onMapDelete) {
props.onMapDelete(entryIndexToDelete);
}
Expand Down Expand Up @@ -123,7 +120,6 @@ export function MapArrayField(props: MapArrayFieldProps) {
fieldPath={`${props.fieldPath}.${idx}`}
keyPlaceholder={props.keyPlaceholder}
valuePlaceholder={props.valuePlaceholder}
onFormChange={props.onFormChange}
keyOptions={props.keyOptions}
valueOptions={props.valueOptions}
/>
Expand Down
Loading

0 comments on commit fb3dbba

Please sign in to comment.