From ab1ef1a5b546307f6a1a6f9b655bd98b81658be0 Mon Sep 17 00:00:00 2001 From: emilielr <43408175+emilielr@users.noreply.github.com> Date: Thu, 31 Oct 2024 13:01:56 +0100 Subject: [PATCH] feat(contact): make email optional in form (#1708) * feat(contact): make email optional and add checkbox to prevent user from submit without email * style(contact): align checkbox with text on first line --- tavla/app/components/ContactForm.tsx | 68 ++++++++++++++++++---------- tavla/app/components/actions.ts | 6 ++- 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/tavla/app/components/ContactForm.tsx b/tavla/app/components/ContactForm.tsx index d13863352..8217fe336 100644 --- a/tavla/app/components/ContactForm.tsx +++ b/tavla/app/components/ContactForm.tsx @@ -1,6 +1,6 @@ 'use client' -import { TextArea, TextField } from '@entur/form' -import { Label, Paragraph } from '@entur/typography' +import { Checkbox, TextArea, TextField } from '@entur/form' +import { Label, Paragraph, SubParagraph } from '@entur/typography' import { SubmitButton } from 'components/Form/SubmitButton' import { postForm } from './actions' import { @@ -10,11 +10,12 @@ import { } from 'app/(admin)/utils' import { useState } from 'react' import { FormError } from 'app/(admin)/components/FormError' -import { useToast } from '@entur/alert' +import { SmallAlertBox, useToast } from '@entur/alert' import { Expandable } from './Expandable' import { usePostHog } from 'posthog-js/react' import { isEmptyOrSpaces } from 'app/(admin)/edit/utils' import { validEmail } from 'utils/email' + function ContactForm() { const posthog = usePostHog() @@ -23,12 +24,13 @@ function ContactForm() { const [formState, setFormError] = useState( undefined, ) + const [disabledEmail, setDisabledEmail] = useState(false) const submit = async (data: FormData) => { const email = data.get('email') as string const message = data.get('message') as string - if (!validEmail(email)) + if (!disabledEmail && !validEmail(email)) return setFormError(getFormFeedbackForError('auth/missing-email')) if (isEmptyOrSpaces(message)) @@ -40,6 +42,7 @@ function ContactForm() { if (error) return setFormError(error) else { setIsOpen(false) + setDisabledEmail(false) setFormError(undefined) addToast('Takk for tilbakemelding!') } @@ -66,25 +69,9 @@ function ContactForm() { Vi setter stor pris på tilbakemeldinger og innspill, og bistår gjerne hvis du vil ha hjelp til å komme i gang - med Tavla + med Tavla. -
- - -
- - Hvis du ønsker å legge ved bilder, kan du sende en - e-post til tavla@entur.org. - +
+ + +
+
+ setDisabledEmail(!disabledEmail)} + > + Jeg ønsker ikke å oppgi e-postadresse og vil ikke få + svar på henvendelsen. + + {disabledEmail && ( + + Vi kan bare svare på meldingen hvis vi har + e-postadressen din. + + )} +
+ diff --git a/tavla/app/components/actions.ts b/tavla/app/components/actions.ts index 53d10b294..11ff976df 100644 --- a/tavla/app/components/actions.ts +++ b/tavla/app/components/actions.ts @@ -3,11 +3,15 @@ import { isEmptyOrSpaces } from 'app/(admin)/edit/utils' import { TFormFeedback, getFormFeedbackForError } from 'app/(admin)/utils' import { validEmail } from 'utils/email' + async function postForm(prevState: TFormFeedback | undefined, data: FormData) { const email = data.get('email') as string const message = data.get('message') as string + const disabledEmail = data.get('disabledEmail') as string - if (!validEmail(email)) return getFormFeedbackForError('auth/missing-email') + if (disabledEmail !== 'on' && !validEmail(email)) { + return getFormFeedbackForError('auth/missing-email') + } if (isEmptyOrSpaces(message)) return getFormFeedbackForError('contact/message-missing')