'use client'; /* This example requires Tailwind CSS v2.0+ */ import { FC, Fragment, useEffect, useRef, useState, useTransition } from 'react'; import { Dialog, Transition } from '@headlessui/react'; import { CheckIcon } from '@heroicons/react/24/outline'; import { useAgeConfirmation } from 'app/hooks/use-age-confirmation'; import clsx from 'clsx'; import { isBefore, isValid, parse } from 'date-fns'; import { useTranslations } from 'next-intl'; import { useRouter } from 'next/navigation'; type AgeGateFormProps = { checkoutUrl: string; didCancel?: () => void; }; const AgeGateForm: FC = ({ checkoutUrl, didCancel }) => { const t = useTranslations('Index'); const router = useRouter(); const [isPending, startTransition] = useTransition(); const [hasValidDate, setHasValidDate] = useState(false); const [month, setMonth] = useState(); const [day, setDay] = useState(); const [year, setYear] = useState(); const { onAgeConfirmed } = useAgeConfirmation(); const yearFieldRef = useRef(null); const minAge = 20; const maxAge = 130; const save = () => { if (hasValidDate) { onAgeConfirmed(); startTransition(() => { router.push(checkoutUrl); }); } }; const cancel = () => { if (didCancel) { didCancel(); } }; useEffect(() => { const now = new Date(); const thresholdDate = new Date(now.getFullYear() - minAge, now.getMonth(), now.getDate()); const minDate = new Date(now.getFullYear() - maxAge, now.getMonth(), now.getDate()); if (month && day && year) { const date = parse(`${month}-${day}-${year}`, 'MM-dd-yyyy', new Date()); const oldEnough = isBefore(date, thresholdDate); const tooOld = isBefore(date, minDate); setHasValidDate(isValid(date) && oldEnough && !tooOld); } else { setHasValidDate(false); } }, [month, day, year]); return ( <> {}}> ); }; export default AgeGateForm;