@@ -320,33 +320,43 @@ const moveStage = (state: State, action: AnyAction): State => {
320320const 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
352362export const replaceOperatorSnippetTokens = ( str : string ) : string => {
0 commit comments