-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from shanedbutler/upgrade/browser-router-to-ro…
…uter-provider Upgrade/react router to data router provider
- Loading branch information
Showing
16 changed files
with
617 additions
and
909 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,106 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { BrowserRouter, Route, Routes } from 'react-router-dom'; | ||
import { createBrowserRouter, RouterProvider } from 'react-router-dom'; | ||
import { supabase } from './supabaseUtils/supabaseClient'; | ||
import { Login } from './auth/Login'; | ||
import { Register } from './auth/Register'; | ||
import { ApplicationViews } from './views/ApplicationViews'; | ||
import { GameDetails } from './game/GameDetails'; | ||
import ProtectedRoute from './views/ProtectedRoute'; | ||
import { Dashboard } from './dashboard/Dashboard'; | ||
import { PlayerProfile } from './profile/PlayerProfile'; | ||
import { PlayerEdit } from './profile/PlayerEdit'; | ||
import { GameEdit } from './game/GameEdit'; | ||
import { GameForm } from './game/GameForm'; | ||
import { PlayerManagement } from './profile/PlayerManagement'; | ||
import { sessionLoader } from './managers/sessionLoader'; | ||
import { gameByIdEditLoader, gameByIdLoader } from './managers/gameLoader'; | ||
import { userProfileByIdLoader, userProfilesLoader } from './managers/userProfileLoader'; | ||
import Unauthorized from './auth/Unauthorized'; | ||
|
||
export const App = () => { | ||
const [session, setSession] = useState(null) | ||
const [loading, setLoading] = useState(true); | ||
const [session, setSession] = useState(null); | ||
|
||
useEffect(() => { | ||
// Listen for changes to the user authentication state | ||
const { data: authListener } = supabase.auth.onAuthStateChange((_event, session) => { | ||
setSession(session ?? null); | ||
setLoading(false); | ||
}); | ||
useEffect(() => { | ||
supabase.auth.getSession().then(({ data: { session } }) => { | ||
setSession(session); | ||
}); | ||
|
||
// Unsubscribe from the listener when the component unmounts | ||
return () => { | ||
if (authListener && typeof authListener.unsubscribe === 'function') { | ||
authListener.unsubscribe(); | ||
} | ||
}; | ||
}, [supabase.auth]); | ||
const { | ||
data: { subscription }, | ||
} = supabase.auth.onAuthStateChange((_event, session) => { | ||
setSession(session); | ||
}); | ||
|
||
return ( | ||
<BrowserRouter> | ||
{!loading && | ||
<Routes> | ||
{session ? | ||
<Route | ||
path="*" | ||
element={ | ||
<div className="content-wrapper selection:bg-lime-100"> | ||
<ApplicationViews user={session?.user} /> | ||
</div> | ||
} | ||
/> | ||
: | ||
<> | ||
<Route path="*" element={<Login />} /> | ||
<Route path="/register" element={<Register />} /> | ||
</> | ||
} | ||
</Routes> | ||
} | ||
</BrowserRouter> | ||
); | ||
}; | ||
return () => subscription.unsubscribe(); | ||
}, []); | ||
|
||
const router = createBrowserRouter([ | ||
{ | ||
path: '/', | ||
element: <Login />, | ||
}, | ||
{ | ||
path: '/register', | ||
element: <Register />, | ||
}, | ||
{ | ||
path: '/', | ||
element: <ProtectedRoute />, | ||
id: 'protected', | ||
loader: sessionLoader, | ||
children: [ | ||
{ | ||
path: 'dashboard', | ||
element: <Dashboard />, | ||
}, | ||
{ | ||
path: 'game/:id', | ||
element: <GameDetails />, | ||
loader: gameByIdLoader, | ||
}, | ||
{ | ||
path: 'profile/:id', | ||
element: <PlayerProfile />, | ||
loader: userProfileByIdLoader, | ||
}, | ||
{ | ||
path: 'profile/edit', | ||
element: <PlayerEdit />, | ||
}, | ||
{ | ||
path: 'players', | ||
element: <PlayerManagement />, | ||
loader: userProfilesLoader, | ||
}, | ||
{ | ||
path: '/', | ||
element: <ProtectedRoute adminOnly />, | ||
id: 'protected-admin', | ||
children: [ | ||
{ | ||
path: 'game/:id/edit', | ||
element: <GameEdit />, | ||
loader: gameByIdEditLoader, | ||
}, | ||
{ | ||
path: 'new-game', | ||
element: <GameForm />, | ||
}, | ||
{ | ||
path: 'players', | ||
element: <PlayerManagement />, | ||
loader: userProfilesLoader, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
path: '/unauthorized', | ||
element: <Unauthorized />, | ||
}, | ||
]); | ||
|
||
return ( | ||
<RouterProvider router={router} /> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
const Unauthorized = () => { | ||
const navigate = useNavigate(); | ||
|
||
const handleGoBack = () => { | ||
navigate(-1); | ||
}; | ||
|
||
return ( | ||
<div className="text-center mt-12"> | ||
<h1 className="text-3xl font-bold">403 - Unauthorized</h1> | ||
<p className="mt-4">You do not have permission to view this page.</p> | ||
<button className="mt-6 px-4 py-2 bg-blue-500 text-white rounded" onClick={handleGoBack}>Go Back</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Unauthorized; |
Oops, something went wrong.