Skip to content

Commit f2633dd

Browse files
authored
fix(aggregations): hide the stage error message when changing the operator COMPASS-5684 (#3110)
* hide the error message when changing stage * didn't mean that.. * clear error, not syntaxError * test STAGE_OPERATOR_SELECTED
1 parent 254a02f commit f2633dd

File tree

3 files changed

+68
-28
lines changed

3 files changed

+68
-28
lines changed

packages/compass-aggregations/src/components/stage-editor/stage-editor.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class StageEditor extends Component {
136136
}
137137

138138
renderSyntaxError() {
139-
if (!this.props.isValid) {
139+
if (!this.props.isValid && this.props.syntaxError) {
140140
return (
141141
<div
142142
className={styles['stage-editor-syntax-error']}

packages/compass-aggregations/src/modules/pipeline.ts

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -320,33 +320,43 @@ const moveStage = (state: State, action: AnyAction): State => {
320320
const selectStageOperator = (state: State, action: AnyAction): State => {
321321
const operatorName = action.stageOperator;
322322
const oldStage = state[action.index];
323-
if (operatorName !== oldStage.stageOperator) {
324-
const newState = copyState(state);
325-
// if the value of the existing state operator has not been modified by user,
326-
// we can easily replace it or else persist the one user changed
327-
let value = getStageDefaultValue(operatorName, action.isCommenting, action.env);
328-
if (hasUserChangedStage(oldStage, action.env)) {
329-
value = oldStage.stage;
330-
}
331-
newState[action.index].stageOperator = operatorName;
332-
newState[action.index].stage = value;
333-
newState[action.index].isExpanded = true;
334-
newState[action.index].isComplete = false;
335-
newState[action.index].previewDocuments = [];
336-
if (
337-
[SEARCH, SEARCH_META, DOCUMENTS].includes(newState[action.index].stageOperator) &&
338-
action.env !== ADL && action.env !== ATLAS
339-
) {
340-
newState[action.index].isMissingAtlasOnlyStageSupport = true;
341-
} else {
342-
newState[action.index].isMissingAtlasOnlyStageSupport = false;
343-
}
344-
const { isValid, syntaxError } = validateStage(newState[action.index]);
345-
newState[action.index].isValid = isValid;
346-
newState[action.index].syntaxError = syntaxError;
347-
return newState;
323+
324+
if (operatorName === oldStage.stageOperator) {
325+
return state;
348326
}
349-
return state;
327+
328+
// If the value of the existing state operator has not been modified by user,
329+
// we can easily replace it or else persist the one user changed
330+
let value;
331+
if (hasUserChangedStage(oldStage, action.env)) {
332+
value = oldStage.stage;
333+
}
334+
else {
335+
value = getStageDefaultValue(operatorName, action.isCommenting, action.env);
336+
}
337+
338+
const newState = copyState(state);
339+
340+
newState[action.index].stageOperator = operatorName;
341+
newState[action.index].stage = value;
342+
newState[action.index].isExpanded = true;
343+
newState[action.index].isComplete = false;
344+
newState[action.index].previewDocuments = [];
345+
newState[action.index].isMissingAtlasOnlyStageSupport = !!(
346+
[SEARCH, SEARCH_META, DOCUMENTS].includes(operatorName) &&
347+
action.env !== ADL && action.env !== ATLAS
348+
);
349+
350+
// Re-validate the stage according to the new operator
351+
const { isValid, syntaxError } = validateStage(newState[action.index]);
352+
newState[action.index].isValid = isValid;
353+
newState[action.index].syntaxError = syntaxError;
354+
355+
// Clear the server error when we change the stage operator because it isn't
356+
// relevant anymore
357+
newState[action.index].error = null;
358+
359+
return newState;
350360
};
351361

352362
export const replaceOperatorSnippetTokens = (str: string): string => {

packages/compass-aggregations/src/stores/store.spec.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
stageDeleted,
77
stageAdded,
88
stageAddedAfter,
9-
stageToggled
9+
stageToggled,
10+
stageOperatorSelected
1011
} from '../modules/pipeline';
1112
import { INITIAL_STATE } from '../modules/index';
1213
import { expect } from 'chai';
@@ -426,5 +427,34 @@ describe('Aggregation Store', function() {
426427
store.dispatch(stageCollapseToggled(0));
427428
});
428429
});
430+
431+
context('when the action is STAGE_OPERATOR_SELECTED', function() {
432+
it('clears the error', function(done) {
433+
const unsubscribe = store.subscribe(() => {
434+
unsubscribe();
435+
const pipeline = store.getState().pipeline[0];
436+
delete pipeline.id;
437+
expect(pipeline).to.deep.equal({
438+
stageOperator: '$match',
439+
stage: '{\n query\n}',
440+
isMissingAtlasOnlyStageSupport: false,
441+
isValid: false,
442+
isEnabled: true,
443+
isExpanded: true,
444+
isLoading: false,
445+
isComplete: false,
446+
previewDocuments: [],
447+
syntaxError: 'Stage must be a properly formatted document.',
448+
error: null,
449+
projections: []
450+
});
451+
done();
452+
});
453+
454+
store.getState().pipeline[0].error = 'foo';
455+
456+
store.dispatch(stageOperatorSelected(0, '$match', false, 'on-prem'));
457+
});
458+
});
429459
});
430460
});

0 commit comments

Comments
 (0)