Skip to content
This repository was archived by the owner on Apr 1, 2021. It is now read-only.

Commit 8633908

Browse files
authored
Merge pull request #37 from canalplus/fix_current
Fix current
2 parents 3a41f05 + d91aabc commit 8633908

4 files changed

Lines changed: 61 additions & 16 deletions

File tree

dev/dev.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const store = createStore(combineReducers({
1919

2020
keysInit({store: store});
2121

22-
const PureMosaic = ({binder1, binder2}) => {
22+
const PureMosaic = ({binder1, binder2, selectedId}) => {
2323
const selectedId1 = binder1.selectedId;
2424
const active1 = binder1.active;
2525
const selectedId2 = binder2.selectedId;
@@ -55,6 +55,7 @@ const PureMosaic = ({binder1, binder2}) => {
5555

5656
const Mosaic = connect(state => {
5757
return {
58+
selectedId: state['@@keys'].current.selectedId,
5859
binder1: state['@@keys'].getBinder('binder1'),
5960
binder2: state['@@keys'].getBinder('binder2'),
6061
};

src/redux/actions.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,30 @@ export function clone(obj) {
2525
return cloneObject;
2626
}
2727

28+
export function bindersKeys(newState) {
29+
const keys = Object.keys(newState);
30+
keys.splice(keys.indexOf('current'), 1);
31+
return keys;
32+
}
33+
2834
export function _activeKeyBinder(binderId, id, memory = false) {
2935
if (globalStore.dispatch) {
3036
const newState = clone(globalStore.getState()[NAME]);
31-
for (const key of Object.keys(newState)) {
37+
for (const key of bindersKeys(newState)) {
3238
newState[key].active = false;
3339
}
34-
if (Object.keys(newState).some(key => key === binderId)) {
40+
if (bindersKeys(newState).some(key => key === binderId)) {
3541
newState[binderId].active = true;
3642
if (!memory) {
3743
newState[binderId].selectedId =
3844
newState[binderId].elements && newState[binderId].elements[0].id;
3945
} else {
4046
newState[binderId].selectedId = id || newState[binderId].selectedId;
4147
}
42-
newState.selectedId = newState[binderId].selectedId;
43-
newState.currentStrapeId = binderId;
48+
newState.current = {
49+
selectedId: newState[binderId].selectedId,
50+
binderId: binderId,
51+
};
4452
globalStore.dispatch({
4553
type: ACTIVE_KEYBINDER,
4654
state: newState,
@@ -52,10 +60,16 @@ export function _activeKeyBinder(binderId, id, memory = false) {
5260
export function addKeyBinderToStore(binderId, active) {
5361
if (globalStore.dispatch) {
5462
const newState = clone(globalStore.getState()[NAME]);
55-
if (!Object.keys(newState).some(key => key === binderId)) {
63+
if (!bindersKeys(newState).some(key => key === binderId)) {
5664
newState[binderId] = {};
5765
newState[binderId].id = binderId;
5866
newState[binderId].active = active;
67+
if (active) {
68+
newState.current = {
69+
selectedId: newState[binderId].elements && newState[binderId].elements[0].id,
70+
binderId: binderId,
71+
};
72+
}
5973
globalStore.dispatch({
6074
type: ADD_KEYBINDER_TO_STORE,
6175
state: newState,
@@ -67,6 +81,12 @@ export function addKeyBinderToStore(binderId, active) {
6781
export function _updateBinderState(binderId, binderState) {
6882
if (globalStore.dispatch) {
6983
const newState = clone(globalStore.getState()[NAME]);
84+
if (newState[binderId].active) {
85+
newState.current = {
86+
selectedId: binderState.selectedId,
87+
binderId: binderId,
88+
};
89+
}
7090
newState[binderId] = {...newState[binderId], ...binderState};
7191
globalStore.dispatch({
7292
type: UPDATE_BINDER_STATE,
@@ -129,7 +149,7 @@ export function updateSelectedId(binderId, selectedId, marginLeft) {
129149
const newState = clone(globalStore.getState()[NAME]);
130150
newState[binderId].selectedId = selectedId;
131151
newState[binderId].marginLeft = marginLeft;
132-
newState.selectedId = selectedId;
152+
newState.current.selectedId = selectedId;
133153
globalStore.dispatch({
134154
type: UPDATE_SELECTED_KEY,
135155
state: newState,

src/redux/reducer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import {
66
} from './actions';
77

88
const initialKeysSate = {
9-
selectedId: null,
10-
currentStrapeId: null,
9+
current: {
10+
selectedId: null,
11+
binderId: null,
12+
},
1113
getBinder: function(binderId) {
1214
return this[binderId] || {marginLeft: 0};
1315
},

test/redux/actions.spec.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe('redux/actions.js', () => {
3232
sinon.test(function() {
3333
const store = createStore((state = {
3434
'@@keys': {
35+
current: {},
3536
binderId: {active: false, elements: [{id: '1'}]},
3637
binderId2: {active: true, elements: [{id: '2'}]},
3738
},
@@ -45,8 +46,7 @@ describe('redux/actions.js', () => {
4546
state: {
4647
binderId: {active: true, elements: {0: {id: '1'}}, selectedId: '1'},
4748
binderId2: {active: false, elements: {0: {id: '2'}}},
48-
currentStrapeId: 'binderId',
49-
selectedId: '1',
49+
current: {binderId: 'binderId', selectedId: '1'},
5050
},
5151
});
5252
actions._activeKeyBinder('binderId');
@@ -66,6 +66,7 @@ describe('redux/actions.js', () => {
6666
it('should keep in memory the selected Id when memory is true', sinon.test(function() {
6767
const store = createStore((state = {
6868
'@@keys': {
69+
current: {},
6970
binderId: {active: false, selectedId: '2', elements: [{id: '1'}]},
7071
binderId2: {active: true, selectedId: '3'},
7172
},
@@ -79,8 +80,7 @@ describe('redux/actions.js', () => {
7980
state: {
8081
binderId: {active: true, elements: {0: {id: '1'}}, selectedId: '2'},
8182
binderId2: {active: false, selectedId: '3'},
82-
currentStrapeId: 'binderId',
83-
selectedId: '2',
83+
current: {selectedId: '2', binderId: 'binderId'},
8484
},
8585
});
8686
actions._activeKeyBinder('binderId', null, true);
@@ -90,6 +90,7 @@ describe('redux/actions.js', () => {
9090
it('should add new binder to store if not exists yet', sinon.test(function() {
9191
const store = createStore((state = {
9292
'@@keys': {
93+
current: {},
9394
binderId: {active: false},
9495
},
9596
}) => state);
@@ -99,13 +100,18 @@ describe('redux/actions.js', () => {
99100
.once()
100101
.withArgs({
101102
type: actions.ADD_KEYBINDER_TO_STORE,
102-
state: {binderId: {active: false}, binderId2: {active: false, id: 'binderId2'}},
103+
state: {
104+
binderId: {active: false},
105+
binderId2: {active: false, id: 'binderId2'},
106+
current: {},
107+
},
103108
});
104109
actions.addKeyBinderToStore('binderId2', false);
105110
}));
106111
it('should activated binder when active is true', sinon.test(function() {
107112
const store = createStore((state = {
108113
'@@keys': {
114+
current: {},
109115
binderId: {active: false},
110116
},
111117
}) => state);
@@ -115,13 +121,18 @@ describe('redux/actions.js', () => {
115121
.once()
116122
.withArgs({
117123
type: actions.ADD_KEYBINDER_TO_STORE,
118-
state: {binderId: {active: false}, binderId2: {active: true, id: 'binderId2'}},
124+
state: {
125+
binderId: {active: false},
126+
binderId2: {active: true, id: 'binderId2'},
127+
current: {selectedId: undefined, binderId: 'binderId2'},
128+
},
119129
});
120130
actions.addKeyBinderToStore('binderId2', true);
121131
}));
122132
it('should not dipatch if binderId already exists in state', sinon.test(function() {
123133
const store = createStore((state = {
124134
'@@keys': {
135+
current: {},
125136
binderId: {active: false},
126137
},
127138
}) => state);
@@ -136,6 +147,7 @@ describe('redux/actions.js', () => {
136147
it('should update margin and selectedId to state', sinon.test(function() {
137148
const store = createStore((state = {
138149
'@@keys': {
150+
current: {selectedId: null},
139151
binderId: {active: false, selectedId: 1, marginLeft: 0},
140152
},
141153
}) => state);
@@ -146,8 +158,8 @@ describe('redux/actions.js', () => {
146158
.withArgs({
147159
type: actions.UPDATE_SELECTED_KEY,
148160
state: {
149-
selectedId: 2,
150161
binderId: {active: false, selectedId: 2, marginLeft: 10},
162+
current: {selectedId: 2},
151163
},
152164
});
153165
actions.updateSelectedId('binderId', 2, 10);
@@ -194,4 +206,14 @@ describe('redux/actions.js', () => {
194206
startSpy.should.have.been.not.called;
195207
}));
196208
});
209+
describe('bindersKeys', () => {
210+
it('should delete current keys and return list of objet keys', () => {
211+
const object = {
212+
current: {},
213+
binder1: {},
214+
binder2: {},
215+
};
216+
actions.bindersKeys(object).should.eql(['binder1', 'binder2']);
217+
});
218+
});
197219
});

0 commit comments

Comments
 (0)