Now loading user in a wrapper for the app, added loading screen

This commit is contained in:
Carter 2024-10-29 11:13:51 -06:00
parent 864b7b0689
commit eafb88ebfc
8 changed files with 734 additions and 66 deletions

2
.env
View file

@ -3,4 +3,4 @@ VITE_APP_API_URL=https://bxt-dev.api.adaptiveagriculture.ca/v1
#Auth0 #Auth0
VITE_AUTH0_CLIENT_DOMAIN=brandxtech.auth0.com VITE_AUTH0_CLIENT_DOMAIN=brandxtech.auth0.com
VITE_AUTH0_AUDIENCE=bxt-dev.api.adaptiveagriculture.ca VITE_AUTH0_AUDIENCE=bxt-dev.api.adaptiveagriculture.ca
VITE_AUTH0_BXT_CLIENT_ID=dzJTpqIeMA4Rwk4xujtwPbAO3TY32bM1 VITE_AUTH0_DEV_CLIENT_ID=dzJTpqIeMA4Rwk4xujtwPbAO3TY32bM1

717
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -18,6 +18,9 @@
}, },
"dependencies": { "dependencies": {
"@auth0/auth0-react": "^2.2.4", "@auth0/auth0-react": "^2.2.4",
"@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0",
"@mui/material": "^6.1.5",
"axios": "^1.7.7", "axios": "^1.7.7",
"moment": "^2.30.1", "moment": "^2.30.1",
"react": "^18.3.1", "react": "^18.3.1",

View file

@ -2,23 +2,17 @@ import { Auth0Provider } from '@auth0/auth0-react'
import { or } from '../utils/types' import { or } from '../utils/types'
import AuthWrapper from '../providers/auth' import AuthWrapper from '../providers/auth'
import HTTPProvider from '../providers/http' import HTTPProvider from '../providers/http'
import App from './App'
import { useState } from 'react' import { useState } from 'react'
import LoadingScreen from './LoadingScreen'
import UserWrapper from './UserWrapper'
function AuthHTTPWrapper() { function AuthHTTPWrapper() {
const [token, setToken] = useState<string | undefined>(undefined) const [token, setToken] = useState<string | undefined>(undefined)
let url: string | undefined = window.location.origin; let url: string | undefined = import.meta.env.VITE_AUTH0_CLIENT_DOMAIN;
let audience: string | undefined; let audience: string | undefined = import.meta.env.VITE_AUTH0_AUDIENCE;
if (window.location.origin === "https://staging.brandxtech.ca") {
url = import.meta.env.VITE_AUTH0_CLIENT_STAGING_DOMAIN;
audience = import.meta.env.VITE_AUTH0_STAGING_AUDIENCE;
} else {
url = import.meta.env.VITE_AUTH0_CLIENT_DOMAIN;
audience = import.meta.env.VITE_AUTH0_AUDIENCE;
}
let client_id = import.meta.env.VITE_AUTH0_BXT_CLIENT_ID; let client_id = import.meta.env.VITE_AUTH0_DEV_CLIENT_ID;
return ( return (
<Auth0Provider <Auth0Provider
@ -34,10 +28,12 @@ function AuthHTTPWrapper() {
<AuthWrapper setToken={setToken}> <AuthWrapper setToken={setToken}>
{ token ? { token ?
<HTTPProvider token={token}> <HTTPProvider token={token}>
<App/> <UserWrapper/>
</HTTPProvider> </HTTPProvider>
: :
<p>no token</p> <LoadingScreen
message='Loading user profile'
/>
} }
</AuthWrapper> </AuthWrapper>
</Auth0Provider> </Auth0Provider>

23
src/app/LoadingScreen.tsx Normal file
View file

@ -0,0 +1,23 @@
import { CircularProgress, Typography } from "@mui/material";
interface Props {
message?: string;
}
export default function LoadingScreen(props: Props) {
const { message } = props;
return (
<>
<CircularProgress
size={125}
style={{ marginBottom: 24 }}
/>
<br/>
<Typography variant="h6">
{message ? message : "Loading..."}
</Typography>
</>
);
}

View file

@ -5,13 +5,15 @@ import './App.css'
import { useUserAPI } from '../providers/pond/userAPI' import { useUserAPI } from '../providers/pond/userAPI'
import { useAuth0 } from '@auth0/auth0-react' import { useAuth0 } from '@auth0/auth0-react'
import { or } from '../utils/types' import { or } from '../utils/types'
import LoadingScreen from './LoadingScreen'
function App() { export default function UserWrapper() {
const [count, setCount] = useState(0) const [count, setCount] = useState(0)
const [loading, setLoading] = useState(false) const [loading, setLoading] = useState(false)
const userAPI = useUserAPI(); const userAPI = useUserAPI();
const useAuth = useAuth0(); const useAuth = useAuth0();
const hasFetched = useRef(false); const hasFetched = useRef(false);
const [user, setUser] = useState<any | undefined>(undefined)
let user_id = or(useAuth.user?.sub, "") let user_id = or(useAuth.user?.sub, "")
@ -19,9 +21,10 @@ function App() {
// if (loading) return; // if (loading) return;
if (hasFetched.current) return; if (hasFetched.current) return;
setLoading(true) setLoading(true)
console.log("send") // console.log("send")
userAPI.getUser(user_id).then(resp => { userAPI.getUser(user_id).then(resp => {
console.log(resp) // console.log(resp)
setUser(resp)
}).finally(() => { }).finally(() => {
setLoading(false) setLoading(false)
hasFetched.current = true; hasFetched.current = true;
@ -33,6 +36,12 @@ function App() {
loadUser() loadUser()
}, [loading]) }, [loading])
if (!user || loading) return (
<LoadingScreen
message={"Loading user profile..."}
/>
)
return ( return (
<><div> <><div>
<a href="https://vitejs.dev" target="_blank"> <a href="https://vitejs.dev" target="_blank">
@ -55,6 +64,4 @@ function App() {
</p> </p>
</> </>
) )
} }
export default App

View file

@ -19,21 +19,13 @@ export default function AuthWrapper(props: Props) {
if (isAuthenticated) getAccessTokenSilently().then(t => { if (isAuthenticated) getAccessTokenSilently().then(t => {
if (t.length < 1) { if (t.length < 1) {
loginWithRedirect() loginWithRedirect()
} else {
} }
setToken(t) setToken(t)
}).catch(() => { }).catch(() => {
// No token, go back to login // No token, go back to login
loginWithRedirect() loginWithRedirect()
}) })
}, [setToken, isAuthenticated]) }, [setToken, isAuthenticated])
if (isAuthenticated) return ( return children;
children
)
return (
<p>Redirecting...</p>
)
} }

View file

@ -3,7 +3,7 @@ import UserProvider, { useUserAPI } from "./userAPI";
export const pondURL = (partial: string, demo: boolean = false): string => { export const pondURL = (partial: string, demo: boolean = false): string => {
let url = import.meta.env.VITE_APP_API_URL + (demo ? "/demo" : "") + partial; let url = import.meta.env.VITE_APP_API_URL + (demo ? "/demo" : "") + partial;
console.log(url) // console.log(url)
// if (isStaging()) url = process.env.REACT_APP_STAGING_API_URL + (demo ? "/demo" : "") + partial; // if (isStaging()) url = process.env.REACT_APP_STAGING_API_URL + (demo ? "/demo" : "") + partial;
return url; return url;
}; };