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 ( <>