diff --git a/web/account/log-in-form.react.js b/web/account/log-in-form.react.js --- a/web/account/log-in-form.react.js +++ b/web/account/log-in-form.react.js @@ -1,194 +1,42 @@ // @flow import { useConnectModal } from '@rainbow-me/rainbowkit'; -import invariant from 'invariant'; import * as React from 'react'; import { useSigner } from 'wagmi'; -import { logInActionTypes, logIn } from 'lib/actions/user-actions'; -import { useModalContext } from 'lib/components/modal-provider.react'; -import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors'; -import { - oldValidUsernameRegex, - validEmailRegex, -} from 'lib/shared/account-utils'; -import { - type LogInExtraInfo, - type LogInStartingPayload, - logInActionSources, -} from 'lib/types/account-types'; -import { - useDispatchActionPromise, - useServerCall, -} from 'lib/utils/action-utils'; import { isDev } from 'lib/utils/dev-utils'; -import Button from '../components/button.react'; -import LoadingIndicator from '../loading-indicator.react'; -import Input from '../modals/input.react'; -import { useSelector } from '../redux/redux-utils'; -import { webLogInExtraInfoSelector } from '../selectors/account-selectors'; import css from './log-in-form.css'; -import PasswordInput from './password-input.react'; import SIWEButton from './siwe-button.react.js'; import SIWE from './siwe.react.js'; +import TraditionalLoginForm from './traditional-login-form.react.js'; -const loadingStatusSelector = createLoadingStatusSelector(logInActionTypes); function LoginForm(): React.Node { const { openConnectModal } = useConnectModal(); const { data: signer } = useSigner(); - const inputDisabled = useSelector(loadingStatusSelector) === 'loading'; - const loginExtraInfo = useSelector(webLogInExtraInfoSelector); - const callLogIn = useServerCall(logIn); - const dispatchActionPromise = useDispatchActionPromise(); - const modalContext = useModalContext(); - - const [username, setUsername] = React.useState(''); - const [password, setPassword] = React.useState(''); - const [errorMessage, setErrorMessage] = React.useState(''); const [ siweAuthFlowSelected, setSIWEAuthFlowSelected, ] = React.useState(false); - const usernameInputRef = React.useRef(); - - React.useEffect(() => { - usernameInputRef.current?.focus(); - }, []); - - const onUsernameChange = React.useCallback(e => { - invariant(e.target instanceof HTMLInputElement, 'target not input'); - setUsername(e.target.value); - }, []); - - const onUsernameBlur = React.useCallback(() => { - setUsername(untrimmedUsername => untrimmedUsername.trim()); - }, []); - - const onPasswordChange = React.useCallback(e => { - invariant(e.target instanceof HTMLInputElement, 'target not input'); - setPassword(e.target.value); - }, []); - - const logInAction = React.useCallback( - async (extraInfo: LogInExtraInfo) => { - try { - const result = await callLogIn({ - ...extraInfo, - username, - password, - logInActionSource: logInActionSources.logInFromWebForm, - }); - modalContext.popModal(); - return result; - } catch (e) { - setUsername(''); - setPassword(''); - if (e.message === 'invalid_credentials') { - setErrorMessage('incorrect username or password'); - } else { - setErrorMessage('unknown error'); - } - usernameInputRef.current?.focus(); - throw e; - } - }, - [callLogIn, modalContext, password, username], - ); - - const onSubmit = React.useCallback( - (event: SyntheticEvent) => { - event.preventDefault(); - - if (username.search(validEmailRegex) > -1) { - setUsername(''); - setErrorMessage('usernames only, not emails'); - usernameInputRef.current?.focus(); - return; - } else if (username.search(oldValidUsernameRegex) === -1) { - setUsername(''); - setErrorMessage('alphanumeric usernames only'); - usernameInputRef.current?.focus(); - return; - } else if (password === '') { - setErrorMessage('password is empty'); - usernameInputRef.current?.focus(); - return; - } - - const extraInfo = loginExtraInfo(); - dispatchActionPromise( - logInActionTypes, - logInAction(extraInfo), - undefined, - ({ calendarQuery: extraInfo.calendarQuery }: LogInStartingPayload), - ); - }, - [dispatchActionPromise, logInAction, loginExtraInfo, username, password], - ); - - const loginButtonContent = React.useMemo(() => { - if (inputDisabled) { - return ; - } - return 'Log in'; - }, [inputDisabled]); - const onSIWEButtonClick = React.useCallback(() => { setSIWEAuthFlowSelected(true); openConnectModal && openConnectModal(); }, [openConnectModal]); - let siwe; + let siweSection; if (isDev && siweAuthFlowSelected && signer) { - siwe = ; + siweSection = ; } else if (isDev) { - siwe = ; + siweSection = ; } return (
-
-
-
Username
-
- -
-
-
-
Password
-
- -
-
-
- -
- {siwe} -
{errorMessage}
-
-
+ + {siweSection ?
: null} + {siweSection}
); } diff --git a/web/account/log-in-form.react.js b/web/account/traditional-login-form.react.js copy from web/account/log-in-form.react.js copy to web/account/traditional-login-form.react.js --- a/web/account/log-in-form.react.js +++ b/web/account/traditional-login-form.react.js @@ -1,63 +1,47 @@ // @flow -import { useConnectModal } from '@rainbow-me/rainbowkit'; import invariant from 'invariant'; import * as React from 'react'; -import { useSigner } from 'wagmi'; -import { logInActionTypes, logIn } from 'lib/actions/user-actions'; -import { useModalContext } from 'lib/components/modal-provider.react'; -import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors'; +import { logIn, logInActionTypes } from 'lib/actions/user-actions.js'; +import { useModalContext } from 'lib/components/modal-provider.react.js'; +import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors.js'; import { oldValidUsernameRegex, validEmailRegex, -} from 'lib/shared/account-utils'; -import { - type LogInExtraInfo, - type LogInStartingPayload, - logInActionSources, -} from 'lib/types/account-types'; +} from 'lib/shared/account-utils.js'; +import type { + LogInExtraInfo, + LogInStartingPayload, +} from 'lib/types/account-types.js'; +import { logInActionSources } from 'lib/types/account-types.js'; import { useDispatchActionPromise, useServerCall, -} from 'lib/utils/action-utils'; -import { isDev } from 'lib/utils/dev-utils'; - -import Button from '../components/button.react'; -import LoadingIndicator from '../loading-indicator.react'; -import Input from '../modals/input.react'; -import { useSelector } from '../redux/redux-utils'; -import { webLogInExtraInfoSelector } from '../selectors/account-selectors'; +} from 'lib/utils/action-utils.js'; + +import Button from '../components/button.react.js'; +import LoadingIndicator from '../loading-indicator.react.js'; +import Input from '../modals/input.react.js'; +import { useSelector } from '../redux/redux-utils.js'; +import { webLogInExtraInfoSelector } from '../selectors/account-selectors.js'; import css from './log-in-form.css'; -import PasswordInput from './password-input.react'; -import SIWEButton from './siwe-button.react.js'; -import SIWE from './siwe.react.js'; +import PasswordInput from './password-input.react.js'; const loadingStatusSelector = createLoadingStatusSelector(logInActionTypes); -function LoginForm(): React.Node { - const { openConnectModal } = useConnectModal(); - const { data: signer } = useSigner(); +function TraditionalLoginForm(): React.Node { const inputDisabled = useSelector(loadingStatusSelector) === 'loading'; const loginExtraInfo = useSelector(webLogInExtraInfoSelector); const callLogIn = useServerCall(logIn); - const dispatchActionPromise = useDispatchActionPromise(); const modalContext = useModalContext(); - const [username, setUsername] = React.useState(''); - const [password, setPassword] = React.useState(''); - const [errorMessage, setErrorMessage] = React.useState(''); - const [ - siweAuthFlowSelected, - setSIWEAuthFlowSelected, - ] = React.useState(false); - const usernameInputRef = React.useRef(); - React.useEffect(() => { usernameInputRef.current?.focus(); }, []); + const [username, setUsername] = React.useState(''); const onUsernameChange = React.useCallback(e => { invariant(e.target instanceof HTMLInputElement, 'target not input'); setUsername(e.target.value); @@ -67,11 +51,14 @@ setUsername(untrimmedUsername => untrimmedUsername.trim()); }, []); + const [password, setPassword] = React.useState(''); const onPasswordChange = React.useCallback(e => { invariant(e.target instanceof HTMLInputElement, 'target not input'); setPassword(e.target.value); }, []); + const [errorMessage, setErrorMessage] = React.useState(''); + const logInAction = React.useCallback( async (extraInfo: LogInExtraInfo) => { try { @@ -136,61 +123,45 @@ return 'Log in'; }, [inputDisabled]); - const onSIWEButtonClick = React.useCallback(() => { - setSIWEAuthFlowSelected(true); - openConnectModal && openConnectModal(); - }, [openConnectModal]); - - let siwe; - if (isDev && siweAuthFlowSelected && signer) { - siwe = ; - } else if (isDev) { - siwe = ; - } - return ( -
-
-
-
Username
-
- -
-
-
-
Password
-
- -
+ +
+
Username
+
+
-
- -
- {siwe} -
{errorMessage}
+ />
- -
+
+
+ +
{errorMessage}
+
+ ); } -export default LoginForm; +export default TraditionalLoginForm;