diff --git a/web/src/components/ContactDetails/ContactDetails.test.tsx b/web/src/components/ContactDetails/ContactDetails.test.tsx new file mode 100644 index 0000000..8b0cb8b --- /dev/null +++ b/web/src/components/ContactDetails/ContactDetails.test.tsx @@ -0,0 +1,106 @@ +import { screen, within } from '@testing-library/react'; +import { describe, expect, it } from 'vitest'; +import siteContent from '@/content/siteContent'; +import { axeResults } from '@/test/axe'; +import { renderWithProviders } from '@/test/renderWithProviders'; +import ContactDetails from './ContactDetails'; + +type Props = Partial>; + +const card = (props: Props = {}) => ( + +); + +const portrait = { + src: '/portret.jpg', + alt: 'Andrea', + width: 144, + height: 144, +}; + +describe('ContactDetails', () => { + it('lists e-mail, Instagram and Facebook under a named heading', async () => { + const { container } = await renderWithProviders(card()); + expect( + screen.getByRole('heading', { level: 2, name: 'Direct contact' }), + ).toHaveAttribute('id', 'details-title'); + const list = screen.getByRole('list', { name: 'Direct contact' }); + const names = within(list) + .getAllByRole('link') + .map((link) => link.textContent); + expect(names).toEqual([ + 'info@andreaslop.nl', + '@andreaslopfotografie (opent in nieuw tabblad)', + 'Andrea Slop Fotografie (opent in nieuw tabblad)', + ]); + expect(await axeResults(container)).toHaveNoViolations(); + }); + + it('mails with the default subject in the same tab', async () => { + await renderWithProviders(card()); + const mail = screen.getByTestId('contact-details-email'); + expect(mail).toHaveAttribute( + 'href', + 'mailto:info@andreaslop.nl?subject=Vraag%20via%20andreaslop.nl', + ); + expect(mail).not.toHaveAttribute('target'); + }); + + it('opens the socials safely in a new tab and says so', async () => { + await renderWithProviders(card()); + const instagram = screen.getByRole('link', { + name: '@andreaslopfotografie (opent in nieuw tabblad)', + }); + expect(instagram).toHaveAttribute('target', '_blank'); + expect(instagram).toHaveAttribute('rel', 'noopener noreferrer'); + expect(instagram).toHaveAttribute( + 'href', + 'https://www.instagram.com/andreaslopfotografie/', + ); + }); + + it('hides the icons from assistive technology', async () => { + await renderWithProviders(card()); + const icons = screen.getByTestId('details').querySelectorAll('svg'); + expect(icons).toHaveLength(3); + icons.forEach((icon) => + expect(icon).toHaveAttribute('aria-hidden', 'true'), + ); + }); + + it('renders nothing without a valid channel', async () => { + const { container } = await renderWithProviders( + card({ email: { user: '', domain: '' }, socials: [] }), + ); + expect(screen.queryByTestId('details')).toBeNull(); + expect(container.querySelector('h2')).toBeNull(); + }); + + it('shows the portrait as decoration next to the heading', async () => { + const { container } = await renderWithProviders(card({ portrait })); + const photo = screen.getByTestId('contact-details-portrait'); + expect(photo).toHaveAttribute('src', '/portret.jpg'); + expect(photo).toHaveAttribute('alt', ''); + expect(await axeResults(container)).toHaveNoViolations(); + }); + + it('can appear twice on a page with its own heading ids', async () => { + const { container } = await renderWithProviders( + <> + {card({ titleId: 'details-a', 'data-testid': 'a' })} + {card({ titleId: 'details-b', 'data-testid': 'b' })} + , + ); + expect( + screen.getAllByRole('list', { name: 'Direct contact' }), + ).toHaveLength(2); + expect(await axeResults(container)).toHaveNoViolations(); + }); +}); diff --git a/web/src/components/ContactDetails/ContactDetails.tsx b/web/src/components/ContactDetails/ContactDetails.tsx new file mode 100644 index 0000000..3a05617 --- /dev/null +++ b/web/src/components/ContactDetails/ContactDetails.tsx @@ -0,0 +1,118 @@ +import { + Box, + chakra, + Flex, + Image, + Link, + VisuallyHidden, +} from '@chakra-ui/react'; +import mailOutlined from '@iconify-icons/ant-design/mail-outlined'; +import { Icon } from '@iconify/react/offline'; +import Eyebrow from '@/components/Eyebrow/Eyebrow'; +import socialPlatforms from '@/content/socialPlatforms'; +import type { ContentImage, EmailAddress, SocialLink } from '@/content/types'; +import { contactChannels, type ContactChannel } from '@/lib/contactChannels'; + +type ContactDetailsProps = { + // Id of the h2, unique on the page: the card can appear twice + titleId: string; + title: string; + email: EmailAddress; + socials: SocialLink[]; + // Small round photo next to the heading; decorative, the heading and + // the page already say whose card it is + portrait?: ContentImage; + 'data-testid'?: string; +}; + +const iconOf = (channel: ContactChannel) => + channel.kind === 'email' + ? mailOutlined + : socialPlatforms[channel.platform].icon; + +// "Direct contact": a card with the ways to reach Andrea, each an icon and a +// link (BR-CD-01..08). Renders nothing when no channel is valid. +export default function ContactDetails({ + titleId, + title, + email, + socials, + portrait, + 'data-testid': testId, +}: ContactDetailsProps) { + const channels = contactChannels(email, socials); + if (channels.length === 0) return null; + + return ( + + + {portrait && ( + + )} + + {title} + + + + {channels.map((channel) => ( + + + + + + {/* Space outside the hidden text, or it drops out + of the announced name */} + {channel.label} + {channel.external && ( + <> + {' '} + + (opent in nieuw tabblad) + + + )} + + + ))} + + + ); +} diff --git a/web/src/components/ContactForm/ContactForm.test.tsx b/web/src/components/ContactForm/ContactForm.test.tsx index c3004f8..11121e4 100644 --- a/web/src/components/ContactForm/ContactForm.test.tsx +++ b/web/src/components/ContactForm/ContactForm.test.tsx @@ -26,6 +26,7 @@ const renderForm = ( options={options} mailSubject="Vraag via andreaslop.nl" email={siteContent.email} + replyTime="1-2 werkdagen" submit={vi.fn().mockResolvedValue(undefined)} {...props} />, @@ -117,6 +118,24 @@ describe('ContactForm', () => { expect(screen.queryByTestId('contact-form')).toBeNull(); }); + it('takes the reply time for the thank-you from the content', async () => { + await renderForm({ replyTime: '3 werkdagen' }); + fillIn(); + send(); + expect( + await screen.findByTestId('contact-form-sent'), + ).toHaveTextContent('Ik reageer binnen 3 werkdagen.'); + }); + + it('shows the privacy line under the submit button', async () => { + await renderForm(); + const form = screen.getByTestId('contact-form'); + expect(form).toContainElement(screen.getByTestId('privacy-note')); + expect( + screen.getByRole('link', { name: 'privacyverklaring' }), + ).toHaveAttribute('href', '/privacyverklaring'); + }); + it('keeps the input and offers the mail link when sending fails', async () => { await renderForm({ submit: vi.fn().mockRejectedValue(new Error('x')) }); fillIn(); @@ -194,6 +213,7 @@ describe('ContactForm', () => { preselect={preselect} mailSubject="Vraag via andreaslop.nl" email={siteContent.email} + replyTime="1-2 werkdagen" /> ); diff --git a/web/src/components/ContactForm/ContactForm.tsx b/web/src/components/ContactForm/ContactForm.tsx index 7a235c1..3251d34 100644 --- a/web/src/components/ContactForm/ContactForm.tsx +++ b/web/src/components/ContactForm/ContactForm.tsx @@ -12,6 +12,7 @@ import { Textarea, useRecipe, } from '@chakra-ui/react'; +import PrivacyNote from '@/components/PrivacyNote/PrivacyNote'; import { submitContactForm } from '@/contact/submitContactForm'; import { CONTACT_LIMITS, @@ -39,12 +40,11 @@ type ContactFormProps = { preselect?: { shoot: string; key: number }; mailSubject: string; email: EmailAddress; + // How soon Andrea answers, e.g. '1-2 werkdagen' (siteContent) + replyTime: string; submit?: (payload: ContactPayload) => Promise; }; -const SENT_MESSAGE = - 'Bedankt voor je bericht! Ik reageer binnen 1-2 werkdagen.'; - // The contact form (design .form): name and e-mail, the shoot, the question, // and the browser-side anti-spam. Its behaviour is the pure // contactFormReducer; this component wires it to the fields. @@ -54,6 +54,7 @@ export default function ContactForm({ preselect, mailSubject, email, + replyTime, submit = submitContactForm, }: ContactFormProps) { const reduce = ( @@ -106,7 +107,7 @@ export default function ContactForm({ borderStartColor="accent" bg="paper.alt" > - {SENT_MESSAGE} + {`Bedankt voor je bericht! Ik reageer binnen ${replyTime}.`} ); } @@ -298,6 +299,7 @@ export default function ContactForm({ > {disabled ? 'Versturen…' : '(Aan)vraag versturen'} + ); diff --git a/web/src/components/ContactForm/ContactFormGate.test.tsx b/web/src/components/ContactForm/ContactFormGate.test.tsx index c32f81e..39b3e79 100644 --- a/web/src/components/ContactForm/ContactFormGate.test.tsx +++ b/web/src/components/ContactForm/ContactFormGate.test.tsx @@ -44,7 +44,21 @@ describe('ContactFormGate', () => { expect(screen.getByTestId('contact-gate-notice')).toHaveTextContent( 'Het contactformulier komt binnenkort.', ); - expect(screen.queryByRole('link')).toBeNull(); + // Only the privacy link is left; no mail link without an address + expect( + screen.getAllByRole('link').map((link) => link.textContent), + ).toEqual(['privacyverklaring']); + }); + + it('shows the privacy line under the notice, outside the inert form', async () => { + await renderGate(false); + const notice = screen.getByTestId('contact-gate-notice'); + const note = screen.getByTestId('privacy-note'); + expect(notice).toContainElement(note); + expect(note.closest('[inert]')).toBeNull(); + expect( + screen.getByRole('link', { name: 'privacyverklaring' }), + ).toHaveAttribute('href', '/privacyverklaring'); }); it('shows just the form when switched on', async () => { diff --git a/web/src/components/ContactForm/ContactFormGate.tsx b/web/src/components/ContactForm/ContactFormGate.tsx index ee338c8..41b3f10 100644 --- a/web/src/components/ContactForm/ContactFormGate.tsx +++ b/web/src/components/ContactForm/ContactFormGate.tsx @@ -1,6 +1,7 @@ import type { ReactNode } from 'react'; import { Box, chakra, Link, Text, useRecipe } from '@chakra-ui/react'; import Eyebrow from '@/components/Eyebrow/Eyebrow'; +import PrivacyNote from '@/components/PrivacyNote/PrivacyNote'; import type { EmailAddress } from '@/content/types'; import { emailAddress } from '@/lib/emailAddress'; import { mailtoLink } from '@/lib/mailLink'; @@ -68,6 +69,8 @@ export default function ContactFormGate({ Stuur een e-mail )} + {/* Readable now, while the form's own line is inert (BR-PR-03) */} + ); diff --git a/web/src/components/ContactPanel/ContactPanel.test.tsx b/web/src/components/ContactPanel/ContactPanel.test.tsx new file mode 100644 index 0000000..df309ca --- /dev/null +++ b/web/src/components/ContactPanel/ContactPanel.test.tsx @@ -0,0 +1,124 @@ +import { useState } from 'react'; +import { act, fireEvent, screen } from '@testing-library/react'; +import { describe, expect, it, vi } from 'vitest'; +import packages from '@/content/packages'; +import siteContent from '@/content/siteContent'; +import { axeResults } from '@/test/axe'; +import { FakeIntersectionObserver } from '@/test/fakeIntersectionObserver'; +import { renderWithProviders } from '@/test/renderWithProviders'; +import ContactPanel from './ContactPanel'; + +type Props = Partial>; + +const panel = (props: Props = {}) => ( + +); + +// The form is a lazy chunk; its first import can take over a second under +// the full suite with coverage +const LAZY_LOAD = { timeout: 10_000 }; +const shoot = () => + screen.findByTestId('contact-form-shoot', undefined, LAZY_LOAD); +const mailButton = () => screen.getByRole('link', { name: 'Stuur een e-mail' }); + +describe('ContactPanel', () => { + it('loads the form right away when eager, behind the gate', async () => { + const { container } = await renderWithProviders(panel()); + expect(await shoot()).toHaveValue(''); + expect(screen.getByTestId('contact-gate-notice')).toBeInTheDocument(); + expect(mailButton()).toHaveAttribute( + 'href', + 'mailto:info@andreaslop.nl?subject=Vraag%20via%20andreaslop.nl', + ); + expect(await axeResults(container)).toHaveNoViolations(); + }); + + it('offers the shown packages and Overig as shoots', async () => { + await renderWithProviders(panel({ enabled: true })); + const options = [...(await shoot()).querySelectorAll('option')].map( + (option) => option.textContent, + ); + expect(options).toEqual([ + 'Kies een optie...', + ...packages.map((pkg) => pkg.name), + 'Overig', + ]); + }); + + it('waits with a placeholder until the form comes near the screen when lazy', async () => { + vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver); + FakeIntersectionObserver.reset(); + await renderWithProviders(panel({ load: 'lazy' })); + expect(screen.queryByTestId('contact-form')).toBeNull(); + // The notice is already in place over the reserved space + expect(screen.getByTestId('contact-gate-notice')).toBeInTheDocument(); + + act(() => + FakeIntersectionObserver.instances.forEach((observer) => + observer.trigger(true), + ), + ); + expect(await shoot()).toBeInTheDocument(); + }); + + it('loads a lazy form at once when a package is chosen', async () => { + vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver); + FakeIntersectionObserver.reset(); + await renderWithProviders(panel({ load: 'lazy', pakket: 'newborn' })); + expect(await shoot()).toHaveValue('newborn'); + }); + + it('preselects a known package and puts it in the mail subject', async () => { + await renderWithProviders(panel({ pakket: ' NEWBORN ' })); + expect(await shoot()).toHaveValue('newborn'); + expect(mailButton()).toHaveAttribute( + 'href', + 'mailto:info@andreaslop.nl?subject=Aanvraag%20fotoshoot%3A%20Newborn', + ); + }); + + it('ignores an unknown package', async () => { + await renderWithProviders(panel({ pakket: 'bruiloft' })); + expect(await shoot()).toHaveValue(''); + expect(mailButton()).toHaveAttribute( + 'href', + 'mailto:info@andreaslop.nl?subject=Vraag%20via%20andreaslop.nl', + ); + }); + + it('applies the same package again after the visitor changed it', async () => { + // The page clears ?pakket= after taking it over; a second "Boek" + // click on the same package brings it back + function Harness() { + const [pakket, setPakket] = useState('newborn'); + return ( + <> + + + {panel({ enabled: true, pakket })} + + ); + } + await renderWithProviders(); + fireEvent.change(await shoot(), { target: { value: 'gezin' } }); + fireEvent.click(screen.getByRole('button', { name: 'wis' })); + expect(await shoot()).toHaveValue('gezin'); + fireEvent.click(screen.getByRole('button', { name: 'boek' })); + expect(await shoot()).toHaveValue('newborn'); + }); + + it('shows just the form when switched on', async () => { + await renderWithProviders(panel({ enabled: true })); + await shoot(); + expect(screen.queryByTestId('contact-gate-notice')).toBeNull(); + expect(screen.getByTestId('privacy-note')).toBeInTheDocument(); + }); +}); diff --git a/web/src/components/ContactPanel/ContactPanel.tsx b/web/src/components/ContactPanel/ContactPanel.tsx new file mode 100644 index 0000000..95d9c82 --- /dev/null +++ b/web/src/components/ContactPanel/ContactPanel.tsx @@ -0,0 +1,100 @@ +import { lazy, Suspense, useMemo, useState } from 'react'; +import { Box } from '@chakra-ui/react'; +import { DEFERRED_LOAD_MARGIN } from '@/components/DeferredImage/DeferredImage'; +import ContactFormGate from '@/components/ContactForm/ContactFormGate'; +import type { EmailAddress, PricingPackage } from '@/content/types'; +import useInView from '@/hooks/useInView'; +import { mailSubject } from '@/lib/mailLink'; +import { packageFromSearch } from '@/lib/packageFromSearch'; +import { shootOptions } from '@/lib/shootOptions'; +import { visiblePackages } from '@/lib/visiblePackages'; + +// The form (Chakra fields, validation, reducer) is its own chunk, so a page +// only pays for it when the form is about to be used. On the homepage that +// saved ±0.03 Lighthouse performance on mobile. +const ContactForm = lazy(() => import('@/components/ContactForm/ContactForm')); + +// Keeps the form's space while it loads, so the notice sits where it will +// stay and the page below does not jump (measured with the privacy line: +// 649px on phones, where name and e-mail are stacked, 547px from sm) +const FORM_PLACEHOLDER_HEIGHT = { base: '649px', sm: '547px' }; + +type ContactPanelProps = { + // Prefix for the form's field ids, unique per page + idPrefix: string; + packages: PricingPackage[]; + enabled: boolean; + email: EmailAddress; + replyTime: string; + // Raw ?pakket= value from the page; checked against the shown packages + pakket?: string; + // lazy: load the form when it comes near the screen or a package is + // chosen (homepage); eager: start loading it with the page (contact page) + load: 'lazy' | 'eager'; +}; + +// The contact form, ready to place on a page: the coming-soon gate, the +// shoot options from the packages and a package chosen via ?pakket=, with +// the mail subject that goes with it (BR-CPN-01..06). Heading, URL clean-up, +// scrolling and focus stay with the page. +export default function ContactPanel({ + idPrefix, + packages, + enabled, + email, + replyTime, + pakket, + load, +}: ContactPanelProps) { + const shown = useMemo(() => visiblePackages(packages), [packages]); + const options = useMemo(() => shootOptions(packages), [packages]); + + // Take over a new ?pakket= while rendering (React's pattern for a + // changed prop); the last "Boek" click wins (BR-PARAM-03) + const [seenPakket, setSeenPakket] = useState(); + const [preselect, setPreselect] = useState<{ + shoot: string; + key: number; + }>(); + const [chosenName, setChosenName] = useState(); + if (pakket !== seenPakket) { + setSeenPakket(pakket); + const id = + pakket === undefined ? undefined : packageFromSearch(pakket, shown); + if (id) { + setPreselect({ shoot: id, key: (preselect?.key ?? 0) + 1 }); + setChosenName(shown.find((pkg) => pkg.id === id)?.name); + } + } + const subject = mailSubject(chosenName); + + const { ref, inView } = useInView({ + rootMargin: DEFERRED_LOAD_MARGIN, + }); + // Once loaded the form stays, also after ?pakket= is removed from the + // URL; otherwise it would be recreated and lose what was typed + const [formLoaded, setFormLoaded] = useState(load === 'eager'); + if (!formLoaded && (inView || pakket !== undefined)) setFormLoaded(true); + const placeholder = ; + + return ( + + + {formLoaded ? ( + + + + ) : ( + placeholder + )} + + + ); +} diff --git a/web/src/components/Footer/Footer.test.tsx b/web/src/components/Footer/Footer.test.tsx index 5f153df..c44cb88 100644 --- a/web/src/components/Footer/Footer.test.tsx +++ b/web/src/components/Footer/Footer.test.tsx @@ -41,7 +41,10 @@ describe('Footer', () => { }); expect(link).toHaveAttribute('target', '_blank'); expect(link).toHaveAttribute('rel', 'noopener noreferrer'); - expect(link).toHaveAttribute('href', siteContent.socials[0].href); + expect(link).toHaveAttribute( + 'href', + siteContent.socials.find((s) => s.platform === 'facebook')?.href, + ); }); it('voegt het e-mailadres pas bij het tonen samen', async () => { diff --git a/web/src/components/Hero/Hero.test.tsx b/web/src/components/Hero/Hero.test.tsx index e19484f..c3f6923 100644 --- a/web/src/components/Hero/Hero.test.tsx +++ b/web/src/components/Hero/Hero.test.tsx @@ -21,8 +21,12 @@ describe('Hero', () => { const links = screen.getAllByRole('link', { name: /opent in nieuw tabblad/, }); + // In content order: Instagram, then Facebook expect(links).toHaveLength(2); expect(links[0]).toHaveAccessibleName( + 'Instagram (opent in nieuw tabblad)', + ); + expect(links[1]).toHaveAccessibleName( 'Facebook (opent in nieuw tabblad)', ); expect(links[0]).toHaveAttribute('rel', 'noopener noreferrer'); diff --git a/web/src/components/PrivacyNote/PrivacyNote.test.tsx b/web/src/components/PrivacyNote/PrivacyNote.test.tsx new file mode 100644 index 0000000..84be488 --- /dev/null +++ b/web/src/components/PrivacyNote/PrivacyNote.test.tsx @@ -0,0 +1,18 @@ +import { screen } from '@testing-library/react'; +import { describe, expect, it } from 'vitest'; +import { axeResults } from '@/test/axe'; +import { renderWithProviders } from '@/test/renderWithProviders'; +import PrivacyNote from './PrivacyNote'; + +describe('PrivacyNote', () => { + it('links to the privacy statement', async () => { + const { container } = await renderWithProviders(); + expect(screen.getByTestId('privacy-note')).toHaveTextContent( + 'Lees in de privacyverklaring wat ik met je gegevens doe.', + ); + expect( + screen.getByRole('link', { name: 'privacyverklaring' }), + ).toHaveAttribute('href', '/privacyverklaring'); + expect(await axeResults(container)).toHaveNoViolations(); + }); +}); diff --git a/web/src/components/PrivacyNote/PrivacyNote.tsx b/web/src/components/PrivacyNote/PrivacyNote.tsx new file mode 100644 index 0000000..94240e8 --- /dev/null +++ b/web/src/components/PrivacyNote/PrivacyNote.tsx @@ -0,0 +1,24 @@ +import { Link, Text, type TextProps } from '@chakra-ui/react'; +import RouterLink from '@/components/RouterLink'; + +// The line under a contact form (and under its "coming soon" notice) that +// tells visitors where to read what happens with their details (BR-PR-01). +// One component, so the text and the link never differ between places. +export default function PrivacyNote(props: TextProps) { + return ( + + Lees in de{' '} + + + privacyverklaring + + {' '} + wat ik met je gegevens doe. + + ); +} diff --git a/web/src/components/home/ContactSection/ContactSection.test.tsx b/web/src/components/home/ContactSection/ContactSection.test.tsx index 6a1e554..fbbfa76 100644 --- a/web/src/components/home/ContactSection/ContactSection.test.tsx +++ b/web/src/components/home/ContactSection/ContactSection.test.tsx @@ -14,6 +14,7 @@ const renderSection = (pakket?: string, path = '/') => packages={packages} enabled={false} email={siteContent.email} + replyTime={siteContent.contactForm.replyTime} pakket={pakket} />, { path }, diff --git a/web/src/components/home/ContactSection/ContactSection.tsx b/web/src/components/home/ContactSection/ContactSection.tsx index 34a6dde..aaae4c7 100644 --- a/web/src/components/home/ContactSection/ContactSection.tsx +++ b/web/src/components/home/ContactSection/ContactSection.tsx @@ -1,8 +1,7 @@ -import { lazy, Suspense, useEffect, useMemo, useState } from 'react'; -import { Box, useMediaQuery } from '@chakra-ui/react'; +import { useEffect } from 'react'; +import { useMediaQuery } from '@chakra-ui/react'; import { useNavigate } from '@tanstack/react-router'; -import { DEFERRED_LOAD_MARGIN } from '@/components/DeferredImage/DeferredImage'; -import ContactFormGate from '@/components/ContactForm/ContactFormGate'; +import ContactPanel from '@/components/ContactPanel/ContactPanel'; import PageSection from '@/components/PageSection/PageSection'; import Reveal from '@/components/Reveal/Reveal'; import SectionHeading from '@/components/SectionHeading/SectionHeading'; @@ -11,68 +10,34 @@ import type { HomeContent, PricingPackage, } from '@/content/types'; -import useInView from '@/hooks/useInView'; -import { mailSubject } from '@/lib/mailLink'; -import { packageFromSearch } from '@/lib/packageFromSearch'; -import { shootOptions } from '@/lib/shootOptions'; -import { visiblePackages } from '@/lib/visiblePackages'; - -// The form (Chakra fields, validation, reducer) sits far below the hero. -// Loading it with the page cost ±0.03 Lighthouse performance on mobile, so -// it loads when the section comes near the screen, or straight away when -// the visitor arrives via "Boek ...". -const ContactForm = lazy(() => import('@/components/ContactForm/ContactForm')); const TITLE_ID = 'contact-title'; -// Keeps the form's space while it loads, so the notice sits where it will -// stay and the page below does not jump (measured: 604px on phones, where -// name and e-mail are stacked, 502px from sm) -const FORM_PLACEHOLDER_HEIGHT = { base: '604px', sm: '502px' }; - type ContactSectionProps = { content: HomeContent['contact']; packages: PricingPackage[]; enabled: boolean; email: EmailAddress; + replyTime: string; // ?pakket= from a "Boek ..." link on a price card pakket?: string; }; // "Contact" on the homepage (design #contact): heading and lead, and the -// contact form behind the coming-soon gate. Takes over a package chosen via -// "Boek ...", then tidies the URL and moves focus here (BR-PARAM-02, -// BR-FOCUS-01). +// contact form, which sits far below the hero and so loads lazily. After a +// "Boek ..." click it tidies the URL and moves focus here (BR-PARAM-02, +// BR-FOCUS-01); the panel takes over the package itself. export default function ContactSection({ content, packages, enabled, email, + replyTime, pakket, }: ContactSectionProps) { const navigate = useNavigate(); - const shown = useMemo(() => visiblePackages(packages), [packages]); - const options = useMemo(() => shootOptions(packages), [packages]); - // Take over a new ?pakket= while rendering (React's pattern for a - // changed prop); the last "Boek" click wins (BR-PARAM-03) - const [seenPakket, setSeenPakket] = useState(); - const [preselect, setPreselect] = useState<{ - shoot: string; - key: number; - }>(); - const [chosenName, setChosenName] = useState(); - if (pakket !== seenPakket) { - setSeenPakket(pakket); - const id = - pakket === undefined ? undefined : packageFromSearch(pakket, shown); - if (id) { - setPreselect({ shoot: id, key: (preselect?.key ?? 0) + 1 }); - setChosenName(shown.find((pkg) => pkg.id === id)?.name); - } - } - - // Then remove ?pakket= from the address bar (no extra history entry) and + // Remove ?pakket= from the address bar (no extra history entry) and // continue keyboard and screen reader users here. This navigation // replaces the "Boek" click's before the router has scrolled for it, so // it must ask the router to scroll to #contact itself. @@ -98,16 +63,6 @@ export default function ContactSection({ // eslint-disable-next-line react-hooks/exhaustive-deps }, [pakket, navigate]); - const subject = mailSubject(chosenName); - const { ref, inView } = useInView({ - rootMargin: DEFERRED_LOAD_MARGIN, - }); - // Once loaded the form stays, also after ?pakket= is removed from the - // URL; otherwise it would be recreated and lose what was typed - const [formLoaded, setFormLoaded] = useState(false); - if (!formLoaded && (inView || pakket !== undefined)) setFormLoaded(true); - const placeholder = ; - return ( - - - {formLoaded ? ( - - - - ) : ( - placeholder - )} - - + replyTime={replyTime} + pakket={pakket} + load="lazy" + /> ); diff --git a/web/src/content/siteContent.ts b/web/src/content/siteContent.ts index 96e5382..8a86fbc 100644 --- a/web/src/content/siteContent.ts +++ b/web/src/content/siteContent.ts @@ -5,19 +5,22 @@ const siteContent: SiteContent = { // Elke zin op een eigen regel; de footer toont de regelafbreking footerIntro: 'Lifestyle fotograaf uit Huizen.\nIk leg jouw mooiste momenten vast.', + // In display order: Instagram first, her main channel socials: [ - { - platform: 'facebook', - href: 'https://www.facebook.com/andreaslopfotografie/', - }, { platform: 'instagram', href: 'https://www.instagram.com/andreaslopfotografie/', + label: '@andreaslopfotografie', + }, + { + platform: 'facebook', + href: 'https://www.facebook.com/andreaslopfotografie/', + label: 'Andrea Slop Fotografie', }, ], email: { user: 'info', domain: 'andreaslop.nl' }, // Switched on once the API can receive the form - contactForm: { enabled: false }, + contactForm: { enabled: false, replyTime: '1-2 werkdagen' }, // Huizen and the region around it (Bussum, Hilversum, …), as the pages // say; not the whole country, which would weaken the local signal serviceArea: [ diff --git a/web/src/content/types.ts b/web/src/content/types.ts index c52e799..613b29e 100644 --- a/web/src/content/types.ts +++ b/web/src/content/types.ts @@ -7,6 +7,9 @@ import type { SocialPlatform } from './socialPlatforms.ts'; export interface SocialLink { platform: SocialPlatform; href: string; // alleen getoond als geldige https-URL (zie visibleSocials) + // Link text in the contact card, e.g. '@andreaslopfotografie'; without + // it the card shows the platform name + label?: string; } // Het e-mailadres in delen: zo staat het volledige adres nergens letterlijk @@ -21,7 +24,9 @@ export interface SiteContent { socials: SocialLink[]; email: EmailAddress; // Off: the form is shown dimmed under a "coming soon" notice (FR-HP-07) - contactForm: { enabled: boolean }; + // replyTime: how soon Andrea answers, e.g. '1-2 werkdagen'; used by the + // form's thank-you and the contact page, so they always agree + contactForm: { enabled: boolean; replyTime: string }; // Where Andrea mainly works, for the structured data of the business // and its services. Matches what the pages say, not every place she // would travel to. diff --git a/web/src/lib/contactChannels.test.ts b/web/src/lib/contactChannels.test.ts new file mode 100644 index 0000000..a2886ef --- /dev/null +++ b/web/src/lib/contactChannels.test.ts @@ -0,0 +1,154 @@ +import fc from 'fast-check'; +import { describe, expect, it } from 'vitest'; +import siteContent from '../content/siteContent'; +import socialPlatforms from '../content/socialPlatforms'; +import { anyEmail, socialLink } from '../test/arbitraries'; +import { contactChannels } from './contactChannels'; +import { emailAddress } from './emailAddress'; +import { visibleSocials } from './visibleSocials'; + +const socialLinks = fc.array(socialLink, { maxLength: 5 }); + +describe('contactChannels', () => { + it('lists e-mail, Instagram and Facebook for the site content', () => { + expect(contactChannels(siteContent.email, siteContent.socials)).toEqual( + [ + { + kind: 'email', + href: 'mailto:info@andreaslop.nl?subject=Vraag%20via%20andreaslop.nl', + label: 'info@andreaslop.nl', + external: false, + }, + { + kind: 'social', + platform: 'instagram', + href: 'https://www.instagram.com/andreaslopfotografie/', + label: '@andreaslopfotografie', + external: true, + }, + { + kind: 'social', + platform: 'facebook', + href: 'https://www.facebook.com/andreaslopfotografie/', + label: 'Andrea Slop Fotografie', + external: true, + }, + ], + ); + }); + + it('leaves out an invalid e-mail address and invalid links', () => { + const channels = contactChannels({ user: 'info', domain: 'nodot' }, [ + { platform: 'facebook', href: 'javascript:alert(1)' }, + { platform: 'instagram', href: 'https://instagram.com/x' }, + ]); + expect(channels.map((channel) => channel.kind)).toEqual(['social']); + }); + + it('falls back to the platform name without a usable label', () => { + const channels = contactChannels({ user: '', domain: '' }, [ + { platform: 'instagram', href: 'https://instagram.com/x' }, + { + platform: 'facebook', + href: 'https://facebook.com/x', + label: ' ', + }, + ]); + expect(channels.map((channel) => channel.label)).toEqual([ + 'Instagram', + 'Facebook', + ]); + }); + + it('is empty when nothing is valid', () => { + expect(contactChannels({ user: '', domain: '' }, [])).toEqual([]); + }); + + it('puts the e-mail channel first exactly when the address is valid (invariant)', () => { + fc.assert( + fc.property(anyEmail, socialLinks, (email, socials) => { + const channels = contactChannels(email, socials); + const valid = emailAddress(email) !== null; + expect(channels[0]?.kind === 'email').toBe(valid); + expect( + channels.filter((channel) => channel.kind === 'email'), + ).toHaveLength(valid ? 1 : 0); + }), + ); + }); + + it('shows exactly the visible social links, in order (oracle)', () => { + fc.assert( + fc.property(anyEmail, socialLinks, (email, socials) => { + const shown = contactChannels(email, socials).flatMap( + (channel) => + channel.kind === 'social' + ? [ + { + platform: channel.platform, + href: channel.href, + }, + ] + : [], + ); + expect(shown).toEqual( + visibleSocials(socials).map(({ platform, href }) => ({ + platform, + href, + })), + ); + }), + ); + }); + + it('labels a social link with its trimmed label, or else the platform name (invariant)', () => { + fc.assert( + fc.property(socialLinks, (socials) => { + const channels = contactChannels( + { user: '', domain: '' }, + socials, + ); + visibleSocials(socials).forEach((link, index) => { + const expected = + link.label?.trim() || + socialPlatforms[link.platform].name; + expect(channels[index].label).toBe(expected); + expect(channels[index].label.trim()).not.toBe(''); + }); + }), + ); + }); + + it('only links to mailto: or https: (invariant)', () => { + fc.assert( + fc.property(anyEmail, socialLinks, (email, socials) => { + for (const channel of contactChannels(email, socials)) { + const scheme = new URL(channel.href).protocol; + expect( + channel.kind === 'email' ? 'mailto:' : 'https:', + ).toBe(scheme); + } + }), + ); + }); + + it('opens exactly the social channels in a new tab (invariant)', () => { + fc.assert( + fc.property(anyEmail, socialLinks, (email, socials) => { + for (const channel of contactChannels(email, socials)) { + expect(channel.external).toBe(channel.kind === 'social'); + } + }), + ); + }); + + it('has at most one channel per link plus the e-mail (invariant)', () => { + fc.assert( + fc.property(anyEmail, socialLinks, (email, socials) => { + expect( + contactChannels(email, socials).length, + ).toBeLessThanOrEqual(1 + socials.length); + }), + ); + }); +}); diff --git a/web/src/lib/contactChannels.ts b/web/src/lib/contactChannels.ts new file mode 100644 index 0000000..d237ac7 --- /dev/null +++ b/web/src/lib/contactChannels.ts @@ -0,0 +1,48 @@ +import socialPlatforms, { + type SocialPlatform, +} from '../content/socialPlatforms.ts'; +import type { EmailAddress, SocialLink } from '../content/types.ts'; +import { emailAddress } from './emailAddress.ts'; +import { mailSubject, mailtoLink } from './mailLink.ts'; +import { visibleSocials } from './visibleSocials.ts'; + +// One way to reach Andrea, as the contact card shows it +export type ContactChannel = + | { kind: 'email'; href: string; label: string; external: false } + | { + kind: 'social'; + platform: SocialPlatform; + href: string; + label: string; + // Opens in a new tab (BR-CD-05) + external: true; + }; + +// The channels of the contact card (BR-CD-01..05): the e-mail address first +// when it is valid, then the valid social links in content order, each with +// its label or else the platform name. Only mailto: and https: links come +// out, because both helpers already drop everything else. +export function contactChannels( + email: EmailAddress, + socials: readonly SocialLink[], +): ContactChannel[] { + const address = emailAddress(email); + const mail: ContactChannel[] = address + ? [ + { + kind: 'email', + href: mailtoLink(address, mailSubject()), + label: address, + external: false, + }, + ] + : []; + const social = visibleSocials(socials).map((link): ContactChannel => ({ + kind: 'social', + platform: link.platform, + href: link.href, + label: link.label?.trim() || socialPlatforms[link.platform].name, + external: true, + })); + return [...mail, ...social]; +} diff --git a/web/src/routes/index.tsx b/web/src/routes/index.tsx index 2869d7e..9580d6d 100644 --- a/web/src/routes/index.tsx +++ b/web/src/routes/index.tsx @@ -47,6 +47,7 @@ function Index() { packages={packages} enabled={siteContent.contactForm.enabled} email={siteContent.email} + replyTime={siteContent.contactForm.replyTime} pakket={pakket} /> {instagram && ( diff --git a/web/src/test/arbitraries.ts b/web/src/test/arbitraries.ts index 4601ca1..f770a86 100644 --- a/web/src/test/arbitraries.ts +++ b/web/src/test/arbitraries.ts @@ -39,10 +39,18 @@ export const anyHref = fc.oneof( fc.string(), ); -export const socialLink: fc.Arbitrary = fc.record({ - platform, - href: anyHref, -}); +export const socialLink: fc.Arbitrary = fc.record( + { + platform, + href: anyHref, + // Missing, blank, padded or ordinary, as an editor might type it + label: fc.oneof( + fc.constantFrom('', ' ', ' @andreaslopfotografie '), + fc.string({ maxLength: 30 }), + ), + }, + { requiredKeys: ['platform', 'href'] }, +); // Een geldig deel van een e-mailadres: geen spaties, geen @ const emailPart = fc.stringMatching(/^[a-z0-9._-]{1,20}$/); @@ -65,7 +73,7 @@ export const anySiteContent: fc.Arbitrary = fc.record({ footerIntro: fc.string(), socials: fc.array(socialLink, { maxLength: 4 }), email: validEmail, - contactForm: fc.record({ enabled: fc.boolean() }), + contactForm: fc.record({ enabled: fc.boolean(), replyTime: fc.string() }), serviceArea: fc.array( fc.record({ type: fc.constantFrom(