Skip to content

Commit a9668d8

Browse files
committed
Disable form button until all parameters in given
1 parent f63c104 commit a9668d8

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/extensions/forms/chart/NeoForm.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { REPORT_LOADING_ICON } from '../../../report/Report';
77
import debounce from 'lodash/debounce';
88
import { RUN_QUERY_DELAY_MS } from '../../../config/ReportConfig';
99
import NeoParameterSelectionChart from '../../../chart/parameter/ParameterSelectionChart';
10+
import { checkParametersNameInGlobalParameter, extractAllParameterNames } from '../../../utils/parameterUtils';
1011

1112
enum FormStatus {
1213
DATA_ENTRY = 0, // The user is filling in the form.
@@ -42,6 +43,14 @@ const NeoForm = (props: ChartProps) => {
4243
});
4344
}
4445

46+
const isParametersDefined = (cypherQuery: string | undefined) => {
47+
const parameterNames = extractAllParameterNames(cypherQuery);
48+
if (props.parameters) {
49+
return checkParametersNameInGlobalParameter(parameterNames, props.parameters);
50+
}
51+
return false;
52+
};
53+
4554
useEffect(() => {
4655
// If the parameters change after the form is completed, reset it, as there might be another submission.
4756
if (status == FormStatus.SUBMITTED) {
@@ -77,7 +86,7 @@ const NeoForm = (props: ChartProps) => {
7786
<Button
7887
style={{ marginLeft: 15 }}
7988
id='form-submit'
80-
disabled={!submitButtonActive}
89+
disabled={!submitButtonActive || isParametersDefined(props.query)}
8190
onClick={() => {
8291
if (!props.query || !props.query.trim()) {
8392
props.createNotification(

src/utils/parameterUtils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export const extractAllParameterNames = (cypherQuery) => {
2+
// A regular expression pattern to match parameter names following '$'
3+
const pattern = /\$([A-Za-z_]\w*)/g;
4+
5+
const parameterNames: string[] = [];
6+
let match: any;
7+
8+
while ((match = pattern.exec(cypherQuery)) !== null) {
9+
parameterNames.push(match[1]);
10+
}
11+
12+
return parameterNames;
13+
}
14+
15+
export const checkParametersNameInGlobalParameter = (parameterNames: string[], globalParameterNames: any) => {
16+
for (const key of parameterNames) {
17+
if (!globalParameterNames[key] || (Array.isArray(globalParameterNames[key]) && globalParameterNames[key].length === 0)) {
18+
return true;
19+
}
20+
}
21+
return false;
22+
}

0 commit comments

Comments
 (0)