Skip to content

Commit 5e33184

Browse files
authored
Merge pull request #266 from EnCiv/compare-whys#200
Refactor Compare whys#200
2 parents f627a38 + e6104e8 commit 5e33184

14 files changed

+1381
-451
lines changed

app/components/compare-reasons.jsx

-88
This file was deleted.

app/components/deliberation-context.js

+10-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
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+
34
export const DeliberationContext = createContext({})
45
export default DeliberationContext
56

@@ -12,19 +13,9 @@ export function DeliberationContextProvider(props) {
1213
const upsert = useCallback(
1314
obj => {
1415
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
2819
})
2920
},
3021
[setData]
@@ -53,10 +44,7 @@ export function deriveReducedPointList(data, local) {
5344
const { pointById, groupIdsLists } = data
5445
if (!pointById || !groupIdsLists) return data
5546
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), {})
6048
let updated = false
6149
for (const [firstId, ...groupIds] of groupIdsLists) {
6250
reducedPointTable[firstId].group = groupIds.map(id => reducedPointTable[id].point)
@@ -66,17 +54,15 @@ export function deriveReducedPointList(data, local) {
6654
// then copy them over so they are unchanged
6755
for (const pointWithGroup of data.reducedPointList) {
6856
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
7458
else updated = true
7559
}
7660
const newReducedPointList = Object.values(reducedPointTable)
7761
local.pointById = pointById
7862
local.groupIdsList = groupIdsLists
79-
if (!(newReducedPointList.length === data.reducedPointList.length && !updated))
63+
if (!(newReducedPointList.length === data.reducedPointList.length && !updated)) {
8064
data.reducedPointList = newReducedPointList
65+
return { ...data }
66+
}
8167
return data
8268
}

0 commit comments

Comments
 (0)