From f6d2da9fc4cfc2a7c5938abe55d2a326094cbc72 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Wed, 18 May 2022 15:54:49 +0100 Subject: [PATCH 1/4] hide the error message when changing stage --- .../components/stage-editor/stage-editor.jsx | 2 +- .../src/modules/pipeline.ts | 59 +++++++++++-------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/packages/compass-aggregations/src/components/stage-editor/stage-editor.jsx b/packages/compass-aggregations/src/components/stage-editor/stage-editor.jsx index a3e8a22127c..190ccfa94b4 100644 --- a/packages/compass-aggregations/src/components/stage-editor/stage-editor.jsx +++ b/packages/compass-aggregations/src/components/stage-editor/stage-editor.jsx @@ -136,7 +136,7 @@ class StageEditor extends Component { } renderSyntaxError() { - if (!this.props.isValid) { + if (!this.props.isValid && this.props.syntaxError) { return (
{ const selectStageOperator = (state: State, action: AnyAction): State => { const operatorName = action.stageOperator; const oldStage = state[action.index]; - if (operatorName !== oldStage.stageOperator) { - const newState = copyState(state); - // if the value of the existing state operator has not been modified by user, - // we can easily replace it or else persist the one user changed - let value = getStageDefaultValue(operatorName, action.isCommenting, action.env); - if (hasUserChangedStage(oldStage, action.env)) { - value = oldStage.stage; - } - newState[action.index].stageOperator = operatorName; - newState[action.index].stage = value; - newState[action.index].isExpanded = true; - newState[action.index].isComplete = false; - newState[action.index].previewDocuments = []; - if ( - [SEARCH, SEARCH_META, DOCUMENTS].includes(newState[action.index].stageOperator) && - action.env !== ADL && action.env !== ATLAS - ) { - newState[action.index].isMissingAtlasOnlyStageSupport = true; - } else { - newState[action.index].isMissingAtlasOnlyStageSupport = false; - } - const { isValid, syntaxError } = validateStage(newState[action.index]); - newState[action.index].isValid = isValid; - newState[action.index].syntaxError = syntaxError; - return newState; + + if (operatorName === oldStage.stageOperator) { + return state; } - return state; + + // if the value of the existing state operator has not been modified by user, + // we can easily replace it or else persist the one user changed + let value; + if (hasUserChangedStage(oldStage, action.env)) { + value = oldStage.stage; + } + else { + value = getStageDefaultValue(operatorName, action.isCommenting, action.env); + } + + const newState = copyState(state); + + newState[action.index].stageOperator = operatorName; + newState[action.index].stage = value; + newState[action.index].isExpanded = true; + newState[action.index].isComplete = false; + newState[action.index].previewDocuments = []; + newState[action.index].isMissingAtlasOnlyStageSupport = !!( + [SEARCH, SEARCH_META, DOCUMENTS].includes(operatorName) && + action.env !== ADL && action.env !== ATLAS + ); + + const { isValid } = validateStage(newState[action.index]); + newState[action.index].isValid = false; + // Clear the error message because the user just changed the operator. Even + // though the text wouldn't have changed if the user has edited it. + newState[action.index].syntaxError = null; + + return newState; }; export const replaceOperatorSnippetTokens = (str: string): string => { From 467f2dedbc0982f7252c469ab5e357d57d750fd9 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Wed, 18 May 2022 16:31:23 +0100 Subject: [PATCH 2/4] didn't mean that.. --- packages/compass-aggregations/src/modules/pipeline.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compass-aggregations/src/modules/pipeline.ts b/packages/compass-aggregations/src/modules/pipeline.ts index c202dc850fc..82306a02c64 100644 --- a/packages/compass-aggregations/src/modules/pipeline.ts +++ b/packages/compass-aggregations/src/modules/pipeline.ts @@ -348,7 +348,7 @@ const selectStageOperator = (state: State, action: AnyAction): State => { ); const { isValid } = validateStage(newState[action.index]); - newState[action.index].isValid = false; + newState[action.index].isValid = isValid; // Clear the error message because the user just changed the operator. Even // though the text wouldn't have changed if the user has edited it. newState[action.index].syntaxError = null; From 548acd8076bde414c5f76d3b598e65c3e432676a Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Thu, 19 May 2022 10:42:11 +0100 Subject: [PATCH 3/4] clear error, not syntaxError --- .../compass-aggregations/src/modules/pipeline.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/compass-aggregations/src/modules/pipeline.ts b/packages/compass-aggregations/src/modules/pipeline.ts index 82306a02c64..4110de88fcb 100644 --- a/packages/compass-aggregations/src/modules/pipeline.ts +++ b/packages/compass-aggregations/src/modules/pipeline.ts @@ -325,7 +325,7 @@ const selectStageOperator = (state: State, action: AnyAction): State => { return state; } - // if the value of the existing state operator has not been modified by user, + // If the value of the existing state operator has not been modified by user, // we can easily replace it or else persist the one user changed let value; if (hasUserChangedStage(oldStage, action.env)) { @@ -347,11 +347,14 @@ const selectStageOperator = (state: State, action: AnyAction): State => { action.env !== ADL && action.env !== ATLAS ); - const { isValid } = validateStage(newState[action.index]); + // Re-validate the stage according to the new operator + const { isValid, syntaxError } = validateStage(newState[action.index]); newState[action.index].isValid = isValid; - // Clear the error message because the user just changed the operator. Even - // though the text wouldn't have changed if the user has edited it. - newState[action.index].syntaxError = null; + newState[action.index].syntaxError = syntaxError; + + // Clear the server error when we change the stage operator because it isn't + // relevant anymore + newState[action.index].error = null; return newState; }; From 25c51a892d451154798e19be70634954e5d70f10 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Thu, 19 May 2022 10:56:14 +0100 Subject: [PATCH 4/4] test STAGE_OPERATOR_SELECTED --- .../src/stores/store.spec.js | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/compass-aggregations/src/stores/store.spec.js b/packages/compass-aggregations/src/stores/store.spec.js index 6148f0556a9..ce92752d5d2 100644 --- a/packages/compass-aggregations/src/stores/store.spec.js +++ b/packages/compass-aggregations/src/stores/store.spec.js @@ -6,7 +6,8 @@ import { stageDeleted, stageAdded, stageAddedAfter, - stageToggled + stageToggled, + stageOperatorSelected } from '../modules/pipeline'; import { INITIAL_STATE } from '../modules/index'; import { expect } from 'chai'; @@ -426,5 +427,34 @@ describe('Aggregation Store', function() { store.dispatch(stageCollapseToggled(0)); }); }); + + context('when the action is STAGE_OPERATOR_SELECTED', function() { + it('clears the error', function(done) { + const unsubscribe = store.subscribe(() => { + unsubscribe(); + const pipeline = store.getState().pipeline[0]; + delete pipeline.id; + expect(pipeline).to.deep.equal({ + stageOperator: '$match', + stage: '{\n query\n}', + isMissingAtlasOnlyStageSupport: false, + isValid: false, + isEnabled: true, + isExpanded: true, + isLoading: false, + isComplete: false, + previewDocuments: [], + syntaxError: 'Stage must be a properly formatted document.', + error: null, + projections: [] + }); + done(); + }); + + store.getState().pipeline[0].error = 'foo'; + + store.dispatch(stageOperatorSelected(0, '$match', false, 'on-prem')); + }); + }); }); });