-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutils.ts
204 lines (187 loc) · 5.37 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { FormikErrors, FormikTouched, FormikValues } from 'formik';
import { EuiFilterSelectItem } from '@elastic/eui';
import { Schema, ObjectSchema } from 'yup';
import * as yup from 'yup';
import {
FieldType,
FieldValue,
IComponent,
IComponentData,
IComponentField,
WorkspaceFormValues,
WORKFLOW_STATE,
ReactFlowComponent,
Workflow,
WorkflowTemplate,
} from '../../common';
// Append 16 random characters
export function generateId(prefix: string): string {
const uniqueChar = () => {
// eslint-disable-next-line no-bitwise
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
return `${prefix}_${uniqueChar()}${uniqueChar()}${uniqueChar()}${uniqueChar()}`;
}
// Adding any instance metadata. Converting the base IComponent obj into
// an instance-specific IComponentData obj.
export function initComponentData(
data: IComponent,
componentId: string
): IComponentData {
return {
...data,
id: componentId,
} as IComponentData;
}
/*
**************** Formik (form) utils **********************
*/
// TODO: below, we are hardcoding to only persisting and validating create fields.
// If we support both, we will need to dynamically update.
// Converting stored values in component data to initial formik values
export function componentDataToFormik(data: IComponentData): FormikValues {
const formikValues = {} as FormikValues;
data.createFields?.forEach((field) => {
formikValues[field.id] = field.value || getInitialValue(field.type);
});
return formikValues;
}
// TODO: below, we are hardcoding to only persisting and validating create fields.
// If we support both, we will need to dynamically update.
// Injecting the current form values into the component data
export function formikToComponentData(
origData: IComponentData,
formValues: FormikValues
): IComponentData {
return {
...origData,
createFields: origData.createFields?.map(
(createField: IComponentField) => ({
...createField,
value: formValues[createField.id],
})
),
} as IComponentData;
}
// Helper fn to remove state-related fields from a workflow and have a stateless template
// to export and/or pass around, use when updating, etc.
export function reduceToTemplate(workflow: Workflow): WorkflowTemplate {
const {
id,
lastUpdated,
lastLaunched,
state,
...workflowTemplate
} = workflow;
return workflowTemplate;
}
// Helper fn to get an initial value based on the field type
export function getInitialValue(fieldType: FieldType): FieldValue {
switch (fieldType) {
case 'string': {
return '';
}
case 'select': {
return '';
}
case 'json': {
return {};
}
}
}
export function isFieldInvalid(
componentId: string,
fieldName: string,
errors: FormikErrors<WorkspaceFormValues>,
touched: FormikTouched<WorkspaceFormValues>
): boolean {
return (
errors[componentId]?.[fieldName] !== undefined &&
touched[componentId]?.[fieldName] !== undefined
);
}
export function getFieldError(
componentId: string,
fieldName: string,
errors: FormikErrors<WorkspaceFormValues>
): string | undefined {
return errors[componentId]?.[fieldName] as string | undefined;
}
// Process the raw ReactFlow nodes.
// De-select them all, and propagate the form data to the internal node data
export function processNodes(
nodes: ReactFlowComponent[],
formValues: WorkspaceFormValues
): ReactFlowComponent[] {
return nodes.map((node: ReactFlowComponent) => {
return {
...node,
selected: false,
data: formikToComponentData(
{ ...node.data, selected: false },
formValues[node.id]
),
};
});
}
/*
**************** Yup (validation) utils **********************
*/
// TODO: below, we are hardcoding to only persisting and validating create fields.
// If we support both, we will need to dynamically update.
export function getComponentSchema(data: IComponentData): ObjectSchema<any> {
const schemaObj = {} as { [key: string]: Schema };
data.createFields?.forEach((field) => {
schemaObj[field.id] = getFieldSchema(field);
});
return yup.object(schemaObj);
}
// TODO: finalize validations for different field types. May need
// to refer to some backend implementations or OpenSearch documentation
function getFieldSchema(field: IComponentField): Schema {
let baseSchema: Schema;
switch (field.type) {
case 'string':
case 'select': {
baseSchema = yup.string().min(1, 'Too short').max(70, 'Too long');
break;
}
case 'json': {
baseSchema = yup.object().json();
break;
}
}
// TODO: make optional schema if we support optional fields in the future
// return field.optional
// ? baseSchema.optional()
// : baseSchema.required('Required');
return baseSchema.required('Required');
}
export function getStateOptions(): EuiFilterSelectItem[] {
return [
// @ts-ignore
{
name: WORKFLOW_STATE.NOT_STARTED,
checked: 'on',
} as EuiFilterSelectItem,
// @ts-ignore
{
name: WORKFLOW_STATE.PROVISIONING,
checked: 'on',
} as EuiFilterSelectItem,
// @ts-ignore
{
name: WORKFLOW_STATE.FAILED,
checked: 'on',
} as EuiFilterSelectItem,
// @ts-ignore
{
name: WORKFLOW_STATE.COMPLETED,
checked: 'on',
} as EuiFilterSelectItem,
];
}