Skip to content

Commit

Permalink
Merge pull request #82 from shanedbutler/upgrade/browser-router-to-ro…
Browse files Browse the repository at this point in the history
…uter-provider

Upgrade/react router to data router provider
  • Loading branch information
shanedbutler authored Oct 31, 2024
2 parents 109d010 + 6f9d095 commit b8d1adf
Show file tree
Hide file tree
Showing 16 changed files with 617 additions and 909 deletions.
47 changes: 25 additions & 22 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"dependencies": {
"@headlessui/react": "^1.7.4",
"@heroicons/react": "^2.1.5",
"boring-avatars": "^1.10.2",
"boring-avatars": "^1.11.1",
"firebase": "^9.23.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.25.1",
"react-router-dom": "^6.26.2",
"react-select": "^5.8.0"
},
"devDependencies": {
Expand Down
138 changes: 97 additions & 41 deletions client/src/App.jsx
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} />
);
};
2 changes: 1 addition & 1 deletion client/src/auth/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const Login = () => {
alert("Login Failed");
} else {
console.log("Login successful");
navigate("/");
navigate("/dashboard");
}
};

Expand Down
28 changes: 0 additions & 28 deletions client/src/auth/Register.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,6 @@ export const Register = () => {
}
};

// const firebaseHandleSubmit = (e) => {
// e.preventDefault();
// let newUser = {};

// if (positionSelection === '') {
// //handlePrimaryRequired();
// } else if (secondaryPositionSelection === '') {
// //handleSecondaryRequired();
// } else {
// newUser = {
// firstName: firstNameRef.current.value,
// lastName: lastNameRef.current.value,
// email: emailRef.current.value,
// phone: phoneRef.current.value.replace(/\D/g, ''),
// primaryPositionId: parseInt(positionSelection),
// secondaryPositionId: parseInt(secondaryPositionSelection),
// pronounId: parseInt(pronounSelection),
// club: clubRef.current.value,
// emergencyName: emergencyNameRef.current.value,
// emergencyPhone: emergencyPhoneRef.current.value.replace(/\D/g, ''),
// active: true,
// };
// firebaseRegister(newUser, passwordRef.current.value)
// .then(() => navigate("/"))
// .catch(() => alert("Registration Failed"));
// }
// };

const handleCancel = (e) => {
e.preventDefault();
navigate('/login');
Expand Down
19 changes: 19 additions & 0 deletions client/src/auth/Unauthorized.jsx
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;
Loading

0 comments on commit b8d1adf

Please sign in to comment.