Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Jovan Cvetkovic <[email protected]>
  • Loading branch information
jovancvetkovic3006 committed Jun 23, 2023
1 parent 5ec5aad commit a9cef2e
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const getMonitors = async (httpClient) => {
}
};

const AssociateMonitors = ({ isDarkMode, values, httpClient }) => {
const AssociateMonitors = ({ isDarkMode, values, httpClient, errors }) => {
const [graphUi, setGraphUi] = useState(false);

useEffect(() => {
Expand All @@ -50,7 +50,7 @@ const AssociateMonitors = ({ isDarkMode, values, httpClient }) => {
{graphUi ? (
<MonitorsList values={values} httpClient={httpClient} />
) : (
<MonitorsEditor values={values} />
<MonitorsEditor values={values} errors={errors} isDarkMode={isDarkMode} />
)}
</Fragment>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useState, useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import { FormikCodeEditor } from '../../../../../components/FormControls';
import { hasError, isInvalid, validateExtractionQuery } from '../../../../../utils/validate';

const MonitorsEditor = ({ values, isDarkMode }) => {
const MonitorsEditor = ({ values, isDarkMode, errors }) => {
const codeFieldName = 'associatedMonitorsEditor';
const formikValueName = 'associatedMonitors';
const [editorValue, setEditorValue] = useState('');

useEffect(() => {
try {
const code = JSON.stringify(values.associatedMonitors, null, 4);
// _.set(values, codeFieldName, code);
_.set(values, codeFieldName, code);
setEditorValue(code);
} catch (e) {}
}, [values.associatedMonitors]);
Expand All @@ -34,25 +33,26 @@ const MonitorsEditor = ({ values, isDarkMode }) => {
};

const isInvalid = (name, form) => {
if (form.touched[codeFieldName]) {
try {
const associatedMonitors = form.values[name];
const json = JSON.parse(associatedMonitors);
return !json.sequence?.delegates?.length;
} catch (e) {
return false;
}
try {
const associatedMonitors = form.values[name];
const json = JSON.parse(associatedMonitors);
return !json.sequence?.delegates?.length || json.sequence?.delegates?.length < 2;
} catch (e) {
return true;
}
};

const hasError = (name, form) => {
const associatedMonitors = form.values[name];
return validate(associatedMonitors);
};

const validate = (value) => {
try {
const associatedMonitors = form.values[name];
const json = JSON.parse(associatedMonitors);
return (
json.sequence?.delegates?.length < 2 &&
'Delegates list can not be empty or have less then two associated monitors.'
);
const json = JSON.parse(value);
if (!json.sequence?.delegates?.length || json.sequence?.delegates?.length < 2) {
return 'Delegates list can not be empty or have less then two associated monitors.';
}
} catch (e) {
return 'Invalid json.';
}
Expand All @@ -62,14 +62,17 @@ const MonitorsEditor = ({ values, isDarkMode }) => {
<FormikCodeEditor
name={codeFieldName}
formRow
fieldProps={{}}
fieldProps={{
validate: validate,
}}
rowProps={{
label: 'Define workflow',
fullWidth: true,
// isInvalid: (name, form) => isInvalid(name, form),
// error: (name, form) => hasError(name, form),
isInvalid: (name, form) => form.touched[codeFieldName] && isInvalid(name, form),
error: hasError,
}}
inputProps={{
isInvalid: (name, form) => isInvalid(name, form),
mode: 'json',
width: '80%',
height: '300px',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const MonitorsList = ({ values, httpClient }) => {
<FormikInputWrapper
name={formikFieldName}
fieldProps={{
validate: () => validate(),
validate: validate,
}}
render={({ form }) => (
<FormikFormRow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,12 @@ export default class CreateMonitor extends Component {

<EuiSpacer />

<WorkflowDetails isDarkMode={isDarkMode} values={values} httpClient={httpClient} />
<WorkflowDetails
isDarkMode={isDarkMode}
values={values}
httpClient={httpClient}
errors={errors}
/>

<EuiSpacer />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const FORMIK_INITIAL_VALUES = {
detectorId: '',
associatedMonitors: DEFAULT_ASSOCIATED_MONITORS_VALUE,
associatedMonitorsList: [],
associatedMonitorsEditor: '',
};

export const FORMIK_INITIAL_AGG_VALUES = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import AssociateMonitors from '../../components/AssociateMonitors/AssociateMonit
import { EuiSpacer } from '@elastic/eui';
import { MONITOR_TYPE, SEARCH_TYPE } from '../../../../utils/constants';

const WorkflowDetails = ({ values, isDarkMode, httpClient }) => {
const WorkflowDetails = ({ values, isDarkMode, httpClient, errors }) => {
const isAd = values.searchType === SEARCH_TYPE.AD;
const isComposite = values.monitor_type === MONITOR_TYPE.COMPOSITE_LEVEL;
return (
Expand All @@ -29,7 +29,12 @@ const WorkflowDetails = ({ values, isDarkMode, httpClient }) => {
{isComposite && (
<Fragment>
<EuiSpacer size="xl" />
<AssociateMonitors isDarkMode={isDarkMode} values={values} httpClient={httpClient} />
<AssociateMonitors
isDarkMode={isDarkMode}
values={values}
httpClient={httpClient}
errors={errors}
/>
</Fragment>
)}
</ContentPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const ExpressionBuilder = ({

const condition = _.get(values, formikFullFieldName, '');
const expressions = conditionToExpressions(condition, monitors);
setUsedExpressions(expressions.length ? expressions : [DEFAULT_EXPRESSION]);
setUsedExpressions(expressions?.length ? expressions : [DEFAULT_EXPRESSION]);
};

const expressionsToCondition = (expressions) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { FormikCodeEditor } from '../../../../components/FormControls';
const ExpressionEditor = ({ values, formikFieldName, formikFieldPath, isDarkMode = false }) => {
const [editorValue, setEditorValue] = useState('');
const formikFullFieldName = `${formikFieldPath}${formikFieldName}`;
const formikFullCodeFieldName = 'triggerConditionsCode';
const formikFullCodeFieldName = _.replace(`${formikFullFieldName}_code`, /[.\[\]]/gm, '_');

useEffect(() => {
const code = _.get(values, formikFullFieldName, '');
// _.set(values, formikFullCodeFieldName, code);
_.set(values, formikFullCodeFieldName, code);
setEditorValue(code);
}, [values]);

Expand All @@ -19,19 +19,25 @@ const ExpressionEditor = ({ values, formikFieldName, formikFieldPath, isDarkMode
return !form.values[name]?.length && 'Invalid condition.';
};

const validate = (value) => {
if (!value?.length) return 'Invalid condition.';
};

return (
<FormikCodeEditor
name={formikFullCodeFieldName}
formRow
fieldProps={{}}
fieldProps={{
validate: validate,
}}
rowProps={{
label: 'Trigger condition',
fullWidth: true,
// isInvalid: (name, form) => form.touched[name] && isInvalid(name, form),
// error: (name, form) => hasError(name, form),
isInvalid: (name, form) => form.touched[name] && isInvalid(name, form),
error: (name, form) => hasError(name, form),
}}
inputProps={{
// isInvalid: (name, form) => form.touched[name] && isInvalid(name, form),
isInvalid: (name, form) => form.touched[name] && isInvalid(name, form),
mode: 'text',
width: '80%',
height: '300px',
Expand Down

0 comments on commit a9cef2e

Please sign in to comment.