Shares the contact form, contact card and privacy line
ContactPanel holds the coming-soon gate, the shoot options, the package chosen via ?pakket= and the form, which loads as its own chunk either when it comes near the screen (homepage) or right away (contact page). ContactDetails shows the ways to reach Andrea as a card with icons. A privacy line now sits under the form and under the coming-soon notice, and the reply time in the thank-you comes from the site content. Instagram is listed before Facebook, so the footer and hero follow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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<React.ComponentProps<typeof ContactPanel>>;
|
||||
|
||||
const panel = (props: Props = {}) => (
|
||||
<ContactPanel
|
||||
idPrefix="test"
|
||||
packages={packages}
|
||||
enabled={false}
|
||||
email={siteContent.email}
|
||||
replyTime="1-2 werkdagen"
|
||||
load="eager"
|
||||
{...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<string | undefined>('newborn');
|
||||
return (
|
||||
<>
|
||||
<button onClick={() => setPakket(undefined)}>wis</button>
|
||||
<button onClick={() => setPakket('newborn')}>boek</button>
|
||||
{panel({ enabled: true, pakket })}
|
||||
</>
|
||||
);
|
||||
}
|
||||
await renderWithProviders(<Harness />);
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -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<string | undefined>();
|
||||
const [preselect, setPreselect] = useState<{
|
||||
shoot: string;
|
||||
key: number;
|
||||
}>();
|
||||
const [chosenName, setChosenName] = useState<string>();
|
||||
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<HTMLDivElement>({
|
||||
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 = <Box minH={FORM_PLACEHOLDER_HEIGHT} />;
|
||||
|
||||
return (
|
||||
<ContactFormGate enabled={enabled} email={email} subject={subject}>
|
||||
<Box ref={ref}>
|
||||
{formLoaded ? (
|
||||
<Suspense fallback={placeholder}>
|
||||
<ContactForm
|
||||
idPrefix={idPrefix}
|
||||
options={options}
|
||||
preselect={preselect}
|
||||
mailSubject={subject}
|
||||
email={email}
|
||||
replyTime={replyTime}
|
||||
/>
|
||||
</Suspense>
|
||||
) : (
|
||||
placeholder
|
||||
)}
|
||||
</Box>
|
||||
</ContactFormGate>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user