|
| 1 | +import { |
| 2 | + TextInputField, |
| 3 | + majorScale, |
| 4 | + Button, |
| 5 | + Alert, |
| 6 | + Link, |
| 7 | + Heading, |
| 8 | +} from "evergreen-ui"; |
| 9 | +import { useBoolean } from "utils/hooks/use-boolean"; |
| 10 | +import { useLogin } from "utils/hooks/supabase/use-login"; |
| 11 | +import { useRegister } from "utils/hooks/supabase/use-register"; |
| 12 | +import { Form } from "components/forms/form"; |
| 13 | +import { FormEvent, MouseEvent, useCallback, useMemo } from "react"; |
| 14 | +import { isNilOrEmpty } from "utils/core-utils"; |
| 15 | +import { Flex } from "components/flex"; |
| 16 | +import { SupabaseUser } from "types/supabase-user"; |
| 17 | +import { UserRecord } from "models/user-record"; |
| 18 | +import { useCreateOrUpdateUser } from "generated/hooks/domain/users/use-create-or-update-user"; |
| 19 | +import { useGlobalState } from "utils/hooks/use-global-state"; |
| 20 | +import { useNavigate } from "react-router"; |
| 21 | +import { Link as ReactRouterLink } from "react-router-dom"; |
| 22 | +import { Sitemap } from "sitemap"; |
| 23 | +import { ErrorAlert } from "components/error-alert"; |
| 24 | +import { absolutePath } from "utils/route-utils"; |
| 25 | +import { useInput } from "utils/hooks/use-input"; |
| 26 | + |
| 27 | +interface LoginOrRegisterFormProps { |
| 28 | + initialShowRegister: boolean; |
| 29 | +} |
| 30 | + |
| 31 | +const marginBottom = majorScale(3); |
| 32 | + |
| 33 | +const LoginOrRegisterForm: React.FC<LoginOrRegisterFormProps> = ( |
| 34 | + props: LoginOrRegisterFormProps |
| 35 | +) => { |
| 36 | + const { initialShowRegister } = props; |
| 37 | + const { setGlobalState } = useGlobalState(); |
| 38 | + const navigate = useNavigate(); |
| 39 | + const { value: showRegister, toggle: toggleShowRegister } = |
| 40 | + useBoolean(initialShowRegister); |
| 41 | + const { |
| 42 | + value: email, |
| 43 | + onChange: handleEmailChange, |
| 44 | + ...emailValidation |
| 45 | + } = useInput({ |
| 46 | + initialValue: "", |
| 47 | + isRequired: true, |
| 48 | + }); |
| 49 | + const { |
| 50 | + value: password, |
| 51 | + onChange: handlePasswordChange, |
| 52 | + ...passwordValidation |
| 53 | + } = useInput({ |
| 54 | + initialValue: "", |
| 55 | + isRequired: true, |
| 56 | + }); |
| 57 | + const { mutate: createOrUpdateUser } = useCreateOrUpdateUser({ |
| 58 | + onConflict: "id", |
| 59 | + }); |
| 60 | + const { |
| 61 | + mutate: register, |
| 62 | + reset: resetRegister, |
| 63 | + isLoading: isRegisterLoading, |
| 64 | + isSuccess: isRegisterSuccess, |
| 65 | + error: registerError, |
| 66 | + } = useRegister(); |
| 67 | + const { |
| 68 | + mutate: login, |
| 69 | + reset: resetLogin, |
| 70 | + isLoading: isLoginLoading, |
| 71 | + error: loginError, |
| 72 | + } = useLogin({ |
| 73 | + onSuccess: (supabaseUser: SupabaseUser) => { |
| 74 | + const user = UserRecord.fromSupabaseUser(supabaseUser); |
| 75 | + createOrUpdateUser(user); |
| 76 | + setGlobalState((prev) => prev.setUser(supabaseUser)); |
| 77 | + navigate(Sitemap.home); |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + const error = loginError ?? registerError; |
| 82 | + |
| 83 | + const renderError = |
| 84 | + (showRegister && registerError != null) || |
| 85 | + (!showRegister && loginError != null); |
| 86 | + const buttonText = showRegister ? "Register" : "Login"; |
| 87 | + const toggleLinkText = showRegister |
| 88 | + ? "Already have an account? Login here" |
| 89 | + : "Need an account? Register here"; |
| 90 | + |
| 91 | + const validate = useCallback((): boolean => { |
| 92 | + const emailIsInvalid = isNilOrEmpty(email); |
| 93 | + const passwordIsInvalid = isNilOrEmpty(password); |
| 94 | + const isValid = !emailIsInvalid && !passwordIsInvalid; |
| 95 | + return isValid; |
| 96 | + }, [email, password]); |
| 97 | + |
| 98 | + const handleLogin = useCallback( |
| 99 | + (event: FormEvent<HTMLFormElement> | MouseEvent<HTMLButtonElement>) => { |
| 100 | + event.preventDefault(); |
| 101 | + if (!validate()) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + login({ email: email!, password: password! }); |
| 106 | + }, |
| 107 | + [email, login, password, validate] |
| 108 | + ); |
| 109 | + |
| 110 | + const handleRegister = useCallback( |
| 111 | + (event: FormEvent<HTMLFormElement> | MouseEvent<HTMLButtonElement>) => { |
| 112 | + event.preventDefault(); |
| 113 | + |
| 114 | + if (!validate()) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + register({ email: email!, password: password! }); |
| 119 | + }, |
| 120 | + [email, password, register, validate] |
| 121 | + ); |
| 122 | + |
| 123 | + const handleSubmit = useMemo( |
| 124 | + () => (showRegister ? handleRegister : handleLogin), |
| 125 | + [handleLogin, handleRegister, showRegister] |
| 126 | + ); |
| 127 | + |
| 128 | + const toggleAndReset = useCallback(() => { |
| 129 | + toggleShowRegister(); |
| 130 | + resetLogin(); |
| 131 | + resetRegister(); |
| 132 | + }, [resetLogin, resetRegister, toggleShowRegister]); |
| 133 | + |
| 134 | + return ( |
| 135 | + <Flex.Column alignItems="center" maxWidth={majorScale(60)}> |
| 136 | + <Heading marginBottom={majorScale(2)} size={800}> |
| 137 | + {showRegister ? "Register" : "Login"} |
| 138 | + </Heading> |
| 139 | + <Form |
| 140 | + display="flex" |
| 141 | + flexDirection="column" |
| 142 | + onSubmit={handleSubmit} |
| 143 | + width={majorScale(30)}> |
| 144 | + <TextInputField |
| 145 | + {...emailValidation} |
| 146 | + disabled={showRegister ? isRegisterLoading : isLoginLoading} |
| 147 | + label="Email" |
| 148 | + onChange={handleEmailChange} |
| 149 | + value={email} |
| 150 | + /> |
| 151 | + <TextInputField |
| 152 | + {...passwordValidation} |
| 153 | + disabled={showRegister ? isRegisterLoading : isLoginLoading} |
| 154 | + label="Password" |
| 155 | + onChange={handlePasswordChange} |
| 156 | + type="password" |
| 157 | + value={password} |
| 158 | + /> |
| 159 | + <Button |
| 160 | + isLoading={ |
| 161 | + showRegister ? isRegisterLoading : isLoginLoading |
| 162 | + } |
| 163 | + marginBottom={marginBottom} |
| 164 | + onClick={handleSubmit}> |
| 165 | + {buttonText} |
| 166 | + </Button> |
| 167 | + <Link marginBottom={marginBottom} onClick={toggleAndReset}> |
| 168 | + {toggleLinkText} |
| 169 | + </Link> |
| 170 | + <Link |
| 171 | + is={ReactRouterLink} |
| 172 | + marginBottom={marginBottom} |
| 173 | + to={absolutePath(Sitemap.resetPassword)}> |
| 174 | + Forgot your password? |
| 175 | + </Link> |
| 176 | + </Form> |
| 177 | + {renderError && <ErrorAlert error={error} />} |
| 178 | + {isRegisterSuccess && showRegister && ( |
| 179 | + <Alert intent="success" title="Account successfully created."> |
| 180 | + Check your email for a confirmation link to sign in. |
| 181 | + </Alert> |
| 182 | + )} |
| 183 | + </Flex.Column> |
| 184 | + ); |
| 185 | +}; |
| 186 | + |
| 187 | +export { LoginOrRegisterForm }; |
0 commit comments