Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/repo-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ jobs:
tests/integration/ballotQuestions.test.ts
tests/integration/backfillTestimonyScope.test.ts
tests/integration/conferences.test.ts
tests/integration/conferenceLetters.test.ts
components/auth/hooks.test.ts
components/db/useUpcoming.test.ts
components/db/testimony
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ prod-build.env
!*.secret
infra/dev/typesense-secrets.yml
infra/prod/typesense-secrets.yml
# Firebase extension secrets for the emulator
extensions/*.secret.local

.python-version
__pycache__/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jest.mock("../auth", () => ({
const mounted: string[] = []
jest.mock("../db", () => ({
useProfile: () => ({ profile: {} }),
useMember: () => ({ member: undefined }),
hasDraftChanged: () => false,
useConferenceTestimony: () => ({ items: [], loading: false }),
useEditConferenceTestimony: (
Expand Down
23 changes: 20 additions & 3 deletions components/conference/ConferenceDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { useAppDispatch } from "../hooks"
import { maple, siteUrl } from "../links"
import { useConferenceTop, usePinnedNavbar } from "./hooks"
import { InputContext } from "./input/context"
import { initialsOf } from "./input/accounts"
import { CosignForm, CosignStep } from "./input/CosignForm"
import { CosignLetter } from "./input/CosignLetter"
import { InputForm, Step } from "./input/InputForm"
Expand All @@ -44,7 +45,12 @@ import { Close } from "./input/Close"
import { X } from "./input/icons"
import { jumpToSection } from "./navbar"
import { Notice, PageColumn, Title } from "./styles"
import { conferenceName, ConferencePageProps, ConferenceSummary } from "./types"
import {
conferenceName,
ConferencePageProps,
ConferenceSummary,
ConfereeView
} from "./types"

/** The mockup's column, widened to 1320 once the rail sits beside it. */
const Page = styled(PageColumn)`
Expand Down Expand Up @@ -527,6 +533,7 @@ function Details({
<InputPanel
uid={user.uid}
conference={conference}
conferees={conferees}
subject={subject}
open={open}
width={width}
Expand All @@ -552,6 +559,7 @@ function Details({
function InputPanel({
uid,
conference,
conferees,
subject,
open,
width,
Expand All @@ -569,6 +577,7 @@ function InputPanel({
}: {
uid: string
conference: ConferenceSummary
conferees: ConfereeView[]
subject: string
open: boolean
width: number
Expand All @@ -585,6 +594,10 @@ function InputPanel({
onPosted: () => void
}) {
const { t } = useTranslation("conferences")
const initials = useMemo(
() => conferees.map(c => initialsOf(c.name)),
[conferees]
)
const edit = useEditConferenceTestimony(
uid,
conference.court,
Expand All @@ -600,7 +613,7 @@ function InputPanel({
const title =
cosigning && edit.publication
? "input.cosign_edit_title"
: !cosigning && step === "review"
: (cosigning ? cosignStep : step) === "review"
? "input.review_title"
: "input.panel_title"
return (
Expand All @@ -621,14 +634,18 @@ function InputPanel({
key={cosigning.id}
edit={edit}
parent={cosigning}
court={conference.court}
subject={subject}
conferees={initials}
step={cosignStep}
onStep={onCosignStep}
onCancel={onClose}
onPosted={onPosted}
/>
) : (
<InputForm
uid={uid}
court={conference.court}
conferees={initials}
edit={edit}
subject={subject}
step={step}
Expand Down
119 changes: 111 additions & 8 deletions components/conference/input/CosignForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,25 @@ jest.mock("next-i18next", () => ({
}))

jest.mock("components/auth", () => ({
useAuth: () => ({ claims: { role: "user" } })
useAuth: () => ({
claims: { role: "user" },
user: { email: "gina@example.org" }
})
}))
const mockProfile: { current: Record<string, unknown> } = { current: {} }
jest.mock("components/db", () => ({
useProfile: () => ({ profile: { fullName: "Gina K." } })
useProfile: () => ({ profile: mockProfile.current }),
useMember: () => ({ member: undefined })
}))

const legislators = {
representative: { id: "r1", name: "Rae Rep", district: "3rd Middlesex" },
senator: { id: "s1", name: "Sam Sen", district: "Middlesex and Suffolk" }
}
beforeEach(() => {
mockProfile.current = { fullName: "Gina K.", ...legislators }
})

const parent = {
id: "p1",
authorUid: "org",
Expand Down Expand Up @@ -58,6 +71,9 @@ const mount = (
<CosignForm
edit={edit}
parent={parent}
court={194}
subject="Phone-Free Schools"
conferees={["CC", "BF"]}
step={step}
onStep={more.onStep ?? jest.fn()}
onCancel={jest.fn()}
Expand Down Expand Up @@ -115,17 +131,104 @@ describe("CosignForm", () => {
const draft = { cosignOfId: "p1", content: "" }
const edit = editing({ draft } as Partial<UseEditConferenceTestimony>)
const onPosted = jest.fn()
mount(edit, "write", { onPosted })
press("input.cosign")
expect(edit.publishTestimony.execute).not.toHaveBeenCalled()
const onStep = jest.fn()
mount(edit, "write", { onPosted, onStep })
press("input.review")
expect(onStep).not.toHaveBeenCalled()

fireEvent.click(
screen.getByRole("checkbox", { name: "input.cosign_no_words_label" })
)
expect(screen.getByRole("textbox")).toBeDisabled()
await act(async () => press("input.cosign"))
expect(edit.publishTestimony.execute).toHaveBeenCalled()
expect(onPosted).toHaveBeenCalled()
press("input.review")
expect(onStep).toHaveBeenCalledWith("review")
expect(edit.publishTestimony.execute).not.toHaveBeenCalled()
})

describe("review", () => {
const draft = { cosignOfId: "p1", content: "My note." }

it("shows where the cosign goes and the letter as sent, and posts it", async () => {
const edit = editing({ draft } as Partial<UseEditConferenceTestimony>)
const onPosted = jest.fn()
mount(edit, "review", { onPosted })
expect(screen.getByText("review.goes_cosign")).toBeInTheDocument()
expect(
screen.getByText("Dear Representative Rae Rep and Senator Sam Sen,")
).toBeInTheDocument()
expect(
screen.getByText("I cosigned this input by Example Coalition on MAPLE:")
).toBeInTheDocument()
expect(screen.getByText("The letter.")).toBeInTheDocument()
// The reader is copied, and replies go to them.
expect(screen.getAllByText("gina@example.org").length).toBeGreaterThan(1)
await act(async () => press("review.cosign_and_send"))
expect(edit.publishTestimony.execute).toHaveBeenCalled()
expect(onPosted).toHaveBeenCalled()
})

it("tells a cosign without words of its own that it still counts", () => {
const onStep = jest.fn()
mount(
editing({
draft: { cosignOfId: "p1", content: "" }
} as Partial<UseEditConferenceTestimony>),
"review",
{ onStep }
)
expect(screen.getByText(/review\.no_line/)).toBeInTheDocument()
press("review.add_line")
expect(onStep).toHaveBeenCalledWith("write")
})

it("says no banner over a cosign with words", () => {
mount(editing({ draft } as Partial<UseEditConferenceTestimony>), "review")
expect(screen.queryByText(/review\.no_line/)).not.toBeInTheDocument()
})

it("says so, and sends no letter, when MAPLE has no one to notify", async () => {
mockProfile.current = { fullName: "Gina K." }
const edit = editing({ draft } as Partial<UseEditConferenceTestimony>)
mount(edit, "review")
expect(screen.getByText("review.emailed_none")).toBeInTheDocument()
expect(screen.queryByText("review.preview_label")).not.toBeInTheDocument()
await act(async () => press("input.cosign"))
expect(edit.publishTestimony.execute).toHaveBeenCalled()
})

it("keeps another reply-to address on the draft, for this letter alone", async () => {
jest.useFakeTimers()
const edit = editing({ draft } as Partial<UseEditConferenceTestimony>)
mount(edit, "review")
press("review.use_other")
fireEvent.change(screen.getByLabelText("review.reply_to_label"), {
target: { value: "gina.work@example.org" }
})
press("review.done")
await act(async () => {
jest.advanceTimersByTime(2000)
})
expect(edit.saveDraft.execute).toHaveBeenCalledWith(
expect.objectContaining({
cosignOfId: "p1",
replyTo: "gina.work@example.org"
})
)
jest.useRealTimers()
})

it("refuses an address that isn't one", () => {
const edit = editing({ draft } as Partial<UseEditConferenceTestimony>)
mount(edit, "review")
press("review.use_other")
fireEvent.change(screen.getByLabelText("review.reply_to_label"), {
target: { value: "not an address" }
})
press("review.done")
expect(screen.getByRole("alert")).toHaveTextContent(
"review.reply_to_invalid"
)
})
})

it.each([
Expand Down
74 changes: 67 additions & 7 deletions components/conference/input/CosignForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ import {
} from "./InputForm"
import { useInputContext } from "./context"
import { UsersRound } from "./icons"
import { LetterReview, NoLine } from "./Review"
import { hasLegislators } from "functions/src/conferenceLetters/letter"
import { brand, caution, ink, line, red, wash } from "./tokens"
import { useAutosave } from "./useAutosave"

export type CosignStep = "confirm" | "write"
export type CosignStep = "confirm" | "write" | "review"

/** Below lg, where the letter sits above it, the agreeing step takes the room
* it needs and leaves the letter the rest. */
Expand Down Expand Up @@ -256,11 +258,19 @@ const refusalOf = (error?: Error) =>
export function CosignForm({
edit,
parent,
court,
subject,
conferees,
step,
onStep,
onCancel,
onPosted
}: {
court: number
/** What the conference is called, for the letter's subject. */
subject: string
/** Initials of the conferees, for the weekly update's row. */
conferees: string[]
/** The user's draft and publication on the conference. */
edit: UseEditConferenceTestimony
/** What is being cosigned. */
Expand All @@ -281,6 +291,8 @@ export function CosignForm({
const [note, setNote] = useState("")
const [noWords, setNoWords] = useState(false)
const [reason, setReason] = useState("")
// The address replies to this letter go to, when not the reader's own.
const [chosen, setChosen] = useState("")
const field = useRef<HTMLTextAreaElement>(null)
const ask = useAsk(field)

Expand All @@ -298,25 +310,28 @@ export function CosignForm({
// Words drafted since are kept over the posted cosign's having none.
if (edit.publication && !edit.publication.content && !seed) setNoWords(true)
if (edit.draft?.editReason) setReason(edit.draft.editReason)
if (edit.draft?.replyTo) setChosen(edit.draft.replyTo)
}, [edit.loading, edit.draft, edit.publication, parent.id])

const content = noWords ? "" : note
const { save, dirty, posting, post, flush, reset } = useAutosave({
edit,
fields: { content, reason },
fields: { content, reason, chosen },
matches: (draft, f) =>
draft?.cosignOfId === parent.id &&
draft?.content === f.content &&
(draft?.editReason ?? "") === f.reason,
(draft?.editReason ?? "") === f.reason &&
(draft?.replyTo ?? "") === f.chosen,
write: f =>
saveDraft.execute({
position: parent.position,
content: f.content,
cosignOfId: parent.id,
cosignOfAuthorUid: parent.authorUid,
editReason: f.reason || undefined
editReason: f.reason || undefined,
replyTo: f.chosen || undefined
}),
active: step === "write",
active: step !== "confirm",
onPosted
})

Expand Down Expand Up @@ -389,6 +404,47 @@ export function CosignForm({

const name = profile?.fullName || t("input.anonymous")
const type = accountTypeOf(claims?.role ?? "user")
if (step === "review") {
const sends = hasLegislators(profile ?? {})
return (
<Body>
<Scroll>
{!content.trim() && <NoLine onAdd={() => onStep("write")} />}
<LetterReview
court={court}
conferees={conferees}
author={name}
conference={subject}
position={parent.position}
content={content}
cosign={{ author: nameOf(parent), content: parent.content }}
chosen={chosen}
onChoose={setChosen}
/>
{problem}
</Scroll>
<Actions>
<Quiet
type="button"
onClick={() => onStep("write")}
style={{ marginRight: "auto" }}
>
{t("review.back")}
</Quiet>
<Leave type="button" onClick={onCancel}>
{t("input.cancel")}
</Leave>
<Press
type="button"
disabled={posting || publishTestimony.loading}
onClick={post}
>
{t(sends ? "review.cosign_and_send" : "input.cosign")}
</Press>
</Actions>
</Body>
)
}
return (
<Body>
<Scroll>
Expand Down Expand Up @@ -466,9 +522,13 @@ export function CosignForm({
posting ||
publishTestimony.loading
}
onClick={() => (ready ? post() : ask.ask())}
onClick={() => {
if (!ready) ask.ask()
else if (editing) post()
else onStep("review")
}}
>
{t(editing ? "input.cosign_save" : "input.cosign")}
{t(editing ? "input.cosign_save" : "input.review")}
</Press>
</Actions>
</Body>
Expand Down
Loading
Loading