Skip to content

Commit 6410f22

Browse files
committed
fix: updated website examples
1 parent c80e647 commit 6410f22

File tree

8 files changed

+44
-44
lines changed

8 files changed

+44
-44
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ members = [
44
"examples/core/*",
55
"examples/demos/*",
66
"examples/comprehensive/*",
7-
# "examples/website/*",
7+
"examples/website/*",
88
"website",
99
]
1010
resolver = "2"

examples/website/app_in_a_file/README.md

-3
This file was deleted.

examples/website/app_in_a_file/src/main.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,42 @@ pub fn main<G: Html>() -> PerseusApp<G> {
1010
// Create a new template at `index`, which maps to our landing page
1111
.template(
1212
Template::build("index")
13-
.template_with_state(index_page)
14-
.build_state_fn(get_index_build_state),
13+
.view_with_state(index_page)
14+
.build_state_fn(get_build_state)
15+
.build(),
1516
)
16-
.template(Template::new("about").template(about_page))
17+
.template(Template::build("about").view(about_page).build())
1718
}
1819

20+
#[auto_scope]
1921
// EXCERPT_START
20-
#[perseus::template]
21-
fn index_page<'a, G: Html>(cx: Scope<'a>, props: IndexPropsRx<'a>) -> View<G> {
22+
fn index_page<G: Html>(cx: Scope, state: &IndexStateRx) -> View<G> {
2223
view! { cx,
2324
h1 { (format!(
2425
"Hello, {}!",
25-
props.name.get()
26+
state.name.get()
2627
)) }
2728
input(
2829
placeholder = "Name",
29-
bind:value = props.name
30+
bind:value = state.name
3031
)
3132
a(href = "about") { "About" }
3233
}
3334
}
3435

35-
#[derive(Serialize, Deserialize, ReactiveState)]
36-
#[rx(alias = "IndexPropsRx")]
37-
struct IndexProps {
36+
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
37+
#[rx(alias = "IndexStateRx")]
38+
struct IndexState {
3839
name: String,
3940
}
4041

4142
// This function will be run when you build your app, to generate default state
4243
// ahead-of-time
4344
#[engine_only_fn]
44-
async fn get_index_build_state(
45-
_info: StateGeneratorInfo<()>,
46-
) -> RenderFnResultWithCause<IndexProps> {
47-
let props = IndexProps {
45+
async fn get_build_state(_info: StateGeneratorInfo<()>) -> IndexState {
46+
IndexState {
4847
name: "User".to_string(),
49-
};
50-
Ok(props)
48+
}
5149
}
5250
// EXCERPT_END
5351

examples/website/i18n/README.md

-3
This file was deleted.

examples/website/i18n/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use perseus::{t, Html, PerseusApp, Template};
1+
use perseus::prelude::*;
22
use sycamore::prelude::*;
33

44
#[perseus::main(perseus_warp::dflt_server)]
55
pub fn main<G: Html>() -> PerseusApp<G> {
66
PerseusApp::new()
7-
.template(Template::new("index").template(index_page))
7+
.template(Template::build("index").view(index_page).build())
88
// EXCERPT_START
99
.locales_and_translations_manager(
1010
"en-US", // Default locale

examples/website/state_generation/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ perseus = { path = "../../../packages/perseus" }
1010
sycamore = "^0.8.1"
1111
serde = { version = "1", features = [ "derive" ] }
1212
serde_json = "1"
13+
thiserror = "1"
1314

1415
[target.'cfg(engine)'.dependencies]
1516
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }

examples/website/state_generation/README.md

-3
This file was deleted.

examples/website/state_generation/src/main.rs

+26-16
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
use perseus::prelude::*;
22
use serde::{Deserialize, Serialize};
3+
use std::io;
34
use std::time::Duration;
45
use sycamore::prelude::*;
56

67
#[perseus::main(perseus_warp::dflt_server)]
78
pub fn main<G: Html>() -> PerseusApp<G> {
89
PerseusApp::new().template(
910
Template::build("post")
10-
.template_with_state(post_page)
11+
.view_with_state(post_page)
1112
.build_paths_fn(get_build_paths)
1213
.build_state_fn(get_build_state)
1314
// Reload every blog post every day, in case it's changed
1415
.revalidate_after(Duration::new(60 * 60 * 24, 0))
1516
// If the user requests a page we haven't created yet, still
1617
// pass it to `get_build_state()` and cache the output for
1718
// future users (lazy page building)
18-
.incremental_generation(),
19+
.incremental_generation()
20+
.build(),
1921
)
2022
}
2123

24+
#[auto_scope]
2225
// EXCERPT_START
23-
#[perseus::template]
24-
fn post_page<'a, G: Html>(cx: Scope<'a>, props: PostRx<'a>) -> View<G> {
26+
fn post_page<G: Html>(cx: Scope, state: &PostRx) -> View<G> {
2527
view! { cx,
26-
h1 { (props.title.get()) }
27-
p { (props.author.get()) }
28+
h1 { (state.title.get()) }
29+
p { (state.author.get()) }
2830
div(
29-
dangerously_set_inner_html = &props.content.get()
31+
dangerously_set_inner_html = &state.content.get()
3032
)
3133
}
3234
}
3335
// EXCERPT_END
3436

35-
#[derive(Serialize, Deserialize, ReactiveState)]
37+
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
3638
#[rx(alias = "PostRx")]
3739
struct Post {
3840
title: String,
@@ -45,24 +47,29 @@ struct Post {
4547
#[engine_only_fn]
4648
async fn get_build_state(
4749
StateGeneratorInfo { path, .. }: StateGeneratorInfo<()>,
48-
) -> RenderFnResultWithCause<Post> {
50+
) -> Result<Post, BlamedError<MyError>> {
4951
let raw_post = match get_post_for_path(path) {
5052
Ok(post) => post,
5153
// If the user sends us some bogus path with incremental generation,
5254
// return a 404 appropriately
53-
Err(err) => blame_err!(client, 404, err),
55+
Err(err) => {
56+
return Err(BlamedError {
57+
blame: ErrorBlame::Client(Some(404)),
58+
error: MyError,
59+
})
60+
}
5461
};
5562
let html_content = parse_markdown(raw_post.content);
56-
let props = Post {
63+
let post = Post {
5764
title: raw_post.title,
5865
author: raw_post.author,
5966
content: html_content,
6067
};
61-
Ok(props)
68+
Ok(post)
6269
}
6370
#[engine_only_fn]
64-
async fn get_build_paths() -> RenderFnResult<BuildPaths> {
65-
Ok(BuildPaths {
71+
async fn get_build_paths() -> BuildPaths {
72+
BuildPaths {
6673
// These will all become URLs at `/post/<name>`
6774
paths: vec![
6875
"welcome".to_string(),
@@ -71,12 +78,15 @@ async fn get_build_paths() -> RenderFnResult<BuildPaths> {
7178
],
7279
// Perseus supports helper state, but we don't need it here
7380
extra: ().into(),
74-
})
81+
}
7582
}
7683
// EXCERPT_END
7784

7885
// SNIP
79-
fn get_post_for_path(_path: String) -> Result<Post, std::io::Error> {
86+
#[derive(thiserror::Error, Debug)]
87+
#[error("an error!")]
88+
struct MyError;
89+
fn get_post_for_path(_path: String) -> Result<Post, io::Error> {
8090
unimplemented!()
8191
}
8292
fn parse_markdown(_content: String) -> String {

0 commit comments

Comments
 (0)