import React from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import snackbar from 'soapbox/actions/snackbar'; import { verifyAge } from 'soapbox/actions/verification'; import { Button, Datepicker, Form, Text } from 'soapbox/components/ui'; import { useAppDispatch, useAppSelector, useInstance } from 'soapbox/hooks'; const messages = defineMessages({ fail: { id: 'age_verification.fail', defaultMessage: 'You must be {ageMinimum, plural, one {# year} other {# years}} old or older.', }, }); function meetsAgeMinimum(birthday: Date, ageMinimum: number) { const month = birthday.getUTCMonth(); const day = birthday.getUTCDate(); const year = birthday.getUTCFullYear(); return new Date(year + ageMinimum, month, day) <= new Date(); } const AgeVerification = () => { const intl = useIntl(); const dispatch = useAppDispatch(); const instance = useInstance(); const isLoading = useAppSelector((state) => state.verification.isLoading) as boolean; const ageMinimum = useAppSelector((state) => state.verification.ageMinimum) as any; const [date, setDate] = React.useState(''); const isValid = typeof date === 'object'; const onChange = React.useCallback((date) => setDate(date), []); const handleSubmit = React.useCallback((event) => { event.preventDefault(); const birthday = new Date(date); if (meetsAgeMinimum(birthday, ageMinimum)) { dispatch(verifyAge(birthday)); } else { dispatch( snackbar.error(intl.formatMessage(messages.fail, { ageMinimum })), ); } }, [date, ageMinimum]); return (

); }; export default AgeVerification;