import React, { useMemo, useState, useCallback } from 'react'; import * as Yup from 'yup'; import { useFormik } from 'formik'; import { Row, Col } from 'react-grid-system'; import { Link, useHistory } from 'react-router-dom'; import { Button, InputGroup, Intent, FormGroup, Spinner, } from '@blueprintjs/core'; import { FormattedMessage as T, useIntl } from 'react-intl'; import AppToaster from 'components/AppToaster'; import ErrorMessage from 'components/ErrorMessage'; import Icon from 'components/Icon'; import { If } from 'components'; import withAuthenticationActions from 'containers/Authentication/withAuthenticationActions'; import { compose } from 'utils'; function RegisterUserForm({ requestRegister, requestLogin }) { const { formatMessage } = useIntl(); const history = useHistory(); const [shown, setShown] = useState(false); const passwordRevealer = useCallback(() => { setShown(!shown); }, [shown]); const ValidationSchema = Yup.object().shape({ first_name: Yup.string() .required() .label(formatMessage({ id: 'first_name_' })), last_name: Yup.string() .required() .label(formatMessage({ id: 'last_name_' })), email: Yup.string() .email() .required() .label(formatMessage({ id: 'email' })), phone_number: Yup.string() .matches() .required() .label(formatMessage({ id: 'phone_number_' })), password: Yup.string() .min(4) .required() .label(formatMessage({ id: 'password' })), }); const initialValues = useMemo( () => ({ first_name: '', last_name: '', email: '', phone_number: '', password: '', }), [], ); const { errors, touched, handleSubmit, getFieldProps, isSubmitting, } = useFormik({ enableReinitialize: true, validationSchema: ValidationSchema, initialValues: { ...initialValues, country: 'libya', }, onSubmit: (values, { setSubmitting, setErrors }) => { requestRegister(values) .then((response) => { requestLogin({ crediential: values.email, password: values.password, }) .then(() => { history.push('/register/subscription'); setSubmitting(false); }) .catch((errors) => { AppToaster.show({ message: formatMessage({ id: 'something_wentwrong', }), intent: Intent.SUCCESS, }); }); }) .catch((errors) => { if (errors.some((e) => e.type === 'PHONE_NUMBER_EXISTS')) { setErrors({ phone_number: formatMessage({ id: 'the_phone_number_already_used_in_another_account', }), }); } if (errors.some((e) => e.type === 'EMAIL_EXISTS')) { setErrors({ email: formatMessage({ id: 'the_email_already_used_in_another_account', }), }); } setSubmitting(false); }); }, }); const passwordRevealerTmp = useMemo( () => ( passwordRevealer()}> <> {' '} <> {' '} ), [shown, passwordRevealer], ); return (

{' '}
} intent={ errors.first_name && touched.first_name && Intent.DANGER } helperText={ } className={'form-group--first-name'} > } intent={errors.last_name && touched.last_name && Intent.DANGER} helperText={ } className={'form-group--last-name'} > } intent={ errors.phone_number && touched.phone_number && Intent.DANGER } helperText={ } className={'form-group--phone-number'} > } intent={errors.email && touched.email && Intent.DANGER} helperText={ } className={'form-group--email'} > } labelInfo={passwordRevealerTmp} intent={errors.password && touched.password && Intent.DANGER} helperText={ } className={'form-group--password has-password-revealer'} >


{' '} {' '}

); } export default compose(withAuthenticationActions)(RegisterUserForm);