2022-06-09 12:22:19 -07:00
import React from 'react' ;
2022-05-24 03:24:26 -07:00
import { defineMessages , FormattedMessage , useIntl } from 'react-intl' ;
2022-03-21 11:09:01 -07:00
import snackbar from 'soapbox/actions/snackbar' ;
import { verifyAge } from 'soapbox/actions/verification' ;
2022-06-08 09:06:05 -07:00
import { Button , Datepicker , Form , Text } from 'soapbox/components/ui' ;
2022-11-26 08:38:16 -08:00
import { useAppDispatch , useAppSelector , useInstance } from 'soapbox/hooks' ;
2022-03-21 11:09:01 -07:00
2022-05-24 03:24:26 -07:00
const messages = defineMessages ( {
fail : {
id : 'age_verification.fail' ,
defaultMessage : 'You must be {ageMinimum, plural, one {# year} other {# years}} old or older.' ,
} ,
} ) ;
2022-03-21 11:09:01 -07:00
2022-06-09 12:22:19 -07:00
function meetsAgeMinimum ( birthday : Date , ageMinimum : number ) {
2022-03-21 11:09:01 -07:00
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 ( ) ;
2022-06-09 12:22:19 -07:00
const dispatch = useAppDispatch ( ) ;
2022-11-26 08:38:16 -08:00
const instance = useInstance ( ) ;
2022-03-21 11:09:01 -07:00
2022-06-20 06:46:43 -07:00
const isLoading = useAppSelector ( ( state ) = > state . verification . isLoading ) as boolean ;
const ageMinimum = useAppSelector ( ( state ) = > state . verification . ageMinimum ) as any ;
2022-03-21 11:09:01 -07:00
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 (
2022-06-09 12:22:19 -07:00
snackbar . error ( intl . formatMessage ( messages . fail , { ageMinimum } ) ) ,
2022-03-21 11:09:01 -07:00
) ;
}
} , [ date , ageMinimum ] ) ;
return (
< div >
2022-07-22 10:30:16 -07:00
< div className = 'pb-4 sm:pb-10 mb-4 border-b border-gray-200 dark:border-gray-800 border-solid -mx-4 sm:-mx-10' >
2022-03-21 11:09:01 -07:00
< h1 className = 'text-center font-bold text-2xl' >
2022-05-24 03:24:26 -07:00
< FormattedMessage id = 'age_verification.header' defaultMessage = 'Enter your birth date' / >
2022-03-21 11:09:01 -07:00
< / h1 >
< / div >
2022-06-08 10:13:44 -07:00
< div className = 'sm:pt-10 md:w-2/3 mx-auto' >
2022-04-04 08:54:00 -07:00
< Form onSubmit = { handleSubmit } >
2022-06-08 09:06:05 -07:00
< Datepicker onChange = { onChange } / >
2022-03-21 11:09:01 -07:00
< Text theme = 'muted' size = 'sm' >
2022-11-26 05:09:28 -08:00
< FormattedMessage
id = 'age_verification.body'
defaultMessage = '{siteTitle} requires users to be at least {ageMinimum} years old to access its platform. Anyone under the age of {ageMinimum} years old cannot access this platform.'
values = { {
2022-11-26 08:45:13 -08:00
siteTitle : instance.title ,
2022-11-26 05:09:28 -08:00
ageMinimum ,
} }
/ >
2022-03-21 11:09:01 -07:00
< / Text >
< div className = 'text-center' >
2022-11-26 05:09:28 -08:00
< Button block theme = 'primary' type = 'submit' disabled = { isLoading || ! isValid } >
< FormattedMessage id = 'onboarding.next' defaultMessage = 'Next' / >
< / Button >
2022-03-21 11:09:01 -07:00
< / div >
< / Form >
< / div >
< / div >
) ;
} ;
export default AgeVerification ;