diff --git a/packages/flow-client/src/app/redux/modules/builder/builder.slice.spec.ts b/packages/flow-client/src/app/redux/modules/builder/builder.slice.spec.ts index 6c38c26..4ab47ac 100644 --- a/packages/flow-client/src/app/redux/modules/builder/builder.slice.spec.ts +++ b/packages/flow-client/src/app/redux/modules/builder/builder.slice.spec.ts @@ -1,3 +1,4 @@ +import { RootState } from '../../store'; import { flowActions } from '../flow/flow.slice'; import { builderActions, @@ -12,6 +13,7 @@ import { selectNewFlowCounter, selectNewFolderCounter, selectNewTreeItem, + BuilderState, } from './builder.slice'; describe('builder.slice', () => { @@ -220,7 +222,7 @@ describe('builder.slice', () => { }); describe('extra reducers', () => { - it('flowActions.removeEntity() should trigger closeFlow if the entity is an open flow', () => { + it('flowActions.removeFlowEntity() should trigger closeFlow if the entity is an open flow', () => { const initialState = { ...baseInitialState, openFlows: ['flow1', 'flow2'], @@ -228,7 +230,7 @@ describe('builder.slice', () => { }; const action = { - type: flowActions.removeEntity.type, + type: flowActions.removeFlowEntity.type, payload: 'flow1', }; @@ -239,7 +241,7 @@ describe('builder.slice', () => { expect(state.openFlows).toEqual(['flow2']); }); - it('flowActions.removeEntity() should not affect state if the entity is not an open flow', () => { + it('flowActions.removeFlowEntity() should not affect state if the entity is not an open flow', () => { const initialState = { ...baseInitialState, openFlows: ['flow1', 'flow2'], @@ -247,7 +249,7 @@ describe('builder.slice', () => { }; const action = { - type: flowActions.removeEntity.type, + type: flowActions.removeFlowEntity.type, payload: 'flow3', // flow3 is not in openFlows }; @@ -264,8 +266,8 @@ describe('builder.slice', () => { builder: { ...baseInitialState, theme: 'dark' as const, - }, - }; + } as BuilderState, + } as RootState; expect(selectTheme(state)).toEqual('dark'); }); @@ -275,8 +277,8 @@ describe('builder.slice', () => { builder: { ...baseInitialState, showPrimarySidebar: true, - }, - }; + } as BuilderState, + } as RootState; expect(selectShowPrimarySidebar(state)).toEqual(true); }); @@ -286,8 +288,8 @@ describe('builder.slice', () => { builder: { ...baseInitialState, showSecondarySidebar: false, - }, - }; + } as BuilderState, + } as RootState; expect(selectShowSecondarySidebar(state)).toEqual(false); }); @@ -297,8 +299,8 @@ describe('builder.slice', () => { builder: { ...baseInitialState, showConsolePanel: true, - }, - }; + } as BuilderState, + } as RootState; expect(selectShowConsolePanel(state)).toEqual(true); }); @@ -308,8 +310,8 @@ describe('builder.slice', () => { builder: { ...baseInitialState, editing: 'node1', - }, - }; + } as BuilderState, + } as RootState; expect(selectEditing(state)).toEqual('node1'); }); @@ -319,8 +321,8 @@ describe('builder.slice', () => { builder: { ...baseInitialState, openFlows: ['flow1', 'flow2'], - }, - }; + } as BuilderState, + } as RootState; expect(selectOpenFlows(state)).toEqual(['flow1', 'flow2']); }); @@ -330,8 +332,8 @@ describe('builder.slice', () => { builder: { ...baseInitialState, activeFlow: 'flow1', - }, - }; + } as BuilderState, + } as RootState; expect(selectActiveFlow(state)).toEqual('flow1'); }); @@ -341,8 +343,8 @@ describe('builder.slice', () => { builder: { ...baseInitialState, newFlowCounter: 5, - }, - }; + } as BuilderState, + } as RootState; expect(selectNewFlowCounter(state)).toEqual(5); }); @@ -352,8 +354,8 @@ describe('builder.slice', () => { builder: { ...baseInitialState, newFolderCounter: 3, - }, - }; + } as BuilderState, + } as RootState; expect(selectNewFolderCounter(state)).toEqual(3); }); @@ -363,8 +365,8 @@ describe('builder.slice', () => { builder: { ...baseInitialState, newTreeItem: 'newItem1', - }, - }; + } as BuilderState, + } as RootState; expect(selectNewTreeItem(state)).toEqual('newItem1'); }); diff --git a/packages/flow-client/src/app/redux/modules/builder/builder.slice.ts b/packages/flow-client/src/app/redux/modules/builder/builder.slice.ts index fe638ff..25eeeee 100644 --- a/packages/flow-client/src/app/redux/modules/builder/builder.slice.ts +++ b/packages/flow-client/src/app/redux/modules/builder/builder.slice.ts @@ -1,6 +1,7 @@ -import { createSlice, PayloadAction } from '@reduxjs/toolkit'; +import { createSelector, createSlice, PayloadAction } from '@reduxjs/toolkit'; import { Theme } from '../../../themes'; import { flowActions } from '../flow/flow.slice'; +import { RootState } from '../../store'; export const BUILDER_FEATURE_KEY = 'builder'; @@ -77,6 +78,12 @@ export const builderSlice = createSlice({ }, // Action to set the active flow setActiveFlow: (state, action: PayloadAction) => { + if (action.payload) { + builderSlice.caseReducers.openFlow( + state, + action as PayloadAction + ); + } state.activeFlow = action.payload; }, addNewFlow: (state, action: PayloadAction) => { @@ -93,7 +100,7 @@ export const builderSlice = createSlice({ }, extraReducers(builder) { builder.addCase( - flowActions.removeEntity, + flowActions.removeFlowEntity, (state, action: PayloadAction) => { if (state.openFlows.includes(action.payload)) { builderSlice.caseReducers.closeFlow(state, action); @@ -108,31 +115,57 @@ export const builderReducer = builderSlice.reducer; export const builderActions = builderSlice.actions; // Selectors -export const selectTheme = (state: { [BUILDER_FEATURE_KEY]: BuilderState }) => - state[BUILDER_FEATURE_KEY].theme; -export const selectShowPrimarySidebar = (state: { - [BUILDER_FEATURE_KEY]: BuilderState; -}) => state[BUILDER_FEATURE_KEY].showPrimarySidebar; -export const selectShowSecondarySidebar = (state: { - [BUILDER_FEATURE_KEY]: BuilderState; -}) => state[BUILDER_FEATURE_KEY].showSecondarySidebar; -export const selectShowConsolePanel = (state: { - [BUILDER_FEATURE_KEY]: BuilderState; -}) => state[BUILDER_FEATURE_KEY].showConsolePanel; -export const selectEditing = (state: { [BUILDER_FEATURE_KEY]: BuilderState }) => - state[BUILDER_FEATURE_KEY].editing; -export const selectOpenFlows = (state: { - [BUILDER_FEATURE_KEY]: BuilderState; -}) => state[BUILDER_FEATURE_KEY].openFlows; -export const selectActiveFlow = (state: { - [BUILDER_FEATURE_KEY]: BuilderState; -}) => state[BUILDER_FEATURE_KEY].activeFlow; -export const selectNewFlowCounter = (state: { - [BUILDER_FEATURE_KEY]: BuilderState; -}) => state[BUILDER_FEATURE_KEY].newFlowCounter; -export const selectNewFolderCounter = (state: { - [BUILDER_FEATURE_KEY]: BuilderState; -}) => state[BUILDER_FEATURE_KEY].newFolderCounter; -export const selectNewTreeItem = (state: { - [BUILDER_FEATURE_KEY]: BuilderState; -}) => state[BUILDER_FEATURE_KEY].newTreeItem; + +// builder state +export const selectBuilderState = (state: RootState) => + state[BUILDER_FEATURE_KEY]; + +export const selectTheme = createSelector( + selectBuilderState, + (builderState: BuilderState) => builderState.theme +); + +export const selectShowPrimarySidebar = createSelector( + selectBuilderState, + (builderState: BuilderState) => builderState.showPrimarySidebar +); + +export const selectShowSecondarySidebar = createSelector( + selectBuilderState, + (builderState: BuilderState) => builderState.showSecondarySidebar +); + +export const selectShowConsolePanel = createSelector( + selectBuilderState, + (builderState: BuilderState) => builderState.showConsolePanel +); + +export const selectEditing = createSelector( + selectBuilderState, + (builderState: BuilderState) => builderState.editing +); + +export const selectOpenFlows = createSelector( + selectBuilderState, + (builderState: BuilderState) => builderState.openFlows +); + +export const selectActiveFlow = createSelector( + selectBuilderState, + (builderState: BuilderState) => builderState.activeFlow +); + +export const selectNewFlowCounter = createSelector( + selectBuilderState, + (builderState: BuilderState) => builderState.newFlowCounter +); + +export const selectNewFolderCounter = createSelector( + selectBuilderState, + (builderState: BuilderState) => builderState.newFolderCounter +); + +export const selectNewTreeItem = createSelector( + selectBuilderState, + (builderState: BuilderState) => builderState.newTreeItem +);