-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathContent.tsx
More file actions
73 lines (62 loc) · 1.8 KB
/
Content.tsx
File metadata and controls
73 lines (62 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React, { useCallback, useState } from 'react';
import type { DependencyList } from 'react';
import { useAuthClient } from './auth';
export const Content: React.FC = () => {
const authClient = useAuthClient();
const [doLogin, isLoginLoading] = useAsyncCallback(() => authClient.login(), [
authClient,
]);
const [doRefresh, isRefreshLoading] = useAsyncCallback(
() => authClient.refresh(),
[authClient]
);
const [doLogout, isLogoutLoading] = useAsyncCallback(
() => authClient.logout(),
[authClient]
);
return (
<div>
<p>Auth client ready? {String(authClient.isInitialized)}</p>
<p>Auth client authenticated? {String(authClient.isAuthenticated)}</p>
<div>
<button
onClick={doLogin}
disabled={authClient.isAuthenticated || isLoginLoading}
>
Login
</button>
<button
onClick={doRefresh}
disabled={!authClient.tokens.refreshToken || isRefreshLoading}
>
Refresh
</button>
<button
onClick={doLogout}
disabled={!authClient.isAuthenticated || isLogoutLoading}
>
Logout
</button>
</div>
{isLoginLoading ? <p>Login in progress..</p> : null}
{isRefreshLoading ? <p>Refresh in progress..</p> : null}
<p>Tokens:</p>
<pre>{JSON.stringify(authClient.tokens, null, 2)}</pre>
</div>
);
};
function useAsyncCallback<T extends (...args: never[]) => Promise<unknown>>(
callback: T,
deps: DependencyList
): [T, boolean] {
const [isLoading, setLoading] = useState(false);
const cb = useCallback(async (...argsx: never[]) => {
setLoading(true);
try {
return await callback(...argsx);
} finally {
setLoading(false);
}
}, deps) as T;
return [cb, isLoading];
}