Skip to content

Commit

Permalink
Add loading state to google search console
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyec committed Sep 18, 2024
1 parent 2c7e5de commit 7d7927d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
5 changes: 3 additions & 2 deletions plugins/google-search-console/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function AppLoadSite({ login, logout }: AppLoadSiteProps) {
}

export function App() {
const { login, logout, tokens, isReady } = useGoogleToken();
const { login, logout, tokens, isReady, loading } = useGoogleToken();

const ref = useRef<HTMLDivElement>(null)

Expand All @@ -125,6 +125,7 @@ export function App() {

return (
<GoogleLogin
loading={loading}
hasError
errorMessage={
e.error.name !== 'GoogleError' ? e.error.message || '' : ''
Expand All @@ -150,7 +151,7 @@ export function App() {
logout={logout}
/>
) : (
<GoogleLogin login={login} />
<GoogleLogin login={login} loading={loading} />
)
) : null}
</AuthContext.Provider>
Expand Down
45 changes: 26 additions & 19 deletions plugins/google-search-console/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function getLocalStorageTokens() {
}

export function useGoogleToken() {
const [loading, setLoading] = useState(false);
const pollInterval = useRef<number>();

const pollForTokens = (readKey: string) => {
Expand Down Expand Up @@ -58,29 +59,35 @@ export function useGoogleToken() {
}, []);

const login = async () => {
// Retrieve the authorization URL & set of unique read/write keys
const response = await fetch(
`${import.meta.env.VITE_OAUTH_API_DOMAIN}/authorize`,
{
method: 'POST',
},
);
if (response.status !== 200) return;
try {
setLoading(true)

const authorize = await response.json();
// Retrieve the authorization URL & set of unique read/write keys
const response = await fetch(
`${import.meta.env.VITE_OAUTH_API_DOMAIN}/authorize`,
{
method: 'POST',
},
);
if (response.status !== 200) return;

// Open up the provider's login window.
window.open(authorize.url);
const authorize = await response.json();

// While the user is logging in, poll the backend with the
// read key. On successful login, tokens will be returned.
const tokens = await pollForTokens(authorize.readKey);
// Open up the provider's login window.
window.open(authorize.url);

// Store tokens in local storage to keep the user logged in.
window.localStorage.setItem('tokens', JSON.stringify(tokens));
// While the user is logging in, poll the backend with the
// read key. On successful login, tokens will be returned.
const tokens = await pollForTokens(authorize.readKey);

// Update the component state.
setTokens(tokens as GoogleToken);
// Store tokens in local storage to keep the user logged in.
window.localStorage.setItem('tokens', JSON.stringify(tokens));

// Update the component state.
setTokens(tokens as GoogleToken);
} finally {
setLoading(false)
}
};

const refresh = useCallback(async (): Promise<GoogleToken | null> => {
Expand Down Expand Up @@ -129,5 +136,5 @@ export function useGoogleToken() {
setTokens(null);
};

return { isReady, login, refresh, tokens, logout };
return { isReady, login, refresh, tokens, logout, loading };
}
4 changes: 3 additions & 1 deletion plugins/google-search-console/src/screens/GoogleLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import bigG from '../images/[email protected]';

interface GoogleLoginProps {
login: () => void;
loading: boolean;
hasError?: boolean;
errorMessage?: string;
}

export default function GoogleLogin({
login,
loading,
hasError,
errorMessage,
}: GoogleLoginProps) {
Expand All @@ -26,7 +28,7 @@ export default function GoogleLogin({
</div>
</div>
<button type="button" onClick={login}>
Log In
{loading ? "Loading..." : "Log In"}
</button>
</div>
);
Expand Down

0 comments on commit 7d7927d

Please sign in to comment.