-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: added new example for fetching data
Addresses #96.
- Loading branch information
1 parent
5c338aa
commit 6b08ffe
Showing
9 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
.perseus/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "perseus-example-fetching" | ||
version = "0.3.0" | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
perseus = { path = "../../packages/perseus", features = [ "hydrate" ] } | ||
sycamore = "0.7" | ||
serde = { version = "1", features = ["derive"] } | ||
serde_json = "1" | ||
ureq = "2" | ||
reqwasm = "0.4" | ||
wasm-bindgen-futures = "0.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is the message! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use perseus::{Html, RenderFnResultWithCause, Template}; | ||
use serde::{Deserialize, Serialize}; | ||
use sycamore::prelude::*; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct IndexProps { | ||
ip: String, | ||
} | ||
|
||
#[perseus::template(IndexPage)] | ||
#[component(IndexPage<G>)] | ||
pub fn index_page(IndexProps { ip }: IndexProps) -> View<G> { | ||
// This will store the message that we get | ||
// Until we've got it, we'll display `fetching...` | ||
let message = Signal::new("fetching...".to_string()); | ||
|
||
// This will only run in the browser | ||
// `reqwasm` wraps browser-specific APIs, so we don't want it running on the server | ||
if G::IS_BROWSER { | ||
// Spawn a `Future` on this thread to fetch the data | ||
// Don't worry, this doesn't need to be sent to JavaScript for execution | ||
// | ||
// We want to access the `message` `Signal`, so we'll clone it in (and then we need `move` because this has to be `'static`) | ||
wasm_bindgen_futures::spawn_local(cloned!(message => async move { | ||
// This interface may seem weird, that's because it wraps the browser's Fetch API | ||
// We request from a local path here because of CORS restrictions (see the book) | ||
let body = reqwasm::http::Request::get("/message") | ||
.send() | ||
.await | ||
.unwrap() | ||
.text() | ||
.await | ||
.unwrap(); | ||
message.set(body); | ||
})); | ||
} | ||
|
||
view! { | ||
p { (format!("IP address of the builder was: {}", ip)) } | ||
p { (format!("The message at `/message` is: {}", message.get())) } | ||
} | ||
} | ||
|
||
pub fn get_template<G: Html>() -> Template<G> { | ||
Template::new("index") | ||
.build_state_fn(get_build_state) | ||
.template(index_page) | ||
} | ||
|
||
#[perseus::autoserde(build_state)] | ||
pub async fn get_build_state( | ||
_path: String, | ||
_locale: String, | ||
) -> RenderFnResultWithCause<IndexProps> { | ||
// This just gets the IP address of the machine that built the app | ||
let body: String = ureq::get("https://api.ipify.org").call()?.into_string()?; | ||
Ok(IndexProps { ip: body }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
mod index; | ||
|
||
use perseus::define_app; | ||
define_app! { | ||
templates: [ | ||
index::get_template::<G>() | ||
], | ||
error_pages: perseus::ErrorPages::new(|url, status, err, _| { | ||
sycamore::view! { | ||
p { (format!("An error with HTTP code {} occurred at '{}': '{}'.", status, url, err)) } | ||
} | ||
}), | ||
static_aliases: { | ||
"/message" => "message.txt" | ||
} | ||
} |