Skip to content

Commit fd1561d

Browse files
Refactor state selectors
1 parent 1e28ed8 commit fd1561d

28 files changed

Lines changed: 1123 additions & 555 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"redux-persist": "^5.10.0",
4646
"redux-saga": "^0.16.0",
4747
"redux-thunk": "^2.2.0",
48+
"reselect": "^4.0.0",
4849
"websocket": "^1.0.26"
4950
},
5051
"devDependencies": {

‎src/public/dist/bundle.js‎

Lines changed: 728 additions & 313 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/public/dist/bundle.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/public/js/components/controls/FlowSelect/index.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { Menu, Dropdown, Button } from 'antd';
44

55
import { NO_TRAIL_FLOW } from 'utils/constants';
66
import { selectCodeCrumbedFlow } from 'components/dataBus/store/actions';
7+
import { getCodeCrumbsUserChoice } from 'components/dataBus/store/selectors';
8+
import { getCheckedState } from 'components/controls/ViewSwitches/store/selectors';
79
import './index.scss';
810

911
const FlowSelect = ({
@@ -56,12 +58,12 @@ const FlowSelect = ({
5658
};
5759

5860
const mapStateToProps = state => {
59-
const { checkedState } = state.viewSwitches;
60-
const { codeCrumbedFlowsMap, selectedCrumbedFlowKey } = state.dataBus;
61+
const { codeCrumbsDiagramOn, codeCrumbsMinimize } = getCheckedState(state);
62+
const { codeCrumbedFlowsMap, selectedCrumbedFlowKey } = getCodeCrumbsUserChoice(state);
6163

6264
return {
63-
codeCrumbsDiagramOn: checkedState.codeCrumbs,
64-
codeCrumbsMinimize: checkedState.codeCrumbsMinimize,
65+
codeCrumbsDiagramOn,
66+
codeCrumbsMinimize,
6567
codeCrumbedFlowsMap,
6668
selectedCrumbedFlowKey
6769
};

‎src/public/js/components/controls/ViewSwitches/ViewSwitchesContainer.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import React from 'react';
22
import { connect } from 'react-redux';
33

44
import { toggleSwitch, fireButtonAction } from './store/actions';
5+
import { getSwitches, getCheckedState, getDisabledState } from './store/selectors';
56
import ViewSwitchList from './component/List/ViewSwitchList';
67

78
const mapStateToProps = state => {
8-
const { switches, checkedState, disabledState } = state.viewSwitches;
9-
10-
return { switches, checkedState, disabledState };
9+
return {
10+
switches: getSwitches(state),
11+
checkedState: getCheckedState(state),
12+
disabledState: getDisabledState(state)
13+
};
1114
};
1215

1316
const mapDispatchToProps = {

‎src/public/js/components/controls/ViewSwitches/store/constants.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ export const ACTIONS = {
88
};
99

1010
export const CONTROLS_KEYS = {
11-
SOURCE: 'source',
11+
SOURCE_DIAGRAM_ON: 'sourceDiagramOn',
1212
SOURCE_COLLAPSE_TO_MIN: 'sourceCollapseToMin',
1313
SOURCE_EXPAND_ALL: 'sourceExpandAll',
1414
SOURCE_DIM_FOLDERS: 'sourceDimFolders',
1515
SOURCE_KEEP_ONLY_ACTIVE_ITEMS: 'sourceKeepOnlyActiveItems',
16-
SOURCE_EXPLORER_BAR: 'sourceExplorerBar',
17-
DEPENDENCIES: 'dependencies',
16+
SOURCE_EXPLORER_BAR: 'explorerBar',
17+
DEPENDENCIES_DIAGRAM_ON: 'dependenciesDiagramOn',
1818
DEPENDENCIES_SHOW_DIRECT_ONLY: 'dependenciesShowDirectOnly',
19-
CODE_CRUMBS: 'codeCrumbs',
19+
CODE_CRUMBS_DIAGRAM_ON: 'codeCrumbsDiagramOn',
2020
CODE_CRUMBS_MINIMIZE: 'codeCrumbsMinimize',
2121
CODE_CRUMBS_LINE_NUMBERS: 'codeCrumbsLineNumbers',
2222
CODE_CRUMBS_DETAILS: 'codeCrumbsDetails',
23-
CODE_CRUMBS_KEEP_ONLY_SELECTED_FLOW: 'codeCrumbsKeepOnlySelectedFlow',
23+
CODE_CRUMBS_FILTER_FLOW: 'codeCrumbsFilterFlow',
2424
SIDE_BAR: 'sideBar'
2525
};
2626

‎src/public/js/components/controls/ViewSwitches/store/reducer.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const DefaultState = {
44
switches: [
55
{
66
name: 'Source',
7-
key: CONTROLS_KEYS.SOURCE,
7+
key: CONTROLS_KEYS.SOURCE_DIAGRAM_ON,
88
children: [
99
{
1010
name: 'close all',
@@ -37,7 +37,7 @@ const DefaultState = {
3737
},
3838
{
3939
name: 'Dependencies',
40-
key: CONTROLS_KEYS.DEPENDENCIES,
40+
key: CONTROLS_KEYS.DEPENDENCIES_DIAGRAM_ON,
4141
children: [
4242
{
4343
name: 'direct only',
@@ -48,7 +48,7 @@ const DefaultState = {
4848
},
4949
{
5050
name: 'CodeCrumbs',
51-
key: CONTROLS_KEYS.CODE_CRUMBS,
51+
key: CONTROLS_KEYS.CODE_CRUMBS_DIAGRAM_ON,
5252
children: [
5353
{
5454
name: 'minimize',
@@ -58,7 +58,7 @@ const DefaultState = {
5858
{
5959
name: 'current trail only',
6060
title: 'Show only files for selected flow',
61-
key: CONTROLS_KEYS.CODE_CRUMBS_KEEP_ONLY_SELECTED_FLOW
61+
key: CONTROLS_KEYS.CODE_CRUMBS_FILTER_FLOW
6262
},
6363
{
6464
name: 'line numbers',
@@ -74,16 +74,16 @@ const DefaultState = {
7474
}
7575
],
7676
checkedState: {
77-
[CONTROLS_KEYS.SOURCE]: true,
77+
[CONTROLS_KEYS.SOURCE_DIAGRAM_ON]: true,
7878
[CONTROLS_KEYS.DEPENDENCIES_SHOW_DIRECT_ONLY]: true,
7979
[CONTROLS_KEYS.CODE_CRUMBS_LINE_NUMBERS]: true,
80-
[CONTROLS_KEYS.CODE_CRUMBS_KEEP_ONLY_SELECTED_FLOW]: true
80+
[CONTROLS_KEYS.CODE_CRUMBS_FILTER_FLOW]: true
8181
},
8282
disabledState: {
83-
[CONTROLS_KEYS.CODE_CRUMBS_KEEP_ONLY_SELECTED_FLOW]: true
83+
[CONTROLS_KEYS.CODE_CRUMBS_FILTER_FLOW]: true
8484
},
8585
valuesState: {
86-
zoom: 1,
86+
diagramZoom: 1,
8787
selectedTabInSideBar: 'Code'
8888
}
8989
};
@@ -132,7 +132,7 @@ export default (state = DefaultState, action) => {
132132
...state,
133133
valuesState: {
134134
...state.valuesState,
135-
zoom: Math.round(action.payload * 10) / 10
135+
diagramZoom: Math.round(action.payload * 10) / 10
136136
}
137137
};
138138

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const getSwitches = state => state.viewSwitches.switches;
2+
3+
export const getCheckedState = state => state.viewSwitches.checkedState;
4+
export const getValuesState = state => state.viewSwitches.valuesState;
5+
export const getDisabledState = state => state.viewSwitches.disabledState;

‎src/public/js/components/controls/ZoomControl/index.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Button, Icon } from 'antd';
55
const ButtonGroup = Button.Group;
66

77
import { setZoom } from 'components/controls/ViewSwitches/store/actions';
8+
import { getValuesState } from 'components/controls/ViewSwitches/store/selectors';
89
import './index.scss';
910

1011
const ZoomControl = ({ zoom, setZoom }) => {
@@ -21,10 +22,10 @@ const ZoomControl = ({ zoom, setZoom }) => {
2122
};
2223

2324
const mapStateToProps = state => {
24-
const { valuesState } = state.viewSwitches;
25+
const { diagramZoom } = getValuesState(state);
2526

2627
return {
27-
zoom: valuesState.zoom
28+
zoom: diagramZoom
2829
};
2930
};
3031

‎src/public/js/components/dataBus/store/actions/index.js‎

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@ import {
44
getCodeCrumbsMapForCurrentCcFlow
55
} from 'utils/treeLayout';
66
import { fetchFile } from 'utils/connection';
7+
import { ACTIONS as VIEW_SWITCHES_ACTIONS } from 'components/controls/ViewSwitches/store/constants';
8+
import {
9+
getCheckedState,
10+
getValuesState,
11+
getDisabledState
12+
} from 'components/controls/ViewSwitches/store/selectors';
713

814
import { getFoldersForPaths, downloadObjectAsJsonFile, uploadFileAsObject } from './utils';
9-
import { ACTIONS } from '../constants';
10-
import { ACTIONS as VIEW_SWITCHES_ACTIONS } from 'components/controls/ViewSwitches/store/constants';
15+
import { ACTIONS, DEFAULT_NAMESPACE } from '../constants';
16+
import {
17+
getSource,
18+
getSourceUserChoice,
19+
getCodeCrumbsUserChoice,
20+
getDependenciesUserChoice
21+
} from '../selectors';
1122

1223
export const setInitialSourceData = payload => ({
1324
type: ACTIONS.SET_INITIAL_SOURCE_DATA,
@@ -48,7 +59,7 @@ export const selectCodeCrumb = (fileNode, codeCrumb) => ({
4859

4960
export const setDependenciesEntryPoint = fileNode => (dispatch, getState) => {
5061
const state = getState();
51-
const { dependenciesShowDirectOnly } = state.viewSwitches.checkedState;
62+
const { dependenciesShowDirectOnly } = getCheckedState(state);
5263

5364
return dispatch({
5465
type: ACTIONS.SET_DEPENDENCIES_ENTRY_POINT,
@@ -78,7 +89,7 @@ export const selectDependencyEdge = (target, sources, groupName) => dispatch =>
7889
export const selectCodeCrumbedFlow = flow => (dispatch, getState) => {
7990
const state = getState();
8091

81-
const { selectedCrumbedFlowKey, codeCrumbedFlowsMap } = state.dataBus;
92+
const { selectedCrumbedFlowKey, codeCrumbedFlowsMap } = getCodeCrumbsUserChoice(state);
8293
const firstFlow = Object.keys(codeCrumbedFlowsMap || {})[0];
8394

8495
dispatch({
@@ -89,20 +100,17 @@ export const selectCodeCrumbedFlow = flow => (dispatch, getState) => {
89100

90101
export const calcFilesTreeLayoutNodes = () => (dispatch, getState) => {
91102
const state = getState();
92-
const {
93-
sourceTree,
94-
openedFolders,
95-
activeItemsMap,
96-
codeCrumbedFlowsMap,
97-
selectedCrumbedFlowKey,
98-
filesMap
99-
} = state.dataBus;
100-
const { checkedState } = state.viewSwitches;
103+
104+
const { sourceTree, filesMap } = getSource(state);
105+
const { openedFolders, activeItemsMap } = getSourceUserChoice(state);
106+
const { codeCrumbedFlowsMap, selectedCrumbedFlowKey } = getCodeCrumbsUserChoice(state);
107+
108+
const { codeCrumbsDiagramOn, codeCrumbsMinimize, codeCrumbsFilterFlow } = getCheckedState(state);
101109

102110
if (!sourceTree) return;
103111

104112
let activeCodeCrumbs = undefined;
105-
if (checkedState.codeCrumbsKeepOnlySelectedFlow && codeCrumbedFlowsMap[selectedCrumbedFlowKey]) {
113+
if (codeCrumbsFilterFlow && codeCrumbedFlowsMap[selectedCrumbedFlowKey]) {
106114
activeCodeCrumbs = getCodeCrumbsMapForCurrentCcFlow({
107115
codeCrumbedFlowsMap,
108116
selectedCrumbedFlowKey,
@@ -113,7 +121,7 @@ export const calcFilesTreeLayoutNodes = () => (dispatch, getState) => {
113121
return dispatch({
114122
type: ACTIONS.UPDATE_FILES_TREE_LAYOUT_NODES,
115123
payload: getTreeLayout(sourceTree, {
116-
includeFileChildren: checkedState.codeCrumbs && !checkedState.codeCrumbsMinimize,
124+
includeFileChildren: codeCrumbsDiagramOn && !codeCrumbsMinimize,
117125
openedFolders,
118126
activeItemsMap,
119127
activeCodeCrumbs
@@ -123,13 +131,13 @@ export const calcFilesTreeLayoutNodes = () => (dispatch, getState) => {
123131

124132
export const setActiveItems = (filesList, foldersMap = {}) => (dispatch, getState) => {
125133
const state = getState();
134+
const { activeItemsMap } = getSourceUserChoice(state);
135+
const { sourceKeepOnlyActiveItems } = getCheckedState(state);
126136

127137
return dispatch({
128138
type: ACTIONS.SET_ACTIVE_ITEMS,
129139
payload: {
130-
...(!state.viewSwitches.checkedState.sourceKeepOnlyActiveItems
131-
? state.dataBus.activeItemsMap
132-
: {}),
140+
...(!sourceKeepOnlyActiveItems ? activeItemsMap : {}),
133141
...filesList.reduce((acc, item) => {
134142
//TODO:move this to util!
135143
acc[item] = true;
@@ -144,27 +152,23 @@ export const setActiveItems = (filesList, foldersMap = {}) => (dispatch, getStat
144152
export const updateFoldersByActiveChildren = () => (dispatch, getState) => {
145153
const state = getState();
146154

147-
const {
148-
filesMap,
149-
openedFolders,
150-
selectedNode,
151-
codeCrumbedFlowsMap,
152-
selectedCrumbedFlowKey
153-
} = state.dataBus;
155+
const { filesMap } = getSource(state);
156+
const { openedFolders, selectedNode } = getSourceUserChoice(state);
157+
const { codeCrumbedFlowsMap, selectedCrumbedFlowKey } = getCodeCrumbsUserChoice(state);
154158

155159
const {
156-
dependencies,
157-
codeCrumbs,
160+
dependenciesDiagramOn,
161+
codeCrumbsDiagramOn,
158162
sourceKeepOnlyActiveItems,
159-
codeCrumbsKeepOnlySelectedFlow
160-
} = state.viewSwitches.checkedState;
163+
codeCrumbsFilterFlow
164+
} = getCheckedState(state);
161165

162-
const depFilePaths = dependencies ? Object.keys(selectedNode.dependencies || {}) : [];
163-
let ccFilePaths = codeCrumbs
166+
const depFilePaths = dependenciesDiagramOn ? Object.keys(selectedNode.dependencies || {}) : [];
167+
let ccFilePaths = codeCrumbsDiagramOn
164168
? Object.keys(filesMap).filter(path => filesMap[path].hasCodecrumbs)
165169
: [];
166170

167-
if (codeCrumbsKeepOnlySelectedFlow && codeCrumbedFlowsMap[selectedCrumbedFlowKey]) {
171+
if (codeCrumbsFilterFlow && codeCrumbedFlowsMap[selectedCrumbedFlowKey]) {
168172
const currentFlowFiles = getFilesForCurrentCcFlow({
169173
codeCrumbedFlowsMap,
170174
selectedCrumbedFlowKey,
@@ -191,20 +195,26 @@ export const updateFoldersByActiveChildren = () => (dispatch, getState) => {
191195

192196
// TODO: group and move actions to different files
193197
export const downloadStore = () => (dispatch, getState) => {
194-
const { viewSwitches, dataBus } = getState();
198+
const state = getState();
199+
200+
const { sourceTree, filesMap, foldersMap } = getSource(state);
201+
const { dependenciesRootEntryName } = getDependenciesUserChoice(state);
202+
const { codeCrumbedFlowsMap } = getCodeCrumbsUserChoice(state);
195203

196204
const partialStateToSave = {
197205
viewSwitches: {
198-
checkedState: viewSwitches.checkedState,
199-
disabledState: viewSwitches.disabledState,
200-
valuesState: viewSwitches.valuesState
206+
checkedState: getCheckedState(state),
207+
valuesState: getValuesState(state),
208+
disabledState: getDisabledState(state)
201209
},
202210
dataBus: {
203-
sourceTree: dataBus.sourceTree,
204-
filesMap: dataBus.filesMap,
205-
foldersMap: dataBus.foldersMap,
206-
codeCrumbedFlowsMap: dataBus.codeCrumbedFlowsMap,
207-
dependenciesRootEntryName: dataBus.dependenciesRootEntryName
211+
[DEFAULT_NAMESPACE]: {
212+
sourceTree,
213+
filesMap,
214+
foldersMap,
215+
codeCrumbedFlowsMap,
216+
dependenciesRootEntryName
217+
}
208218
}
209219
};
210220

@@ -214,6 +224,6 @@ export const downloadStore = () => (dispatch, getState) => {
214224
export const uploadStore = file => dispatch => {
215225
uploadFileAsObject(file).then(object => {
216226
dispatch({ type: VIEW_SWITCHES_ACTIONS.SET_FULL_STATE, payload: object.data.viewSwitches });
217-
dispatch(setInitialSourceData(object.data.dataBus));
227+
dispatch(setInitialSourceData(object.data.dataBus[DEFAULT_NAMESPACE]));
218228
});
219-
};
229+
};

0 commit comments

Comments
 (0)