Skip to content

Commit 68817cd

Browse files
authored
Merge pull request #73 from oslabs-beta/rydang/reducer
using new library immer for reducer and fixed new tab bug
2 parents 9606faa + 7a21aee commit 68817cd

File tree

3 files changed

+41
-58
lines changed

3 files changed

+41
-58
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@
5252
},
5353
"dependencies": {
5454
"d3": "^3.5.17",
55+
"immer": "^3.2.0",
5556
"jsondiffpatch": "^0.3.11",
5657
"prop-types": "^15.7.2",
5758
"rc-slider": "^8.6.13",
5859
"rc-tooltip": "^3.7.3",
59-
"react-html-parser": "^2.0.2",
6060
"react": "^16.9.0",
6161
"react-dom": "^16.9.0",
62+
"react-html-parser": "^2.0.2",
6263
"react-json-tree": "^0.11.2",
6364
"react-router-dom": "^5.0.1",
6465
"react-select": "^3.0.4"

src/app/reducers/mainReducer.js

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
/* eslint-disable no-param-reassign */
2+
import produce from 'immer';
13
import * as types from '../constants/actionTypes';
24

3-
export default function mainReducer(state, action) {
5+
export default (state, action) => produce(state, (draft) => {
46
const {
57
port, currentTab, tabs,
6-
} = state;
8+
} = draft;
79
const {
810
snapshots, mode, intervalId, viewIndex, sliderIndex,
911
} = (tabs[currentTab] || {});
@@ -18,13 +20,8 @@ export default function mainReducer(state, action) {
1820

1921
tabs[currentTab].sliderIndex = newIndex;
2022
tabs[currentTab].playing = false;
21-
22-
return {
23-
...state,
24-
tabs,
25-
};
2623
}
27-
return state;
24+
break;
2825
}
2926
case types.MOVE_FORWARD: {
3027
if (sliderIndex < snapshots.length - 1) {
@@ -39,50 +36,36 @@ export default function mainReducer(state, action) {
3936
clearInterval(intervalId);
4037
tabs[currentTab].playing = false;
4138
}
42-
// message is coming from the setInterval
43-
return {
44-
...state,
45-
tabs,
46-
};
4739
}
48-
return state;
40+
break;
4941
}
5042
case types.CHANGE_VIEW: {
5143
// unselect view if same index was selected
5244
if (viewIndex === action.payload) tabs[currentTab].viewIndex = -1;
5345
else tabs[currentTab].viewIndex = action.payload;
54-
55-
return {
56-
...state,
57-
tabs,
58-
};
46+
break;
5947
}
6048
case types.CHANGE_SLIDER: {
6149
port.postMessage({ action: 'jumpToSnap', payload: snapshots[action.payload], tabId: currentTab });
6250
tabs[currentTab].sliderIndex = action.payload;
63-
return { ...state, tabs };
51+
break;
6452
}
6553
case types.EMPTY: {
6654
port.postMessage({ action: 'emptySnap', tabId: currentTab });
6755
tabs[currentTab].sliderIndex = 0;
6856
tabs[currentTab].viewIndex = -1;
6957
tabs[currentTab].playing = false;
7058
tabs[currentTab].snapshots.splice(1);
71-
return {
72-
...state,
73-
tabs,
74-
};
59+
break;
7560
}
7661
case types.SET_PORT: {
77-
return { ...state, port: action.payload };
62+
draft.port = action.payload;
63+
break;
7864
}
7965
case types.IMPORT: {
8066
port.postMessage({ action: 'import', payload: action.payload, tabId: currentTab });
8167
tabs[currentTab].snapshots = action.payload;
82-
return {
83-
...state,
84-
tabs,
85-
};
68+
break;
8669
}
8770
case types.TOGGLE_MODE: {
8871
mode[action.payload] = !mode[action.payload];
@@ -101,66 +84,60 @@ export default function mainReducer(state, action) {
10184
default:
10285
}
10386
port.postMessage({ action: actionText, payload: newMode, tabId: currentTab });
104-
return { ...state, tabs };
87+
break;
10588
}
10689
case types.PAUSE: {
10790
clearInterval(intervalId);
10891
tabs[currentTab].playing = false;
10992
tabs[currentTab].intervalId = null;
110-
return { ...state, tabs };
93+
break;
11194
}
11295
case types.PLAY: {
11396
tabs[currentTab].playing = true;
11497
tabs[currentTab].intervalId = action.payload;
115-
return {
116-
...state,
117-
tabs,
118-
};
98+
break;
11999
}
120100
case types.INITIAL_CONNECT: {
121101
const { payload } = action;
122102
Object.keys(payload).forEach((tab) => {
123-
payload[tab] = {
124-
...payload[tab],
125-
sliderIndex: 0,
126-
viewIndex: -1,
127-
intervalId: null,
128-
playing: false,
129-
};
103+
// check if tab exists in memory
104+
if (!tabs[tab]) {
105+
// add new tab
106+
tabs[tab] = {
107+
...payload[tab],
108+
sliderIndex: 0,
109+
viewIndex: -1,
110+
intervalId: null,
111+
playing: false,
112+
};
113+
}
130114
});
131115

132116
// only set first tab if current tab is non existent
133117
const firstTab = parseInt(Object.keys(payload)[0], 10);
134-
return {
135-
...state,
136-
currentTab: (currentTab === null) ? firstTab : currentTab,
137-
tabs: payload,
138-
};
118+
draft.currentTab = (currentTab === null) ? firstTab : currentTab;
119+
120+
break;
139121
}
140122
case types.NEW_SNAPSHOTS: {
141123
const { payload } = action;
142124

143125
Object.keys(payload).forEach((tab) => {
144126
const { snapshots: newSnaps } = payload[tab];
145-
payload[tab] = {
127+
tabs[tab] = {
146128
...tabs[tab],
147129
...payload[tab],
148130
sliderIndex: newSnaps.length - 1,
149131
};
150132
});
151133

152-
return {
153-
...state,
154-
tabs: payload,
155-
};
134+
break;
156135
}
157136
case types.SET_TAB: {
158-
return {
159-
...state,
160-
currentTab: action.payload,
161-
};
137+
draft.currentTab = action.paylod;
138+
break;
162139
}
163140
default:
164141
throw new Error(`nonexistent action: ${action.type}`);
165142
}
166-
}
143+
});

0 commit comments

Comments
 (0)