Add loading state for login/register pages

This commit is contained in:
Robert Long 2021-11-17 15:12:54 -08:00
parent 56ba87f25d
commit 442086f31b
2 changed files with 112 additions and 93 deletions

View file

@ -28,11 +28,14 @@ export function LoginPage({ onLogin }) {
const passwordRef = useRef(); const passwordRef = useRef();
const history = useHistory(); const history = useHistory();
const location = useLocation(); const location = useLocation();
const [loading, setLoading] = useState(false);
const [error, setError] = useState(); const [error, setError] = useState();
const onSubmitLoginForm = useCallback( const onSubmitLoginForm = useCallback(
(e) => { (e) => {
e.preventDefault(); e.preventDefault();
setLoading(true);
onLogin(homeserver, usernameRef.current.value, passwordRef.current.value) onLogin(homeserver, usernameRef.current.value, passwordRef.current.value)
.then(() => { .then(() => {
if (location.state && location.state.from) { if (location.state && location.state.from) {
@ -41,7 +44,10 @@ export function LoginPage({ onLogin }) {
history.replace("/"); history.replace("/");
} }
}) })
.catch(setError); .catch((error) => {
setError(error);
setLoading(false);
});
}, },
[onLogin, location, history, homeserver] [onLogin, location, history, homeserver]
); );
@ -53,6 +59,9 @@ export function LoginPage({ onLogin }) {
</Header> </Header>
<Content> <Content>
<Center> <Center>
{loading ? (
<div>Loading...</div>
) : (
<Modal> <Modal>
<h2>Login</h2> <h2>Login</h2>
<form onSubmit={onSubmitLoginForm}> <form onSubmit={onSubmitLoginForm}>
@ -106,6 +115,7 @@ export function LoginPage({ onLogin }) {
</Link> </Link>
</Info> </Info>
</Modal> </Modal>
)}
</Center> </Center>
</Content> </Content>
</> </>

View file

@ -25,11 +25,13 @@ export function RegisterPage({ onRegister }) {
const registerPasswordRef = useRef(); const registerPasswordRef = useRef();
const history = useHistory(); const history = useHistory();
const location = useLocation(); const location = useLocation();
const [loading, setLoading] = useState(false);
const [error, setError] = useState(); const [error, setError] = useState();
const onSubmitRegisterForm = useCallback( const onSubmitRegisterForm = useCallback(
(e) => { (e) => {
e.preventDefault(); e.preventDefault();
setLoading(true);
onRegister( onRegister(
registerUsernameRef.current.value, registerUsernameRef.current.value,
registerPasswordRef.current.value registerPasswordRef.current.value
@ -41,7 +43,10 @@ export function RegisterPage({ onRegister }) {
history.replace("/"); history.replace("/");
} }
}) })
.catch(setError); .catch((error) => {
setError(error);
setLoading(false);
});
}, },
[onRegister, location, history] [onRegister, location, history]
); );
@ -53,6 +58,9 @@ export function RegisterPage({ onRegister }) {
</Header> </Header>
<Content> <Content>
<Center> <Center>
{loading ? (
<div>Loading...</div>
) : (
<Modal> <Modal>
<h2>Register</h2> <h2>Register</h2>
<form onSubmit={onSubmitRegisterForm}> <form onSubmit={onSubmitRegisterForm}>
@ -95,6 +103,7 @@ export function RegisterPage({ onRegister }) {
</Link> </Link>
</Info> </Info>
</Modal> </Modal>
)}
</Center> </Center>
</Content> </Content>
</> </>