feat: add TuonoProps type

This commit is contained in:
Valerio Ageno
2024-05-29 21:54:21 +02:00
parent 1c0b4bba04
commit d7d0001299
6 changed files with 30 additions and 8 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ export default function RootRoute({ children }: RootRouteProps): JSX.Element {
return (
<>
<nav>
Navbar: <Link>About</Link>
Navbar: <Link href="/">Home</Link> | <Link href="/about">About</Link>
</nav>
<main>{children}</main>
</>
+2 -4
View File
@@ -3,14 +3,12 @@ use tuono_lib::{Request, Response};
#[derive(Serialize)]
struct MyResponse<'a> {
title: &'a str,
subtitle: &'a str,
description: &'a str,
}
#[tuono_lib::handler]
fn get_server_side_props(req: &Request) -> Response {
Response::Props(Box::new(MyResponse {
title: "title",
subtitle: "subtitle",
description: "This descriptions comes from the rust server",
}))
}
+19 -2
View File
@@ -1,3 +1,20 @@
export default function IndexPage(): JSX.Element {
return <h1>Index Page</h1>
import type { TuonoProps } from 'tuono'
type IndexProps = {
description: string
}
export default function IndexPage({
data,
isLoading,
}: TuonoProps<IndexProps>): JSX.Element {
if (isLoading) {
return <h1>Loading...</h1>
}
return (
<>
<h1>Index Page</h1>
<p>{data?.description}</p>
</>
)
}
+2
View File
@@ -6,4 +6,6 @@ import {
RouterProvider,
} from './router'
export type { TuonoProps } from './types'
export { createRoute, createRootRoute, createRouter, Link, RouterProvider }
@@ -45,7 +45,8 @@ export function useServerSideProps(
;(async (): Promise<void> => {
setIsLoading(true)
try {
const res = await fetch(`/__tuono/data/${route.path}`)
const path = route.path === '/' ? '' : route.path
const res = await fetch(`/__tuono/data/${path}`)
setData(await res.json())
} catch (error) {
// Handle here error
+4
View File
@@ -0,0 +1,4 @@
export interface TuonoProps<T> {
data?: T
isLoading: boolean
}