import PropTypes from 'prop-types'; import * as React from 'react'; import DatePicker from 'react-datepicker'; import { useIntl } from 'react-intl'; import { useDispatch, useSelector } from 'react-redux'; import snackbar from 'soapbox/actions/snackbar'; import { verifyAge } from 'soapbox/actions/verification'; import { Button, Form, FormGroup, Text } from '../../../components/ui'; function meetsAgeMinimum(birthday, ageMinimum) { const month = birthday.getUTCMonth(); const day = birthday.getUTCDate(); const year = birthday.getUTCFullYear(); return new Date(year + ageMinimum, month, day) <= new Date(); } function getMaximumDate(ageMinimum) { const date = new Date(); date.setUTCFullYear(date.getUTCFullYear() - ageMinimum); return date; } const AgeVerification = () => { const intl = useIntl(); const dispatch = useDispatch(); const isLoading = useSelector((state) => state.getIn(['verification', 'isLoading'])); const ageMinimum = useSelector((state) => state.getIn(['verification', 'ageMinimum'])); const siteTitle = useSelector((state) => state.instance.title); 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({ id: 'age_verification.fail', defaultMessage: `You must be ${ageMinimum} years old or older.`, }), ), ); } }, [date, ageMinimum]); return (