From e14cbe41e5afb89d60de55e3e84f5f68878bec69 Mon Sep 17 00:00:00 2001 From: Shekar Siri Date: Tue, 6 Feb 2024 12:09:27 +0100 Subject: [PATCH] change(ui): login to functional component --- frontend/app/components/Login/Login.tsx | 221 ++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 frontend/app/components/Login/Login.tsx diff --git a/frontend/app/components/Login/Login.tsx b/frontend/app/components/Login/Login.tsx new file mode 100644 index 000000000..84cd2b562 --- /dev/null +++ b/frontend/app/components/Login/Login.tsx @@ -0,0 +1,221 @@ +import React, {useState, useEffect, useRef} from 'react'; +// import {useSelector, useDispatch} from 'react-redux'; +import {useHistory, useLocation} from 'react-router-dom'; +import {login, setJwt, fetchTenants} from 'Duck/user'; +import withPageTitle from 'HOCs/withPageTitle'; // Consider using a different approach for titles in functional components +import ReCAPTCHA from 'react-google-recaptcha'; +import {Button, Form, Input, Link, Loader, Popover, Tooltip, Icon} from 'UI'; +import {forgotPassword, signup} from 'App/routes'; +import LoginBg from '../../svg/login-illustration.svg'; +import {ENTERPRISE_REQUEIRED} from 'App/constants'; +import cn from 'classnames'; +import stl from './login.module.css'; +import Copyright from 'Shared/Copyright'; +import {connect} from 'react-redux'; + +const FORGOT_PASSWORD = forgotPassword(); +const SIGNUP_ROUTE = signup(); + +interface LoginProps { + errors: any; // Adjust the type based on your state shape + loading: boolean; + authDetails: any; // Adjust the type based on your state shape + login: typeof login; + setJwt: typeof setJwt; + fetchTenants: typeof fetchTenants; + location: Location; +} + +const Login: React.FC = ({errors, loading, authDetails, login, setJwt, fetchTenants, location}) => { + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + const [CAPTCHA_ENABLED, setCAPTCHA_ENABLED] = useState(window.env.CAPTCHA_ENABLED === 'true'); + const recaptchaRef = useRef(null); + + const history = useHistory(); + const params = new URLSearchParams(location.search); + + useEffect(() => { + if (Object.keys(authDetails).length !== 0) { + if (!authDetails.tenants) { + history.push(SIGNUP_ROUTE); + } + } + }, [authDetails]); + + useEffect(() => { + fetchTenants() + const jwt = params.get('jwt'); + if (jwt) { + setJwt(jwt); + } + }, []); + + const handleSubmit = (token?: string) => { + login({email: email.trim(), password, 'g-recaptcha-response': token}); + }; + + const onSubmit = (e: React.FormEvent) => { + e.preventDefault(); + if (CAPTCHA_ENABLED && recaptchaRef.current) { + recaptchaRef.current.execute(); + } else if (!CAPTCHA_ENABLED) { + handleSubmit(); + } + }; + + return ( +
+
+
+ +
+
+

+ Login to your account +

+
+
+ + {CAPTCHA_ENABLED && ( + handleSubmit(token)} + /> + )} +
+ + + setEmail(e.target.value)} + required + icon="envelope" + /> + + + + setPassword(e.target.value)} + required + icon="key" + /> + +
+
+ {errors && errors.length ? ( +
+ {errors.map((error) => ( +
+ + {error}
+
+ ))} +
+ ) : null} + +
+ + +
+ Having trouble logging in?{' '} + + {'Reset password'} + +
+
+
+ +
+ {authDetails.sso ? ( + + + + ) : ( + + {authDetails.edition === 'ee' ? ( + + SSO has not been configured.
Please reach out to your admin. +
+ ) : ( + ENTERPRISE_REQUEIRED + )} +
+ } + placement="top" + > + + + )} +
+
+ +
+
+ + + + ); +}; + +const mapStateToProps = (state: any, ownProps: any) => ({ + errors: state.getIn(['user', 'loginRequest', 'errors']), + loading: state.getIn(['user', 'loginRequest', 'loading']), + authDetails: state.getIn(['user', 'authDetails']), + params: new URLSearchParams(ownProps.location.search), +}); + +const mapDispatchToProps = { + login, + setJwt, + fetchTenants, +}; + +export default withPageTitle('Login - OpenReplay')( + connect(mapStateToProps, mapDispatchToProps)(Login) +); \ No newline at end of file