Skip to content

Commit 8b1b177

Browse files
author
swyx
committed
add loading
1 parent 5b2354f commit 8b1b177

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/App.js

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { Router, Link, navigate } from '@reach/router';
33
import './App.css';
44
import { useNetlifyIdentity } from './useNetlifyIdentity';
5+
import useLoading from './useLoading';
56

67
let IdentityContext = React.createContext();
78

@@ -22,10 +23,11 @@ function Login() {
2223
const { loginUser, signupUser } = React.useContext(IdentityContext);
2324
const formRef = React.useRef();
2425
const [msg, setMsg] = React.useState('');
26+
const [isLoading, load] = useLoading();
2527
const signup = () => {
2628
const email = formRef.current.email.value;
2729
const password = formRef.current.password.value;
28-
signupUser(email, password)
30+
load(signupUser(email, password))
2931
.then(user => {
3032
console.log('Success! Signed up', user);
3133
navigate('/dashboard');
@@ -39,7 +41,7 @@ function Login() {
3941
e.preventDefault();
4042
const email = e.target.email.value;
4143
const password = e.target.password.value;
42-
loginUser(email, password)
44+
load(loginUser(email, password))
4345
.then(user => {
4446
console.log('Success! Logged in', user);
4547
navigate('/dashboard');
@@ -59,11 +61,15 @@ function Login() {
5961
<input type="password" name="password" />
6062
</label>
6163
</div>
62-
<div>
63-
<input type="submit" value="Log in" />
64-
<button onClick={signup}>Sign Up </button>
65-
{msg && <pre>{msg}</pre>}
66-
</div>
64+
{isLoading ? (
65+
<Spinner />
66+
) : (
67+
<div>
68+
<input type="submit" value="Log in" />
69+
<button onClick={signup}>Sign Up </button>
70+
{msg && <pre>{msg}</pre>}
71+
</div>
72+
)}
6773
</form>
6874
);
6975
}
@@ -75,7 +81,7 @@ function Home() {
7581
<p>
7682
this is a <b>Public Page</b>, not behind an authentication wall
7783
</p>
78-
<p style={{ backgroundColor: '#EEE', padding: '1rem' }}>
84+
<div style={{ backgroundColor: '#EEE', padding: '1rem' }}>
7985
<div>
8086
<a
8187
href={`https://app.netlify.com/start/deploy?repository=https://github.com/netlify/create-react-app-lambda/tree/reachRouterAndGoTrueDemo&stack=cms`}
@@ -90,7 +96,7 @@ function Home() {
9096
<a href="https://github.com/netlify/create-react-app-lambda/tree/reachRouterAndGoTrueDemo">
9197
Open Source.
9298
</a>{' '}
93-
</p>
99+
</div>
94100
</div>
95101
);
96102
}
@@ -99,18 +105,6 @@ function About() {
99105
return <div>About</div>;
100106
}
101107

102-
function useLoading() {
103-
const [isLoading, setState] = React.useState(false);
104-
const load = aPromise => {
105-
setState(true);
106-
return aPromise.then((...args) => {
107-
setState(false);
108-
return Promise.resolve(...args);
109-
});
110-
};
111-
return [isLoading, load];
112-
}
113-
114108
function Dashboard() {
115109
const props = React.useContext(IdentityContext);
116110
const { isConfirmedUser, authedFetch } = props;

src/useLoading.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
3+
export default function useLoading() {
4+
const [isLoading, setState] = React.useState(false);
5+
const load = aPromise => {
6+
setState(true);
7+
return aPromise
8+
.then((...args) => {
9+
setState(false);
10+
return Promise.resolve(...args);
11+
})
12+
.catch((...args) => {
13+
setState(false);
14+
return Promise.reject(...args);
15+
});
16+
};
17+
return [isLoading, load];
18+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy