From 1ea66824d06c215d2f014fe9a4603535173dbcc9 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Sun, 9 Apr 2023 14:52:10 +0100 Subject: [PATCH] Refactor email sign up form --- src/components/Login/EmailSignupForm.tsx | 144 +++++++++------------ src/components/SocialAuth/GitHubButton.tsx | 2 +- src/components/SocialAuth/GoogleButton.tsx | 5 +- 3 files changed, 61 insertions(+), 90 deletions(-) diff --git a/src/components/Login/EmailSignupForm.tsx b/src/components/Login/EmailSignupForm.tsx index 24d6b5516..1cacffc02 100644 --- a/src/components/Login/EmailSignupForm.tsx +++ b/src/components/Login/EmailSignupForm.tsx @@ -1,76 +1,63 @@ -import Cookies from 'js-cookie'; import type { FunctionComponent } from 'preact'; import { useState } from 'preact/hooks'; -import { TOKEN_COOKIE_NAME } from '../../lib/constants'; -import Spinner from '../Spinner'; -const EmailSignupForm: FunctionComponent<{}> = () => { - const [email, setEmail] = useState(''); - const [password, setPassword] = useState(''); - const [name, setName] = useState(''); +const EmailSignupForm: FunctionComponent = () => { + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + const [name, setName] = useState(''); - const [isLoading, setIsLoading] = useState(false); - const [message, setMessage] = useState<{ - type: 'success' | 'error' | 'verification' | 'warning'; - message: string; - } | null>(null); + const [error, setError] = useState(''); + const [isLoading, setIsLoading] = useState(false); + + const handleRegisterResponse = async (res: Response) => { + const json = await res.json(); + + if (!res.ok) { + setError(json.message || 'Something went wrong. Please try again later.'); + setIsLoading(false); + + return; + } + + window.location.href = `/verify?email=${email}`; + }; + + const onSubmit = (e: Event) => { + e.preventDefault(); + + setIsLoading(true); + fetch(`${import.meta.env.PUBLIC_API_URL}/v1-register`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + email, + password, + name, + }), + }) + .then(handleRegisterResponse) + .catch((err) => { + setIsLoading(false); + setError('Something went wrong. Please try again later.'); + }); + }; return ( -
{ - e.preventDefault(); - setIsLoading(true); - fetch('http://localhost:8080/v1-register', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - email, - password, - name, - }), - }) - .then(async (res) => { - const json = await res.json(); - if (res.status === 200) { - window.location.href = `/verify?email=${email}`; - setName(''); - setEmail(''); - setPassword(''); - setMessage({ - type: 'success', - message: - 'We have sent you an email with the verification link. Please follow the instructions to login.', - }); - } else { - const error = new Error(json.message) as any; - error.status = res.status; - throw error; - } - setIsLoading(false); - }) - .catch((err) => { - setIsLoading(false); - setMessage({ - type: 'error', - message: err.message, - }); - }); - }} - > + setName(String((e.target as any).value))} /> @@ -78,13 +65,12 @@ const EmailSignupForm: FunctionComponent<{}> = () => { Email address setEmail(String((e.target as any).value))} /> @@ -92,42 +78,28 @@ const EmailSignupForm: FunctionComponent<{}> = () => { Password setPassword(String((e.target as any).value))} /> - {message && ( -
- {message.message} -
+ {error && ( +

{error}.

)}
); diff --git a/src/components/SocialAuth/GitHubButton.tsx b/src/components/SocialAuth/GitHubButton.tsx index 03eefa8d7..b95a958ec 100644 --- a/src/components/SocialAuth/GitHubButton.tsx +++ b/src/components/SocialAuth/GitHubButton.tsx @@ -73,7 +73,7 @@ export function GitHubButton(props: GitHubButtonProps) { return ( <>