|
| 1 | +import { data, useFetcher } from 'react-router'; |
| 2 | +import type { RouteHandle } from 'react-router'; |
| 3 | + |
| 4 | +import { Trans, useTranslation } from 'react-i18next'; |
| 5 | +import * as v from 'valibot'; |
| 6 | + |
| 7 | +import type { Route, Info } from './+types/privacy-statement'; |
| 8 | + |
| 9 | +import { requireAuth } from '~/.server/utils/auth-utils'; |
| 10 | +import { i18nRedirect } from '~/.server/utils/route-utils'; |
| 11 | +import { confirmPrivacyNoticeSchema } from '~/.server/validation/confirmPrivacyNoticeSchema'; |
| 12 | +import { Button } from '~/components/button'; |
| 13 | +import { ButtonLink } from '~/components/button-link'; |
| 14 | +import { ErrorSummary } from '~/components/error-summary'; |
| 15 | +import { InputCheckbox } from '~/components/input-checkbox'; |
| 16 | +import { Progress } from '~/components/progress'; |
| 17 | +import { getFixedT } from '~/i18n-config.server'; |
| 18 | +import { handle as parentHandle } from '~/routes/protected/layout'; |
| 19 | +import { getLanguage } from '~/utils/i18n-utils'; |
| 20 | + |
| 21 | +export const handle = { |
| 22 | + i18nNamespace: [...parentHandle.i18nNamespace, 'protected'], |
| 23 | +} as const satisfies RouteHandle; |
| 24 | + |
| 25 | +export async function loader({ context, request }: Route.LoaderArgs) { |
| 26 | + requireAuth(context.session, new URL(request.url), ['user']); |
| 27 | + const t = await getFixedT(request, handle.i18nNamespace); |
| 28 | + |
| 29 | + return { |
| 30 | + documentTitle: t('protected:privacy-statement.page-title'), |
| 31 | + defaultFormValues: { |
| 32 | + confirmPrivacyNotice: context.session.inPersonSINCase?.confirmPrivacyNotice, |
| 33 | + }, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +export const meta: Route.MetaFunction = ({ data }) => { |
| 38 | + return [{ title: data.documentTitle }]; |
| 39 | +}; |
| 40 | + |
| 41 | +export async function action({ context, request }: Route.ActionArgs) { |
| 42 | + requireAuth(context.session, new URL(request.url), ['user']); |
| 43 | + const lang = getLanguage(request); |
| 44 | + |
| 45 | + const formData = await request.formData(); |
| 46 | + const input = { confirmPrivacyNotice: formData.get('confirmPrivacyNotice') as string }; |
| 47 | + |
| 48 | + const schema = v.object({ confirmPrivacyNotice: confirmPrivacyNoticeSchema() }); |
| 49 | + const parsedDataResult = v.safeParse(schema, input, { lang }); |
| 50 | + |
| 51 | + if (!parsedDataResult.success) { |
| 52 | + return data({ errors: v.flatten<typeof schema>(parsedDataResult.issues).nested }, { status: 400 }); |
| 53 | + } |
| 54 | + |
| 55 | + // If the first name is valid, store it in the session and redirect to the next page |
| 56 | + context.session.inPersonSINCase = { |
| 57 | + ...(context.session.inPersonSINCase ?? {}), |
| 58 | + ...input, |
| 59 | + }; |
| 60 | + return i18nRedirect('routes/protected/person-case/first-name.tsx', request); |
| 61 | +} |
| 62 | + |
| 63 | +export default function PrivacyStatement({ loaderData, actionData, params }: Route.ComponentProps) { |
| 64 | + const { t } = useTranslation(handle.i18nNamespace); |
| 65 | + |
| 66 | + const fetcher = useFetcher<Info['actionData']>(); |
| 67 | + const isSubmitting = fetcher.state !== 'idle'; |
| 68 | + const errors = fetcher.data?.errors; |
| 69 | + |
| 70 | + return ( |
| 71 | + <> |
| 72 | + <Progress className="mt-8 mb-8" label="" value={20} /> |
| 73 | + <span className="text-md">{t('protected:in-person.title')}</span> |
| 74 | + <h1 className="mb-8 text-2xl font-bold text-slate-700"> |
| 75 | + <Trans |
| 76 | + i18nKey="protected:privacy-statement.title" |
| 77 | + components={{ span: <span className="underline decoration-red-800 underline-offset-8" /> }} |
| 78 | + /> |
| 79 | + </h1> |
| 80 | + |
| 81 | + <fetcher.Form method="post" noValidate> |
| 82 | + <div className="space-y-6"> |
| 83 | + <ErrorSummary errors={errors} /> |
| 84 | + <p> |
| 85 | + Lorem ipsum odor amet, consectetuer adipiscing elit. Gravida pulvinar fringilla augue per lacinia cubilia aliquam. |
| 86 | + Nibh egestas pharetra; at sit ipsum aliquet fames pellentesque. Posuere mauris pretium commodo hendrerit maecenas |
| 87 | + neque imperdiet. Phasellus tempor metus phasellus eu malesuada. Mi fusce dapibus nam metus est sagittis nisl sem |
| 88 | + fringilla. Iaculis gravida netus aptent mattis dignissim massa. Dolor curae donec hac dui, neque proin erat. Nullam |
| 89 | + sem ullamcorper commodo phasellus hendrerit ex. Curabitur venenatis ex, vitae fermentum finibus nibh. |
| 90 | + </p> |
| 91 | + |
| 92 | + <p> |
| 93 | + Himenaeos turpis id pretium mauris pellentesque quis curae. Facilisi sollicitudin justo erat habitasse turpis |
| 94 | + consequat taciti. Scelerisque suspendisse hac dictumst mattis in odio. Molestie molestie parturient arcu iaculis |
| 95 | + lacinia. Ut est vel massa fusce congue laoreet posuere pulvinar. Consectetur pharetra ipsum tortor cubilia ut. |
| 96 | + Placerat a pellentesque commodo bibendum posuere vivamus. Senectus imperdiet sit praesent adipiscing accumsan nibh |
| 97 | + consequat per. Aliquam turpis ut libero non malesuada tortor ac maximus dictum. Non vestibulum pellentesque posuere |
| 98 | + dapibus eleifend cras tempus potenti. |
| 99 | + </p> |
| 100 | + |
| 101 | + <p> |
| 102 | + Rhoncus semper dolor; scelerisque euismod justo integer. Rhoncus et cras cursus velit diam. Vehicula magna sem eget |
| 103 | + urna vitae donec phasellus dignissim volutpat. Arcu mi neque ad nulla; dui maximus. Nulla ligula ultrices facilisi |
| 104 | + urna rhoncus platea per platea. Nascetur nec dapibus augue dictum volutpat tristique nec dis. Dis ante metus tortor |
| 105 | + lacus porta. |
| 106 | + </p> |
| 107 | + |
| 108 | + <InputCheckbox |
| 109 | + id="confirmPrivacyNotice" |
| 110 | + name="confirmPrivacyNotice" |
| 111 | + errorMessage={errors?.confirmPrivacyNotice?.[0]} |
| 112 | + defaultChecked={loaderData.defaultFormValues.confirmPrivacyNotice === 'on'} |
| 113 | + required |
| 114 | + > |
| 115 | + {t('protected:privacy-statement.confirm-privacy-notice-checkbox')} |
| 116 | + </InputCheckbox> |
| 117 | + </div> |
| 118 | + <div className="mt-8 flex flex-wrap items-center gap-3"> |
| 119 | + <ButtonLink id="back-button" file="routes/protected/index.tsx" params={params} disabled={isSubmitting}> |
| 120 | + {t('protected:person-case.previous')} |
| 121 | + </ButtonLink> |
| 122 | + <Button variant="primary" type="submit" id="continue-button" disabled={isSubmitting}> |
| 123 | + {t('protected:person-case.next')} |
| 124 | + </Button> |
| 125 | + </div> |
| 126 | + </fetcher.Form> |
| 127 | + </> |
| 128 | + ); |
| 129 | +} |
0 commit comments