-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-browser.js
35 lines (30 loc) · 1.88 KB
/
gatsby-browser.js
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
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //
import React, { useState, useEffect, Fragment } from "react"
import { ApolloClientHOC } from "./src/apollo/client"
import { silentAuth } from "./src/utils/auth"
import { LoadingContainer } from "./src/components/loadingcontainer"
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //
const SessionCheck = ({ children }) => {
const [loading, setLoading] = useState(true)
// When this component mounts, call the silentAuth method and change the loading state to false
useEffect(() => {
silentAuth(() => setLoading(false))
}, [])
// While loading is false, render the app
if (loading === true)
return (
<div style={{ margin: 0 }}>
<LoadingContainer />
</div>
)
return loading === false && <Fragment>{children}</Fragment>
}
export const wrapRootElement = ({ element }) => (
<ApolloClientHOC>
<SessionCheck>{element}</SessionCheck>
</ApolloClientHOC>
)
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //