2020-10-30 22:18:17 +03:00
|
|
|
import React, { useState } from 'react'
|
|
|
|
import { useHistory } from 'react-router-dom'
|
|
|
|
|
2020-11-17 13:49:10 +03:00
|
|
|
import login from '@wasp/auth/login.js'
|
2020-10-30 22:18:17 +03:00
|
|
|
import signup from '@wasp/actions/signup'
|
|
|
|
|
|
|
|
export default () => {
|
|
|
|
const history = useHistory()
|
|
|
|
const [username, setUsername] = useState()
|
|
|
|
const [email, setEmail] = useState()
|
|
|
|
const [password, setPassword] = useState()
|
2020-11-17 13:49:10 +03:00
|
|
|
const [submitError, setSubmitError] = useState()
|
|
|
|
|
|
|
|
// TODO: Do validation in form and show validation errors.
|
2020-10-30 22:18:17 +03:00
|
|
|
|
|
|
|
const handleSubmit = async (event) => {
|
|
|
|
event.preventDefault()
|
2020-11-17 13:49:10 +03:00
|
|
|
setSubmitError(null)
|
2020-10-30 22:18:17 +03:00
|
|
|
try {
|
|
|
|
await signup({ username, email, password })
|
2020-11-17 13:49:10 +03:00
|
|
|
await login(email, password)
|
2020-10-30 22:18:17 +03:00
|
|
|
history.push('/')
|
|
|
|
} catch (err) {
|
2020-11-17 13:49:10 +03:00
|
|
|
setSubmitError(err)
|
2020-10-30 22:18:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: I should look into using bootstrap v4, it might make all this simpler.
|
|
|
|
return (
|
|
|
|
<div>
|
2020-11-17 13:49:10 +03:00
|
|
|
{ submitError && (
|
|
|
|
<p> { submitError.message || submitError } </p>
|
|
|
|
) }
|
|
|
|
|
2020-10-30 22:18:17 +03:00
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<h2> Username </h2>
|
|
|
|
<input type='text' value={username} onChange={e => setUsername(e.target.value)} />
|
|
|
|
|
|
|
|
<h2> Email </h2>
|
|
|
|
<input type='text' value={email} onChange={e => setEmail(e.target.value)} />
|
|
|
|
|
|
|
|
<h2> Password </h2>
|
|
|
|
<input type='password' value={password} onChange={e => setPassword(e.target.value)} />
|
|
|
|
|
|
|
|
<br />
|
|
|
|
<input type='submit' value='Sign up' />
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|