Skip to content

Commit 000362c

Browse files
committed
reviewPointsToContext
1 parent e534cac commit 000362c

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

stories/review-point-list.stories.jsx

+29-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// https://github.com/EnCiv/civil-pursuit/issues/61
22

3-
import React, { useEffect, useContext } from 'react'
3+
import React, { useEffect, useContext, useState } from 'react'
44
import DeliberationContext from '../app/components/deliberation-context'
55
import { expect } from '@storybook/jest'
66
import RerankStep, { Rerank } from '../app/components/steps/rerank'
@@ -390,14 +390,40 @@ export const onDoneIsCalledAfterUserChangesRank = {
390390
})
391391
},
392392
}
393+
function reviewPointsToContext(reviewPoints) {
394+
const cn = {
395+
...reviewPoints.reduce(
396+
(cn, rp) => {
397+
console.info('cn, rp', cn, rp)
398+
// context, reviewPoint
399+
cn.pointById[rp.point._id] = rp.point
400+
rp.mosts && rp.mosts.forEach(p => (cn.topWhyByParentId[p.parentId] = p))
401+
rp.leasts && rp.leasts.forEach(p => (cn.topWhyByParentId[p.parentId] = p))
402+
rp.rank && (cn.postRankByParentId[rp.rank.parentId] = rp.rank)
403+
return cn
404+
},
405+
{ pointById: {}, topWhyByParentId: {}, postRankByParentId: {} }
406+
),
407+
}
408+
return cn
409+
}
393410

394411
export const rerankStepWithPartialInitialData = {
395412
args: {
396413
reviewPoints: [reviewPoint4, reviewPoint2, reviewPoint3],
397414
discussionId,
398415
round,
399416
},
400-
render: args => <RerankStep {...args} />,
417+
render: args => {
418+
// brute force set/mutate the initial value of the context data
419+
const { data = {}, upsert } = useContext(DeliberationContext)
420+
useState(() => {
421+
// execute this code once, before the component is initally rendered
422+
const cn = reviewPointsToContext(args.reviewPoints)
423+
setTimeout(() => upsert(cn))
424+
})
425+
return <RerankStep {...args} />
426+
},
401427
play: async ({ canvasElement }) => {
402428
const canvas = within(canvasElement)
403429
const categories = canvas.getAllByText('Neutral')
@@ -434,20 +460,7 @@ export const rerankStepWithTopDownUpdate = {
434460
useEffect(() => {
435461
console.info('reviewPoints', reviewPoints)
436462
setTimeout(() => {
437-
const cn = {
438-
...reviewPoints.reduce(
439-
(cn, rp) => {
440-
console.info('cn, rp', cn, rp)
441-
// context, reviewPoint
442-
cn.pointById[rp.point._id] = rp.point
443-
rp.mosts && rp.mosts.forEach(p => (cn.topWhyByParentId[p.parentId] = p))
444-
rp.leasts && rp.leasts.forEach(p => (cn.topWhyByParentId[p.parentId] = p))
445-
rp.rank && (cn.postRankByParentId[rp.rank.parentId] = rp.rank)
446-
return cn
447-
},
448-
{ pointById: {}, topWhyByParentId: {}, postRankByParentId: {} }
449-
),
450-
}
463+
const cn = reviewPointsToContext(reviewPoints)
451464
console.info('cn', cn)
452465
upsert(cn)
453466
}, 2000)

0 commit comments

Comments
 (0)