1
1
import React , { createContext , useCallback , useState , useRef } from 'react'
2
- import { merge } from 'lodash'
2
+ import setOrDeleteByMutatePath from '../lib/set-or-delete-by-mutate-path'
3
+
3
4
export const DeliberationContext = createContext ( { } )
4
5
export default DeliberationContext
5
6
@@ -12,19 +13,9 @@ export function DeliberationContextProvider(props) {
12
13
const upsert = useCallback (
13
14
obj => {
14
15
setData ( data => {
15
- // if something changes in a top level prop, the top level ref has to be changed so it will cause a rerender
16
- const newData = { ...data }
17
- Object . keys ( obj ) . forEach ( key => {
18
- if ( typeof obj [ key ] !== 'object' ) {
19
- newData [ key ] = obj [ key ]
20
- } else {
21
- const newProp = Array . isArray ( obj [ key ] ) ? [ ] : { }
22
- merge ( newProp , data [ key ] , obj [ key ] )
23
- newData [ key ] = newProp
24
- }
25
- } )
26
- deriveReducedPointList ( newData , local )
27
- return newData // spread because we need to return a new reference
16
+ let newData = setOrDeleteByMutatePath ( data , obj )
17
+ newData = deriveReducedPointList ( newData , local )
18
+ return newData // is a new ref is there were changes above, or may be the original ref if no changes
28
19
} )
29
20
} ,
30
21
[ setData ]
@@ -53,10 +44,7 @@ export function deriveReducedPointList(data, local) {
53
44
const { pointById, groupIdsLists } = data
54
45
if ( ! pointById || ! groupIdsLists ) return data
55
46
if ( local . pointById === pointById && local . groupIdsList === groupIdsLists ) return data // nothing to update
56
- const reducedPointTable = Object . entries ( pointById ) . reduce (
57
- ( reducedPointTable , [ id , point ] ) => ( ( reducedPointTable [ id ] = { point } ) , reducedPointTable ) ,
58
- { }
59
- )
47
+ const reducedPointTable = Object . entries ( pointById ) . reduce ( ( reducedPointTable , [ id , point ] ) => ( ( reducedPointTable [ id ] = { point } ) , reducedPointTable ) , { } )
60
48
let updated = false
61
49
for ( const [ firstId , ...groupIds ] of groupIdsLists ) {
62
50
reducedPointTable [ firstId ] . group = groupIds . map ( id => reducedPointTable [ id ] . point )
@@ -66,17 +54,15 @@ export function deriveReducedPointList(data, local) {
66
54
// then copy them over so they are unchanged
67
55
for ( const pointWithGroup of data . reducedPointList ) {
68
56
const ptid = pointWithGroup . point . _id
69
- if (
70
- reducedPointTable [ ptid ] ?. point === pointWithGroup . point &&
71
- aEqual ( reducedPointTable [ ptid ] ?. group , pointWithGroup . group )
72
- )
73
- reducedPointTable [ ptid ] = pointWithGroup // if contentss are unchanged - unchange the ref
57
+ if ( reducedPointTable [ ptid ] ?. point === pointWithGroup . point && aEqual ( reducedPointTable [ ptid ] ?. group , pointWithGroup . group ) ) reducedPointTable [ ptid ] = pointWithGroup // if contentss are unchanged - unchange the ref
74
58
else updated = true
75
59
}
76
60
const newReducedPointList = Object . values ( reducedPointTable )
77
61
local . pointById = pointById
78
62
local . groupIdsList = groupIdsLists
79
- if ( ! ( newReducedPointList . length === data . reducedPointList . length && ! updated ) )
63
+ if ( ! ( newReducedPointList . length === data . reducedPointList . length && ! updated ) ) {
80
64
data . reducedPointList = newReducedPointList
65
+ return { ...data }
66
+ }
81
67
return data
82
68
}
0 commit comments