Skip to content

Commit 69dd2a1

Browse files
Merge pull request #822 from mercedes-benz/feat/FormsButtonImprovements
Feat/forms button improvements
2 parents d822773 + 0432950 commit 69dd2a1

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

cypress/e2e/start_page.cy.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ describe('NeoDash E2E Tests', () => {
205205

206206
cy.get('main .react-grid-item:eq(2) button[aria-label="run"]').scrollIntoView().should('be.visible').click();
207207
cy.wait(500);
208+
cy.get('#form-submit').should('be.disabled');
208209
cy.get('#autocomplete').type('The Matrix');
209210
cy.get('#autocomplete-option-0').click();
211+
cy.get('#form-submit').should('not.be.disabled');
210212
cy.get('#form-submit').click();
211213
cy.wait(500);
212214
cy.get('.form-submitted-message').should('have.text', 'Form Submitted.Reset Form');

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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Extracts all parameter names from a given Cypher query string.
3+
*
4+
* @param {string} cypherQuery The Cypher query string to extract parameter names from.
5+
* @returns {string[]} An array containing all extracted parameter names.
6+
*/
7+
export const extractAllParameterNames = (cypherQuery: string): string[] => {
8+
// A regular expression pattern to match parameter names following '$'
9+
const pattern = /\$([A-Za-z_]\w*)/g;
10+
11+
const parameterNames: string[] = [];
12+
let match: any;
13+
14+
while ((match = pattern.exec(cypherQuery)) !== null) {
15+
parameterNames.push(match[1]);
16+
}
17+
18+
return parameterNames;
19+
}
20+
21+
/**
22+
* Checks if all parameter names are present in the global parameter names.
23+
*
24+
* @param {string[]} parameterNames An array of parameter names to be checked.
25+
* @param {object} globalParameterNames The object containing global parameter names to compare against.
26+
* @returns {boolean} A boolean indicating whether all parameters are present in the global parameters.
27+
*/
28+
export const checkParametersNameInGlobalParameter = (parameterNames: string[], globalParameterNames: any): boolean => {
29+
for (const key of parameterNames) {
30+
if (!globalParameterNames[key] || (Array.isArray(globalParameterNames[key]) && globalParameterNames[key].length === 0) || globalParameterNames[key] === '') {
31+
return true;
32+
}
33+
}
34+
return false;
35+
}

0 commit comments

Comments
 (0)