Skip to content

Commit f874133

Browse files
committed
Fix total on addNewRow and dismissEditor
fixed total on removeRow
1 parent 0ecf836 commit f874133

File tree

2 files changed

+221
-9
lines changed

2 files changed

+221
-9
lines changed

src/reducers/actionHelpers/datasource.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,17 @@ export const dismissEditor = (state, { stateKey }) => {
7474
if (previousData
7575
&& previousProxy
7676
&& previousData.size > previousProxy.size) {
77-
previousTotal = previousProxy.size;
77+
previousTotal = previousTotal - 1;
7878
}
79-
8079
const record = state.get(stateKey);
8180

8281
if (record) {
8382
const updated = record.merge({
8483
data: previousProxy,
8584
proxy: previousProxy,
8685
currentRecords: previousProxy,
87-
total: previousTotal,
8886
isEditing: false,
87+
total: previousTotal,
8988
lastUpdate: generateLastUpdate()
9089
});
9190

@@ -96,16 +95,26 @@ export const dismissEditor = (state, { stateKey }) => {
9695
};
9796

9897
export const removeRow = (state, { stateKey, rowIndex }) => {
98+
const existingState = state.get(stateKey);
99+
const currentTotal = existingState.get('total');
100+
99101
const remainingRows = state
100102
.getIn([stateKey, 'data'])
101103
.remove(rowIndex || 0, 1);
102104

103105
const record = state.get(stateKey);
104106

107+
const updatedTotal = existingState
108+
&& currentTotal
109+
&& currentTotal !== 0
110+
? currentTotal - 1
111+
: 0;
112+
105113
const updated = record.merge({
106114
data: remainingRows,
107115
proxy: remainingRows,
108116
currentRecords: remainingRows,
117+
total: updatedTotal,
109118
lastUpdate: generateLastUpdate()
110119
});
111120

@@ -160,17 +169,23 @@ export const addNewRow = (state, { rowId, stateKey, rowIndex }) => {
160169
data = new List();
161170
}
162171

163-
rowIndex = rowIndex ===undefined
172+
const updatedTotal = existingState
173+
&& existingState.get('total')
174+
? existingState.get('total')
175+
: 0;
176+
177+
rowIndex = rowIndex === undefined
164178
? 0
165179
: rowIndex;
166180

167181
const newData = data.insert(rowIndex, newRow);
182+
168183
const updated = existingState.merge({
169184
data: newData,
170185
proxy: data,
171186
isEditing: true,
172-
lastUpdate: generateLastUpdate(),
173-
total: newData.size
187+
total: updatedTotal + 1,
188+
lastUpdate: generateLastUpdate()
174189
});
175190

176191
return state.setIn([stateKey], updated);
@@ -245,7 +260,7 @@ export const setTreeNodeVisibility = (state, {
245260
return state.setIn([stateKey], updated);
246261
};
247262

248-
export const saveRow = (state, { rowIndex, stateKey, values}) => {
263+
export const saveRow = (state, { rowIndex, stateKey, values }) => {
249264
const data = state
250265
.getIn([stateKey, 'data'])
251266
.set(rowIndex, fromJS(values));

test/reducers/components/datasource.test.js

Lines changed: 199 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,54 @@ describe('The grid dataSource reducer dissmissEditor func', () => {
223223
dataSource(inState, action)
224224
).toEqualImmutable(outState);
225225
});
226+
227+
it('Should decrement and maintain total if proxy less values than data',
228+
() => {
229+
const inState = fromJS({
230+
'test-grid': {
231+
data: [
232+
{ cell: 1 },
233+
{ cell: 2 },
234+
{ cell: 3 }
235+
236+
],
237+
proxy: [
238+
{ cell: 1 },
239+
{ cell: 2 }
240+
],
241+
total: 151
242+
}
243+
});
244+
245+
const outState = fromJS({
246+
'test-grid': {
247+
proxy: [
248+
{ cell: 1 },
249+
{ cell: 2 }
250+
],
251+
total: 150,
252+
data: [
253+
{ cell: 1 },
254+
{ cell: 2 }
255+
],
256+
currentRecords: [
257+
{ cell: 1 },
258+
{ cell: 2 }
259+
],
260+
isEditing: false,
261+
lastUpdate: 1
262+
}
263+
});
264+
265+
const action = {
266+
stateKey: 'test-grid',
267+
type: DISMISS_EDITOR
268+
};
269+
270+
expect(
271+
dataSource(inState, action)
272+
).toEqualImmutable(outState);
273+
});
226274
});
227275

228276
describe('The grid dataSource reducer removeRow func', () => {
@@ -260,7 +308,7 @@ describe('The grid dataSource reducer removeRow func', () => {
260308
currentRecords: [
261309
{ cell: 2 }
262310
],
263-
total: 2,
311+
total: 1,
264312
lastUpdate: 1
265313
}
266314
});
@@ -288,7 +336,7 @@ describe('The grid dataSource reducer removeRow func', () => {
288336
currentRecords: [
289337
{ cell: 1 }
290338
],
291-
total: 2,
339+
total: 1,
292340
lastUpdate: 1
293341
}
294342
});
@@ -305,6 +353,73 @@ describe('The grid dataSource reducer removeRow func', () => {
305353

306354
});
307355

356+
it('Should default total to 0 when inSate total is 0', () => {
357+
358+
const customInState = fromJS({
359+
'test-grid': {
360+
proxy: [],
361+
data: [],
362+
currentRecords: [],
363+
total: 0,
364+
lastUpdate: 1
365+
}
366+
});
367+
368+
const outState = fromJS({
369+
'test-grid': {
370+
proxy: [],
371+
data: [],
372+
currentRecords: [],
373+
total: 0,
374+
lastUpdate: 1
375+
}
376+
});
377+
378+
const action = {
379+
stateKey: 'test-grid',
380+
rowIndex: 1,
381+
type: REMOVE_ROW
382+
};
383+
384+
expect(
385+
dataSource(customInState, action)
386+
).toEqualImmutable(outState);
387+
388+
});
389+
390+
it('Should default total to 0 when total not provided', () => {
391+
392+
const customInState = fromJS({
393+
'test-grid': {
394+
proxy: [],
395+
data: [],
396+
currentRecords: [],
397+
lastUpdate: 1
398+
}
399+
});
400+
401+
const outState = fromJS({
402+
'test-grid': {
403+
proxy: [],
404+
data: [],
405+
currentRecords: [],
406+
total: 0,
407+
lastUpdate: 1
408+
}
409+
});
410+
411+
const action = {
412+
stateKey: 'test-grid',
413+
rowIndex: 1,
414+
type: REMOVE_ROW
415+
};
416+
417+
expect(
418+
dataSource(customInState, action)
419+
).toEqualImmutable(outState);
420+
421+
});
422+
308423
});
309424

310425
describe('The grid dataSource reducer addRow func', () => {
@@ -362,6 +477,88 @@ describe('The grid dataSource reducer addRow func', () => {
362477
).toEqualImmutable(outState);
363478
});
364479

480+
it('Should add a new row and maintain total if multiple pages', () => {
481+
const customInState = fromJS({
482+
'test-grid': {
483+
proxy: [
484+
{ cell: 1 },
485+
{ cell: 2 }
486+
],
487+
data: [
488+
{ cell: 1 },
489+
{ cell: 2 }
490+
],
491+
currentRecords: [
492+
{ cell: 1 },
493+
{ cell: 2 }
494+
],
495+
total: 150
496+
}
497+
});
498+
499+
const outState = fromJS({
500+
'test-grid': {
501+
proxy: [
502+
{ cell: 1 },
503+
{ cell: 2 }
504+
],
505+
data: [
506+
{ cell: '', _key: 'row-0' },
507+
{ cell: 1 },
508+
{ cell: 2 }
509+
],
510+
currentRecords: [
511+
{ cell: 1 },
512+
{ cell: 2 }
513+
],
514+
total: 151,
515+
isEditing: true,
516+
lastUpdate: 1
517+
}
518+
});
519+
520+
const action = {
521+
stateKey: 'test-grid',
522+
rowId: 'row-0',
523+
type: ADD_NEW_ROW
524+
};
525+
526+
expect(
527+
dataSource(customInState, action)
528+
).toEqualImmutable(outState);
529+
});
530+
531+
it('Should add a new row and + 1 to total when no total on inState', () => {
532+
const customInState = fromJS({
533+
'test-grid': {
534+
proxy: [],
535+
data: [],
536+
currentRecords: []
537+
}
538+
});
539+
540+
const outState = fromJS({
541+
'test-grid': {
542+
proxy: [],
543+
data: [{_key: 'row-0'}],
544+
currentRecords: [],
545+
total: 1,
546+
isEditing: true,
547+
lastUpdate: 1
548+
}
549+
});
550+
551+
const action = {
552+
stateKey: 'test-grid',
553+
rowId: 'row-0',
554+
type: ADD_NEW_ROW
555+
};
556+
557+
expect(
558+
dataSource(customInState, action)
559+
).toEqualImmutable(outState);
560+
});
561+
365562
it('Should add a new blank row if no rows have been established', () => {
366563
const innerState = fromJS({
367564
'test-grid': {

0 commit comments

Comments
 (0)