Skip to content

Commit

Permalink
Merge pull request #6 from djnunez-aot/tanstack-swap
Browse files Browse the repository at this point in the history
Convert from React router to tanstack router
  • Loading branch information
jadmsaadaot authored Jul 18, 2024
2 parents 639b0a4 + 48a6f16 commit 34f55f7
Show file tree
Hide file tree
Showing 19 changed files with 273 additions and 135 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ share/python-wheels/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
Expand Down
1 change: 0 additions & 1 deletion submit-web/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ dist-ssr
*.njsproj
*.sln
*.sw?

.idea
7 changes: 4 additions & 3 deletions submit-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
"@mui/icons-material": "^5.15.21",
"@mui/material": "^5.15.20",
"@tanstack/react-query": "^5.45.1",
"@tanstack/react-router": "^1.44.4",
"axios": "^1.7.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.24.0"
"react-dom": "^18.2.0"
},
"devDependencies": {
"@tanstack/eslint-plugin-query": "^5.43.1",
"@tanstack/react-query-devtools": "^5.45.1",
"@tanstack/router-devtools": "^1.39.4",
"@tanstack/router-devtools": "^1.44.4",
"@tanstack/router-plugin": "^1.44.3",
"@tanstack/router-vite-plugin": "^1.39.5",
"@types/node": "^20.14.9",
"@types/react": "^18.2.66",
Expand Down
22 changes: 10 additions & 12 deletions submit-web/src/App.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import React from 'react'
import App from './App'
import { BrowserRouter } from 'react-router-dom'
import { routeTree } from "./routeTree.gen.ts";
import { createRouter, RouterProvider } from "@tanstack/react-router";

describe('<App />', () => {
it('renders', () => {
// Create a new router instance
const router = createRouter({ routeTree });

describe("<App />", () => {
it("renders", () => {
// see: https://on.cypress.io/mounting-react
cy.mount(
<BrowserRouter>
<App />
</BrowserRouter>
)
})
})
cy.mount(<RouterProvider router={router} />);
});
});
11 changes: 9 additions & 2 deletions submit-web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import EAOAppBar from "@/components/EAOAppBar";
import SideNavBar from "@/components/SideNavBar";
import { Box } from "@mui/material";
import { Outlet } from "react-router-dom";
import { Outlet } from "@tanstack/react-router";
import { TanStackRouterDevtools } from "@tanstack/router-devtools";

function App() {
return (
<>
<EAOAppBar />
<Box display={"flex"}>
<SideNavBar />
<Box display={"flex"} flexDirection={"column"} flex={1} padding={"1rem"}>
<Box
display={"flex"}
flexDirection={"column"}
flex={1}
padding={"1rem"}
>
<Outlet />
</Box>
</Box>
<TanStackRouterDevtools />
</>
);
}
Expand Down
47 changes: 28 additions & 19 deletions submit-web/src/components/SideNavBar.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Box, List, ListItem, ListItemButton, useTheme } from "@mui/material";
import { useLocation, useNavigate } from "react-router-dom";
import { useState } from "react";
import { Box, List, ListItem, ListItemButton } from "@mui/material";
import { Link } from "@tanstack/react-router";
import { theme } from "@/styles/theme";

export default function SideNavBar() {
const navigate = useNavigate();
const location = useLocation();
const theme = useTheme();
const [currentPath, setCurrentPath] = useState(window.location.pathname);

const routes = [
{
Expand All @@ -31,22 +31,31 @@ export default function SideNavBar() {
<List>
{routes.map((route) => (
<ListItem key={route.routeName}>
<ListItemButton
onClick={() => navigate(route.path)}
sx={{
fontWeight: "700",
backgroundColor:
location.pathname === route.path
? "rgba(0, 0, 0, 0.1)"
: "transparent",
borderLeft:
location.pathname === route.path
? `4px solid ${theme.palette.primary.main}`
: "none",
<Link
to={route.path}
onClick={() => setCurrentPath(route.path)}
style={{
color: theme.palette.primary.main,
fontWeight: currentPath === route.path ? "bold" : "normal",
textDecoration: "none",
width: "100%",
}}
>
{route.routeName}
</ListItemButton>
<ListItemButton
sx={{
backgroundColor:
currentPath === route.path
? "rgba(0, 0, 0, 0.1)"
: "transparent",
borderLeft:
currentPath === route.path
? `4px solid ${theme.palette.primary.main}`
: "none",
}}
>
<span style={{ color: "inherit" }}>{route.routeName}</span>
</ListItemButton>
</Link>
</ListItem>
))}
</List>
Expand Down
18 changes: 14 additions & 4 deletions submit-web/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ import { StrictMode } from "react";
import ReactDOM from "react-dom/client";
import { ThemeProvider } from "@mui/material";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { RouterProvider } from "react-router-dom";
import { RouterProvider, createRouter } from '@tanstack/react-router'
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";

import { routeTree } from './routeTree.gen.ts'
import { theme } from "@/styles/theme";
import "@/styles/App.scss";

import router from "@/routes/router";

// Create a new router instance
const router = createRouter({ routeTree })

const queryClient = new QueryClient();

// Register the router instance for type safety
declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
}


// Render the app
const rootElement = document.getElementById("root")!;
if (!rootElement.innerHTML) {
Expand All @@ -20,7 +30,7 @@ if (!rootElement.innerHTML) {
<StrictMode>
<QueryClientProvider client={queryClient}>
<ThemeProvider theme={theme}>
<RouterProvider router={router} />
<RouterProvider router={router} />
</ThemeProvider>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
Expand Down
11 changes: 8 additions & 3 deletions submit-web/src/pages/Plans/PlanListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { usePlansData } from "@/hooks/usePlans";
import { AxiosResponse } from "axios";
import { Plan } from "@/models/Plan";
import { Link } from "react-router-dom";
import { Link } from "@tanstack/react-router";

export default function PlanListPage() {
const { isLoading, data, isError, error } = usePlansData();
Expand Down Expand Up @@ -39,13 +39,18 @@ export default function PlanListPage() {
</TableRow>
</TableHead>
<TableBody>
{plans.map((row: Plan) => (
{plans?.map((row: Plan) => (
<TableRow
key={row.id}
sx={{ "&:last-child td, &:last-child th": { border: 0 } }}
>
<TableCell component="th" scope="row">
<Link to={`${row.id}`}>{row.name}</Link>
<Link
to={`/planslist/$planId`}
params={{ planId: `${row.id}` }}
>
{row.name}
</Link>
</TableCell>
<TableCell align="right">{row.submittedDate}</TableCell>
<TableCell align="right">{row.submittedBy}</TableCell>
Expand Down
20 changes: 7 additions & 13 deletions submit-web/src/pages/Plans/PlanPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import { usePlanById } from "@/hooks/usePlans";
import { Plan } from "@/models/Plan";
import { Box, Button, Chip } from "@mui/material";
import { AxiosResponse } from "axios";
import { useNavigate, useParams } from "react-router-dom";
import { Link, useParams } from "@tanstack/react-router";

export default function PlanPage() {
const navigate = useNavigate();
const { planIdParam } = useParams();

const { planId: planIdParam } = useParams({ strict: false });
const planId = Number(planIdParam);
const { status, data, isError, error, isFetching, isLoading } =
usePlanById(planId);

const plan: Plan = (data as AxiosResponse)?.data;

console.log(plan);

if (isLoading) {
return <h2>Loading...</h2>;
}
Expand Down Expand Up @@ -43,13 +39,11 @@ export default function PlanPage() {
<p>On {plan.submittedDate}</p>
</div>
<div>{isFetching ? "Background Updating..." : " "}</div>
<Button
variant="outlined"
color="primary"
onClick={() => navigate("/planslist")}
>
Go Back
</Button>
<Link to={"/planslist"}>
<Button variant="outlined" color="primary">
Go Back
</Button>
</Link>
</>
)}
</div>
Expand Down
7 changes: 0 additions & 7 deletions submit-web/src/pages/Plans/Plans.tsx

This file was deleted.

Loading

0 comments on commit 34f55f7

Please sign in to comment.