import React, { useState, useRef, ChangeEvent, FormEvent, useEffect } from 'react'; import { Form, Input, Button, Link } from 'UI'; import { login } from 'App/routes'; import ReCAPTCHA from 'react-google-recaptcha'; import { signup } from 'Duck/user'; import { connect, ConnectedProps } from 'react-redux'; import Select from 'Shared/Select'; import { SITE_ID_STORAGE_KEY } from 'App/constants/storageKeys'; import { validatePassword } from 'App/validate'; import { PASSWORD_POLICY } from 'App/constants'; import { Alert, Space } from 'antd'; import { toast } from 'react-toastify'; const LOGIN_ROUTE = login(); const mapState = (state: any) => ({ tenants: state.getIn(['user', 'tenants']), errors: state.getIn(['user', 'signupRequest', 'errors']), loading: state.getIn(['user', 'signupRequest', 'loading']) }); const mapDispatch = { signup }; const connector = connect(mapState, mapDispatch); type PropsFromRedux = ConnectedProps; type SignupFormProps = PropsFromRedux; const SignupForm: React.FC = ({ tenants, errors, loading, signup }) => { const [state, setState] = useState({ tenantId: '', fullname: '', password: '', email: '', projectName: '', organizationName: '', reload: false, CAPTCHA_ENABLED: window.env.CAPTCHA_ENABLED === 'true' }); const recaptchaRef = useRef(null); const [passwordError, setPasswordError] = useState(null); const handleSubmit = (token: string) => { const { tenantId, fullname, password, email, projectName, organizationName, auth } = state; if (!validatePassword(password)) return; localStorage.removeItem(SITE_ID_STORAGE_KEY); signup({ tenantId, fullname, password, email, projectName, organizationName, auth, 'g-recaptcha-response': token }).then((resp: any) => { if (resp && resp.errors && Array.isArray(resp.errors) && resp.errors.length > 0) { resp.errors[0] ? toast.error(resp.errors[0]) : toast.error('Something went wrong'); } }); setState({ ...state, reload: true }); }; const write = ({ target: { value, name } }: ChangeEvent) => setState({ ...state, [name]: value }); const writeOption = ({ name, value }: { name: string; value: { value: string } }) => setState({ ...state, [name]: value.value }); const onSubmit = (e: FormEvent) => { e.preventDefault(); const { CAPTCHA_ENABLED } = state; if (CAPTCHA_ENABLED && recaptchaRef.current) { recaptchaRef.current.execute(); } else if (!CAPTCHA_ENABLED) { handleSubmit(''); } }; useEffect(() => { if (state.password && !validatePassword(state.password)) { setPasswordError('Password must be at least 8 characters long'); } else { setPasswordError(null); } }, [state.password]); return (
Logo

Create Account

<> {state.CAPTCHA_ENABLED && ( handleSubmit(token || '')} /> )}
{tenants.length > 0 && ( {passwordError && ( // )} {errors && errors.length && ( )}
By signing up, you agree to our{' '} terms of service {' '} and{' '} privacy policy .
Already having an account?{' '} Login
); }; export default connector(SignupForm);