import './style.css' import Box from '@mui/material/Box' import Modal from '@mui/material/Modal' import Tab from '@mui/material/Tab' import Tabs from '@mui/material/Tabs' import useMediaQuery from '@mui/material/useMediaQuery' import axios, { AxiosError } from 'axios' import { Field, Form, Formik } from 'formik' import _get from 'lodash/get' import _identity from 'lodash/identity' import React, { FC, useState } from 'react' import { authorize, getProfileData } from '../../api' import { isBrowser, setLoggedUserData } from '../../utils' import { requiredValidator } from '../../validators' import { CustomField } from '../common/CustomField' import { PoweredBy } from '../common/PoweredBy' import { COLORS, MODAL_DIMENSIONS, TAB_STYLES } from './constants' import { SignUpForm } from './SignUpForm' export interface Props { onClose: () => void; onLogin: (res: IProfileData) => void; alreadyHasUser?: boolean; userExpired?: boolean; onAuthorizeSuccess?: (res: any) => void; onAuthorizeError?: (e: AxiosError) => void; onGetProfileDataSuccess?: (res: any) => void; onGetProfileDataError?: (e: AxiosError) => void; onForgotPassword?: () => void; onSignup?: () => void; modalClassname?: string; logo?: string; showForgotPasswordButton?: boolean; showSignUpButton?: boolean; showPoweredByImage?: boolean; registerUrl?: string; } const style: React.CSSProperties = { position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%, -50%)', width: MODAL_DIMENSIONS.WIDTH.DESKTOP, backgroundColor: '#e3e3e3', border: `1px solid ${COLORS.BORDER}`, outline: 'none', } const mobileStyle: React.CSSProperties = { ...style, width: MODAL_DIMENSIONS.WIDTH.MOBILE, } export const LoginModal: FC = ({ onClose, onLogin, alreadyHasUser = false, userExpired = false, onGetProfileDataSuccess = _identity, onGetProfileDataError = _identity, onForgotPassword = _identity, onSignup = _identity, modalClassname = '', logo, showForgotPasswordButton = false, showSignUpButton = false, showPoweredByImage = false, }) => { const [error, setError] = useState('') const [activeTab, setActiveTab] = useState<'login' | 'signup'>('login') const isMobile = useMediaQuery('(max-width:600px)') // If onSignup is provided (custom callback), use old behavior const useCustomSignup = onSignup !== _identity const handleTabChange = (_event: React.SyntheticEvent, newValue: 'login' | 'signup') => { setActiveTab(newValue) } return (
{showSignUpButton && !useCustomSignup && ( )} {activeTab === 'login' ? ( { try { const body = { email, password } const authRes = await authorize(body) let profileResponse = null try { profileResponse = await getProfileData() onGetProfileDataSuccess(profileResponse.data) } catch (e) { if (axios.isAxiosError(e)) { onGetProfileDataError(e) } return } const profileSpecifiedData = profileResponse.data const profileDataObj = setLoggedUserData(profileSpecifiedData) if (isBrowser) { window.localStorage.setItem('user_data', JSON.stringify(profileDataObj)) const event = new window.CustomEvent('tf-login') window.document.dispatchEvent(event) } onLogin(_get(authRes, 'data.data', {}) as IProfileData) } catch (e) { if (axios.isAxiosError(e)) { const error = _get(e, 'response.data.message', 'Error') setError(error) } else if (e instanceof Error) { setError(e?.message || 'Error') } } }} > {props => (
logo
{error}
{alreadyHasUser && (

It appears this email is already attached to an account. Please log in here to complete your registration.

)} {userExpired && (

Your session has expired, please log in again.

)}
{showForgotPasswordButton && (
)} {showSignUpButton && useCustomSignup && (
)} {showPoweredByImage ? : null}
)}
) : ( )}
) }