From 14ea8889696a8a81317ffbe32caaf64dd0ca9873 Mon Sep 17 00:00:00 2001 From: Eric Sizer Date: Mon, 16 Dec 2024 14:36:08 -0500 Subject: [PATCH 1/2] use search params to populate default values --- .../components/SupportForm/SupportForm.tsx | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/apps/web/src/pages/SupportPage/components/SupportForm/SupportForm.tsx b/apps/web/src/pages/SupportPage/components/SupportForm/SupportForm.tsx index bcce845b14e..0ce1e71ad00 100644 --- a/apps/web/src/pages/SupportPage/components/SupportForm/SupportForm.tsx +++ b/apps/web/src/pages/SupportPage/components/SupportForm/SupportForm.tsx @@ -2,7 +2,7 @@ // Note: Disable camelcase since variables are being used by API import { FormProvider, SubmitHandler, useForm } from "react-hook-form"; import { defineMessage, useIntl } from "react-intl"; -import { useLocation, Location } from "react-router"; +import { useLocation, Location, useSearchParams } from "react-router"; import { useQuery } from "urql"; import { ReactNode, useState } from "react"; @@ -50,6 +50,15 @@ const anchorTag = (chunks: ReactNode) => ( {chunks} ); +const availableSubjects = ["bug", "feedback", "question"]; +function defaultSubject(subject?: string | null): string { + if (subject && availableSubjects.includes(subject)) { + return subject; + } + + return ""; +} + const SupportFormSuccess = ({ onFormToggle }: SupportFormSuccessProps) => { const intl = useIntl(); return ( @@ -118,10 +127,13 @@ const SupportForm = ({ }: SupportFormProps) => { const intl = useIntl(); const location = useLocation() as Location; + const [params] = useSearchParams(); const previousUrl = location?.state?.referrer ?? document?.referrer ?? ""; const userAgent = window?.navigator.userAgent ?? ""; const methods = useForm({ defaultValues: { + subject: defaultSubject(params.get("subject")), + description: params.get("description") ?? "", user_id: currentUser?.id ?? "", name: currentUser ? getFullNameLabel(currentUser.firstName, currentUser.lastName, intl) From 5fff3d672d50f2ad4b07e98cc5260298fefbb378 Mon Sep 17 00:00:00 2001 From: Eric Sizer Date: Mon, 16 Dec 2024 14:58:00 -0500 Subject: [PATCH 2/2] add tests for support form search params --- apps/playwright/tests/support-page.spec.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/apps/playwright/tests/support-page.spec.ts b/apps/playwright/tests/support-page.spec.ts index bd4afe63f69..1d75be256fd 100644 --- a/apps/playwright/tests/support-page.spec.ts +++ b/apps/playwright/tests/support-page.spec.ts @@ -16,6 +16,21 @@ test.describe("Support page", () => { const accessibilityScanResults = await makeAxeBuilder().analyze(); expect(accessibilityScanResults.violations).toEqual([]); }); + test("populates from search param", async ({ page }) => { + await page.goto("/en/support?subject=bug&description=test"); + await expect( + page.getByRole("combobox", { name: /looking to/i }), + ).toHaveValue("bug"); + await expect(page.getByRole("textbox", { name: /details/i })).toHaveValue( + "test", + ); + }); + test("does not populate invalid subject param", async ({ page }) => { + await page.goto("/en/support?subject=invalid"); + await expect( + page.getByRole("combobox", { name: /looking to/i }), + ).toHaveValue(""); + }); }); test.describe("Support form", () => { test("send POST request to existing API endpoint", async ({ request }) => {