Skip to content

Commit 5b9052d

Browse files
Added logic to load projects data on visiting routes. User guard not working yet
1 parent 15594f8 commit 5b9052d

File tree

7 files changed

+141
-27
lines changed

7 files changed

+141
-27
lines changed

src/renderer/react/routeTree.gen.ts

+42-6
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
// Import Routes
1212

1313
import { Route as rootRoute } from './routes/__root'
14-
import { Route as ProjectsImport } from './routes/projects'
14+
import { Route as HasUserGuardImport } from './routes/_hasUserGuard'
1515
import { Route as IndexImport } from './routes/index'
16+
import { Route as ProjectsIndexImport } from './routes/projects/index'
17+
import { Route as UserSetImport } from './routes/user/set'
18+
import { Route as ProjectsProjectIdIndexImport } from './routes/projects/$projectId/index'
1619

1720
// Create/Update Routes
1821

19-
const ProjectsRoute = ProjectsImport.update({
20-
path: '/projects',
22+
const HasUserGuardRoute = HasUserGuardImport.update({
23+
id: '/_hasUserGuard',
2124
getParentRoute: () => rootRoute,
2225
} as any)
2326

@@ -26,6 +29,21 @@ const IndexRoute = IndexImport.update({
2629
getParentRoute: () => rootRoute,
2730
} as any)
2831

32+
const ProjectsIndexRoute = ProjectsIndexImport.update({
33+
path: '/projects/',
34+
getParentRoute: () => rootRoute,
35+
} as any)
36+
37+
const UserSetRoute = UserSetImport.update({
38+
path: '/user/set',
39+
getParentRoute: () => rootRoute,
40+
} as any)
41+
42+
const ProjectsProjectIdIndexRoute = ProjectsProjectIdIndexImport.update({
43+
path: '/projects/$projectId/',
44+
getParentRoute: () => rootRoute,
45+
} as any)
46+
2947
// Populate the FileRoutesByPath interface
3048

3149
declare module '@tanstack/react-router' {
@@ -34,15 +52,33 @@ declare module '@tanstack/react-router' {
3452
preLoaderRoute: typeof IndexImport
3553
parentRoute: typeof rootRoute
3654
}
37-
'/projects': {
38-
preLoaderRoute: typeof ProjectsImport
55+
'/_hasUserGuard': {
56+
preLoaderRoute: typeof HasUserGuardImport
57+
parentRoute: typeof rootRoute
58+
}
59+
'/user/set': {
60+
preLoaderRoute: typeof UserSetImport
61+
parentRoute: typeof rootRoute
62+
}
63+
'/projects/': {
64+
preLoaderRoute: typeof ProjectsIndexImport
65+
parentRoute: typeof rootRoute
66+
}
67+
'/projects/$projectId/': {
68+
preLoaderRoute: typeof ProjectsProjectIdIndexImport
3969
parentRoute: typeof rootRoute
4070
}
4171
}
4272
}
4373

4474
// Create and export the route tree
4575

46-
export const routeTree = rootRoute.addChildren([IndexRoute, ProjectsRoute])
76+
export const routeTree = rootRoute.addChildren([
77+
IndexRoute,
78+
HasUserGuardRoute,
79+
UserSetRoute,
80+
ProjectsIndexRoute,
81+
ProjectsProjectIdIndexRoute,
82+
])
4783

4884
/* prettier-ignore-end */

src/renderer/react/routes/__root.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export const Route = createRootRouteWithContext<RouterContext>()({
2121
<Link to="/projects" className="[&.active]:font-bold">
2222
Projects
2323
</Link>
24+
<Link to="/user/set" className="[&.active]:font-bold">
25+
User
26+
</Link>
2427
</div>
2528
<hr />
2629
<Outlet />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createFileRoute, redirect } from '@tanstack/react-router';
2+
3+
export const Route = createFileRoute('/_hasUserGuard')({
4+
beforeLoad: async ({ location, context }) => {
5+
const user = await context.core.user.get();
6+
console.log('User:', user);
7+
8+
if (!user) {
9+
throw redirect({
10+
to: '/user/set',
11+
search: {
12+
// Use the current location to power a redirect after login
13+
// (Do not use `router.state.resolvedLocation` as it can
14+
// potentially lag behind the actual current location)
15+
redirect: location.href,
16+
},
17+
});
18+
}
19+
},
20+
});

src/renderer/react/routes/projects.tsx

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createFileRoute } from '@tanstack/react-router';
2+
3+
export const Route = createFileRoute('/projects/$projectId/')({
4+
component: ProjectDashboardPage,
5+
loader: ({ context, params }) =>
6+
context.core.projects.read({ id: params.projectId }),
7+
});
8+
9+
function ProjectDashboardPage() {
10+
const project = Route.useLoaderData();
11+
12+
return <div className="p-2">Dashboard for Project "{project.name}"</div>;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Link, createFileRoute } from '@tanstack/react-router';
2+
3+
export const Route = createFileRoute('/projects/')({
4+
component: ListProjectsPage,
5+
loader: ({ context }) => context.core.projects.list(),
6+
});
7+
8+
function ListProjectsPage() {
9+
const context = Route.useRouteContext();
10+
const projects = Route.useLoaderData();
11+
12+
function createProject() {
13+
context.core.projects.create({ name: 'A test project' });
14+
}
15+
16+
return (
17+
<div className="p-2">
18+
<button onClick={createProject}>Create new Project</button>
19+
Found the following Projects:{' '}
20+
<ul>
21+
{projects.list.map((project) => {
22+
return (
23+
<li key={project.id}>
24+
<Link
25+
to="/projects/$projectId"
26+
params={{ projectId: project.id }}
27+
>
28+
{project.name}
29+
</Link>
30+
</li>
31+
);
32+
})}
33+
</ul>
34+
</div>
35+
);
36+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createFileRoute } from '@tanstack/react-router';
2+
3+
export const Route = createFileRoute('/user/set')({
4+
component: SetUserPage,
5+
});
6+
7+
function SetUserPage() {
8+
const context = Route.useRouteContext();
9+
10+
function setUser() {
11+
context.core.user.set({
12+
userType: 'local',
13+
name: 'John Doe',
14+
15+
locale: {
16+
id: 'en',
17+
name: 'English',
18+
},
19+
});
20+
}
21+
22+
return (
23+
<div className="p-2">
24+
<button onClick={setUser}>Create User</button>
25+
</div>
26+
);
27+
}

0 commit comments

Comments
 (0)