Skip to content

Commit

Permalink
docs: 📝 added example of state amalgamation
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Aug 22, 2021
1 parent 1cb4356 commit cd93fdc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/showcase/app/src/bin/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn main() {
pages::ip::get_page::<SsrNode>(),
pages::time::get_page::<SsrNode>(),
pages::time_root::get_page::<SsrNode>(),
pages::amalgamation::get_page::<SsrNode>(),
],
&config_manager,
);
Expand Down
7 changes: 7 additions & 0 deletions examples/showcase/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ enum AppRoute {
TimeRoot,
#[to("/timeisr/<slug>")]
Time { slug: String },
#[to("/amalgamation")]
Amalgamation,
#[not_found]
NotFound,
}
Expand Down Expand Up @@ -106,6 +108,11 @@ pub fn run() -> Result<(), JsValue> {
pages::time_root::template_fn(),
get_error_pages()
),
AppRoute::Amalgamation => app_shell(
"amalgamation".to_string(),
pages::amalgamation::template_fn(),
get_error_pages()
),
AppRoute::NotFound => template! {
p {"Not Found."}
}
Expand Down
60 changes: 60 additions & 0 deletions examples/showcase/app/src/pages/amalgamation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use perseus::{Template, StringResultWithCause, Request, States};
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
use std::sync::Arc;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct AmalagamationPageProps {
pub message: String,
}

#[component(AboutPage<G>)]
pub fn about_page(props: AmalagamationPageProps) -> SycamoreTemplate<G> {
template! {
p { (format!("The message is: '{}'", props.message)) }
}
}

pub fn get_page<G: GenericNode>() -> Template<G> {
Template::new("amalgamation")
.build_state_fn(Arc::new(get_build_state))
.request_state_fn(Arc::new(get_request_state))
.amalgamate_states_fn(Arc::new(amalgamate_states))
.template(template_fn())
}

pub fn amalgamate_states(states: States) -> StringResultWithCause<Option<String>> {
// We know they'll both be defined
let build_state = serde_json::from_str::<AmalagamationPageProps>(&states.build_state.unwrap()).unwrap();
let req_state = serde_json::from_str::<AmalagamationPageProps>(&states.request_state.unwrap()).unwrap();

Ok(Some(serde_json::to_string(&AmalagamationPageProps {
message: format!("Hello from the amalgamation! (Build says: '{}', server says: '{}'.)", build_state.message, req_state.message),
})
.unwrap()))
}

pub async fn get_build_state(_path: String) -> StringResultWithCause<String> {
Ok(serde_json::to_string(&AmalagamationPageProps {
message: "Hello from the build process!".to_string(),
})
.unwrap())
}

pub async fn get_request_state(_path: String, _req: Request) -> StringResultWithCause<String> {
// Err(("this is a test error!".to_string(), perseus::ErrorCause::Client(None)))
Ok(serde_json::to_string(&AmalagamationPageProps {
message: "Hello from the server!".to_string()
})
.unwrap())
}

pub fn template_fn<G: GenericNode>() -> perseus::template::TemplateFn<G> {
Arc::new(|props| {
template! {
AboutPage(
serde_json::from_str::<AmalagamationPageProps>(&props.unwrap()).unwrap()
)
}
})
}
4 changes: 3 additions & 1 deletion examples/showcase/app/src/pages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod new_post;
pub mod post;
pub mod time;
pub mod time_root;
pub mod amalgamation;

use perseus::{get_templates_map, Template};
use std::collections::HashMap;
Expand All @@ -19,6 +20,7 @@ pub fn get_templates_map<G: GenericNode>() -> HashMap<String, Template<G>> {
new_post::get_page::<G>(),
ip::get_page::<G>(),
time::get_page::<G>(),
time_root::get_page::<G>()
time_root::get_page::<G>(),
amalgamation::get_page::<G>()
]
}

0 comments on commit cd93fdc

Please sign in to comment.