From 3324a15cee3135df7c526da1bc14e810323882ba Mon Sep 17 00:00:00 2001 From: "Federico Pasqua (eisterman)" Date: Sun, 30 Mar 2025 21:13:08 +0200 Subject: [PATCH] Complete to "Client Side Routing" --- src/routeTree.gen.ts | 31 +++++++-- src/routes/__root.tsx | 9 ++- src/routes/contacts.$contractId.tsx | 101 ++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 7 deletions(-) create mode 100644 src/routes/contacts.$contractId.tsx diff --git a/src/routeTree.gen.ts b/src/routeTree.gen.ts index 5e68293..7642cb7 100644 --- a/src/routeTree.gen.ts +++ b/src/routeTree.gen.ts @@ -12,6 +12,7 @@ import { Route as rootRoute } from './routes/__root' import { Route as IndexImport } from './routes/index' +import { Route as ContactsContractIdImport } from './routes/contacts.$contractId' // Create/Update Routes @@ -21,6 +22,12 @@ const IndexRoute = IndexImport.update({ getParentRoute: () => rootRoute, } as any) +const ContactsContractIdRoute = ContactsContractIdImport.update({ + id: '/contacts/$contractId', + path: '/contacts/$contractId', + getParentRoute: () => rootRoute, +} as any) + // Populate the FileRoutesByPath interface declare module '@tanstack/react-router' { @@ -32,6 +39,13 @@ declare module '@tanstack/react-router' { preLoaderRoute: typeof IndexImport parentRoute: typeof rootRoute } + '/contacts/$contractId': { + id: '/contacts/$contractId' + path: '/contacts/$contractId' + fullPath: '/contacts/$contractId' + preLoaderRoute: typeof ContactsContractIdImport + parentRoute: typeof rootRoute + } } } @@ -39,32 +53,37 @@ declare module '@tanstack/react-router' { export interface FileRoutesByFullPath { '/': typeof IndexRoute + '/contacts/$contractId': typeof ContactsContractIdRoute } export interface FileRoutesByTo { '/': typeof IndexRoute + '/contacts/$contractId': typeof ContactsContractIdRoute } export interface FileRoutesById { __root__: typeof rootRoute '/': typeof IndexRoute + '/contacts/$contractId': typeof ContactsContractIdRoute } export interface FileRouteTypes { fileRoutesByFullPath: FileRoutesByFullPath - fullPaths: '/' + fullPaths: '/' | '/contacts/$contractId' fileRoutesByTo: FileRoutesByTo - to: '/' - id: '__root__' | '/' + to: '/' | '/contacts/$contractId' + id: '__root__' | '/' | '/contacts/$contractId' fileRoutesById: FileRoutesById } export interface RootRouteChildren { IndexRoute: typeof IndexRoute + ContactsContractIdRoute: typeof ContactsContractIdRoute } const rootRouteChildren: RootRouteChildren = { IndexRoute: IndexRoute, + ContactsContractIdRoute: ContactsContractIdRoute, } export const routeTree = rootRoute @@ -77,11 +96,15 @@ export const routeTree = rootRoute "__root__": { "filePath": "__root.tsx", "children": [ - "/" + "/", + "/contacts/$contractId" ] }, "/": { "filePath": "index.tsx" + }, + "/contacts/$contractId": { + "filePath": "contacts.$contractId.tsx" } } } diff --git a/src/routes/__root.tsx b/src/routes/__root.tsx index ee16358..f657013 100644 --- a/src/routes/__root.tsx +++ b/src/routes/__root.tsx @@ -1,4 +1,4 @@ -import { createRootRoute } from '@tanstack/react-router'; +import { Outlet, createRootRoute, Link } from '@tanstack/react-router'; import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'; export const Route = createRootRoute({ @@ -24,14 +24,17 @@ export const Route = createRootRoute({ +
+ +
), diff --git a/src/routes/contacts.$contractId.tsx b/src/routes/contacts.$contractId.tsx new file mode 100644 index 0000000..eb7404e --- /dev/null +++ b/src/routes/contacts.$contractId.tsx @@ -0,0 +1,101 @@ +import { createFileRoute } from '@tanstack/react-router'; +import type { ContactRecord } from "@/data.ts"; + +export const Route = createFileRoute('/contacts/$contractId')({ + component: Contact, +}); + +function Contact() { + const contact = { + first: "Your", + last: "Name", + avatar: "https://placecats.com/200/200", + twitter: "your_handle", + notes: "Some notes", + favorite: true, + }; + + return ( +
+
+ {`${contact.first} +
+ +
+

+ {contact.first || contact.last ? ( + <> + {contact.first} {contact.last} + + ) : ( + No Name + )} + +

+ + {contact.twitter ? ( +

+ + {contact.twitter} + +

+ ) : null} + + {contact.notes ?

{contact.notes}

: null} + +
+
+ +
+ +
{ + const response = confirm( + "Please confirm you want to delete this record." + ); + if (!response) { + event.preventDefault(); + } + }} + > + +
+
+
+
+ ); +} + +function Favorite( + { + contact, + }: { + contact: Pick; + } +) { + const favorite = contact.favorite; + + return ( +
+ +
+ ); +} \ No newline at end of file