Skip to content

Commit 7a0c98a

Browse files
authored
Fixed multiple updates with same id in updateMany (#621)
* Fixed multiple updates with same id in updateMany * Removed optional chaining syntax
1 parent 9a410e7 commit 7a0c98a

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

src/entities/fixtures/book.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface BookModel {
22
id: string
33
title: string
4+
author?: string
45
}
56

67
export const AClockworkOrange: BookModel = Object.freeze({
@@ -17,3 +18,9 @@ export const TheGreatGatsby: BookModel = Object.freeze({
1718
id: 'tgg',
1819
title: 'The Great Gatsby'
1920
})
21+
22+
export const TheHobbit: BookModel = Object.freeze({
23+
id: 'th',
24+
title: 'The Hobbit',
25+
author: 'J. R. R. Tolkien'
26+
})

src/entities/unsorted_state_adapter.test.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
BookModel,
55
TheGreatGatsby,
66
AClockworkOrange,
7-
AnimalFarm
7+
AnimalFarm,
8+
TheHobbit
89
} from './fixtures/book'
910
import { createNextState } from '..'
1011

@@ -466,12 +467,20 @@ describe('Unsorted State Adapter', () => {
466467
test('updateMany', () => {
467468
const firstChange = { title: 'First Change' }
468469
const secondChange = { title: 'Second Change' }
469-
const withMany = adapter.setAll(state, [TheGreatGatsby, AClockworkOrange])
470+
const thirdChange = { title: 'Third Change' }
471+
const fourthChange = { author: 'Fourth Change' }
472+
const withMany = adapter.setAll(state, [
473+
TheGreatGatsby,
474+
AClockworkOrange,
475+
TheHobbit
476+
])
470477

471478
const result = createNextState(withMany, draft => {
472479
adapter.updateMany(draft, [
473-
{ id: TheGreatGatsby.id, changes: firstChange },
474-
{ id: AClockworkOrange.id, changes: secondChange }
480+
{ id: TheHobbit.id, changes: firstChange },
481+
{ id: TheGreatGatsby.id, changes: secondChange },
482+
{ id: AClockworkOrange.id, changes: thirdChange },
483+
{ id: TheHobbit.id, changes: fourthChange }
475484
])
476485
})
477486

@@ -480,16 +489,22 @@ describe('Unsorted State Adapter', () => {
480489
"entities": Object {
481490
"aco": Object {
482491
"id": "aco",
483-
"title": "Second Change",
492+
"title": "Third Change",
484493
},
485494
"tgg": Object {
486495
"id": "tgg",
496+
"title": "Second Change",
497+
},
498+
"th": Object {
499+
"author": "Fourth Change",
500+
"id": "th",
487501
"title": "First Change",
488502
},
489503
},
490504
"ids": Array [
491505
"tgg",
492506
"aco",
507+
"th",
493508
],
494509
}
495510
`)

src/entities/unsorted_state_adapter.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function createUnsortedStateAdapter<T>(
1616
): EntityStateAdapter<T> {
1717
type R = EntityState<T>
1818

19-
function addOneMutably(entity: T, state: EntityState<T>): void {
19+
function addOneMutably(entity: T, state: R): void {
2020
const key = selectIdValue(entity, selectId)
2121

2222
if (key in state.entities) {
@@ -108,10 +108,15 @@ export function createUnsortedStateAdapter<T>(
108108
if (update.id in state.entities) {
109109
// If there are multiple updates to one entity, merge them together
110110
updatesPerEntity[update.id] = {
111+
id: update.id,
111112
// Spreads ignore falsy values, so this works even if there isn't
112113
// an existing update already at this key
113-
...updatesPerEntity[update.id],
114-
...update
114+
changes: {
115+
...(updatesPerEntity[update.id]
116+
? updatesPerEntity[update.id].changes
117+
: null),
118+
...update.changes
119+
}
115120
}
116121
}
117122
})

0 commit comments

Comments
 (0)