|
1 | 1 | // https://github.com/EnCiv/civil-pursuit/issues/61
|
2 | 2 |
|
3 |
| -import React, { useEffect, useContext } from 'react' |
| 3 | +import React, { useEffect, useContext, useState } from 'react' |
4 | 4 | import DeliberationContext from '../app/components/deliberation-context'
|
5 | 5 | import { expect } from '@storybook/jest'
|
6 | 6 | import RerankStep, { Rerank } from '../app/components/steps/rerank'
|
@@ -390,14 +390,40 @@ export const onDoneIsCalledAfterUserChangesRank = {
|
390 | 390 | })
|
391 | 391 | },
|
392 | 392 | }
|
| 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 | +} |
393 | 410 |
|
394 | 411 | export const rerankStepWithPartialInitialData = {
|
395 | 412 | args: {
|
396 | 413 | reviewPoints: [reviewPoint4, reviewPoint2, reviewPoint3],
|
397 | 414 | discussionId,
|
398 | 415 | round,
|
399 | 416 | },
|
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 | + }, |
401 | 427 | play: async ({ canvasElement }) => {
|
402 | 428 | const canvas = within(canvasElement)
|
403 | 429 | const categories = canvas.getAllByText('Neutral')
|
@@ -434,20 +460,7 @@ export const rerankStepWithTopDownUpdate = {
|
434 | 460 | useEffect(() => {
|
435 | 461 | console.info('reviewPoints', reviewPoints)
|
436 | 462 | 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) |
451 | 464 | console.info('cn', cn)
|
452 | 465 | upsert(cn)
|
453 | 466 | }, 2000)
|
|
0 commit comments