From 13dd5805322c78986c2bb51fc544713090325a94 Mon Sep 17 00:00:00 2001 From: LilUziCert Date: Sun, 28 Nov 2021 21:43:55 +0000 Subject: [PATCH 1/8] adding an example that fetches data over network and styling using tailwindcss --- examples/page-visit-counter/Cargo.toml | 17 +++++++ examples/page-visit-counter/Trunk.toml | 9 ++++ examples/page-visit-counter/index.html | 13 +++++ examples/page-visit-counter/index.scss | 3 ++ examples/page-visit-counter/src/main.rs | 63 ++++++++++++++++++++++++ examples/page-visit-counter/tailwind.css | 3 ++ 6 files changed, 108 insertions(+) create mode 100644 examples/page-visit-counter/Cargo.toml create mode 100644 examples/page-visit-counter/Trunk.toml create mode 100644 examples/page-visit-counter/index.html create mode 100644 examples/page-visit-counter/index.scss create mode 100644 examples/page-visit-counter/src/main.rs create mode 100644 examples/page-visit-counter/tailwind.css diff --git a/examples/page-visit-counter/Cargo.toml b/examples/page-visit-counter/Cargo.toml new file mode 100644 index 000000000..e11701215 --- /dev/null +++ b/examples/page-visit-counter/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "page-visit-counter" +version = "0.1.0" +edition = "2021" +publish = false +authors = ["Aayush Attri "] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +reqwasm = "0.3.1" +anyhow = "1.0" +serde = "1.0" +console_error_panic_hook = "0.1.7" +console_log = "0.2.0" +log = "0.4.14" +sycamore = { path = "../../packages/sycamore", features = ["futures"] } diff --git a/examples/page-visit-counter/Trunk.toml b/examples/page-visit-counter/Trunk.toml new file mode 100644 index 000000000..8a78695be --- /dev/null +++ b/examples/page-visit-counter/Trunk.toml @@ -0,0 +1,9 @@ +[build] +target = "index.html" +dist = "dist" + +[[hooks]] +stage = "build" +command = "sh" +command_arguments = ["-c", "npx tailwindcss -o ./dist/.stage/tailwind.css"] + diff --git a/examples/page-visit-counter/index.html b/examples/page-visit-counter/index.html new file mode 100644 index 000000000..6c93dbf51 --- /dev/null +++ b/examples/page-visit-counter/index.html @@ -0,0 +1,13 @@ + + + + + + + + Http Example App + + + + + diff --git a/examples/page-visit-counter/index.scss b/examples/page-visit-counter/index.scss new file mode 100644 index 000000000..82cea1a15 --- /dev/null +++ b/examples/page-visit-counter/index.scss @@ -0,0 +1,3 @@ +body { + font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; +} diff --git a/examples/page-visit-counter/src/main.rs b/examples/page-visit-counter/src/main.rs new file mode 100644 index 000000000..a019bfa9e --- /dev/null +++ b/examples/page-visit-counter/src/main.rs @@ -0,0 +1,63 @@ +use sycamore::prelude::*; + +use anyhow::Result; +use reqwasm::http::Request; +use serde::{Deserialize, Serialize}; + +use sycamore::futures::spawn_local_in_scope; + +// API that counts visits to the web-page +const API_BASE_URL: &str = "https://api.countapi.xyz/hit"; + +#[derive(Serialize, Deserialize, Debug)] +struct Visits { + value: u64, +} + +async fn fetch_visits(id: &str) -> Result { + let url = format!("{}/{}/hits", API_BASE_URL, id); + let resp = Request::get(&url).send().await?; + + let body = resp.json::().await?; + Ok(body) +} + +#[component(RenderVisits)] +fn render_visits(counter: ReadSignal) -> View { + view! { + div(class="flex h-screen") { + div(class="m-auto") { + h1(class="text-3xl text-blue-700 mb-2 p-1") { "Page Visit Counter" } + p(class="text-xl p-2") { + "Total visits: " + span(class="text-green-500"){ + (counter.get()) + } + } + } + } + } +} + +#[component(App)] +fn app() -> View { + let counter = Signal::::new(0); + + create_effect(cloned!((counter) => move || { + spawn_local_in_scope(cloned!((counter) => async move { + let website_id = "page-visit-counter.tyz"; + let visits = fetch_visits(website_id).await.unwrap(); + + counter.set(visits.value); + })) + })); + + view! { RenderVisits(counter.handle()) } +} + +fn main() { + console_error_panic_hook::set_once(); + console_log::init_with_level(log::Level::Debug).unwrap(); + + sycamore::render(|| view! { App() }); +} diff --git a/examples/page-visit-counter/tailwind.css b/examples/page-visit-counter/tailwind.css new file mode 100644 index 000000000..b5c61c956 --- /dev/null +++ b/examples/page-visit-counter/tailwind.css @@ -0,0 +1,3 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; From b53e32751788e4c10d52cc60095522b3104ba972 Mon Sep 17 00:00:00 2001 From: LilUziCert Date: Sun, 28 Nov 2021 21:44:35 +0000 Subject: [PATCH 2/8] updating the root cargo.toml to include new example --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index d143270c4..bf14215cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "examples/components", "examples/context", "examples/counter", + "examples/page-visit-counter", "examples/hello", "examples/hello-builder", "examples/hydrate", From 89d9dee9a96f18d4bb107468310c97ccc23faba6 Mon Sep 17 00:00:00 2001 From: LilUziCert Date: Tue, 30 Nov 2021 10:14:34 +0000 Subject: [PATCH 3/8] removing the tailwindcss setup --- examples/page-visit-counter/Cargo.toml | 2 +- examples/page-visit-counter/Trunk.toml | 9 --------- examples/page-visit-counter/src/main.rs | 14 +++++--------- examples/page-visit-counter/tailwind.css | 3 --- 4 files changed, 6 insertions(+), 22 deletions(-) delete mode 100644 examples/page-visit-counter/Trunk.toml delete mode 100644 examples/page-visit-counter/tailwind.css diff --git a/examples/page-visit-counter/Cargo.toml b/examples/page-visit-counter/Cargo.toml index e11701215..45d0d8a2a 100644 --- a/examples/page-visit-counter/Cargo.toml +++ b/examples/page-visit-counter/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "page-visit-counter" version = "0.1.0" -edition = "2021" +edition = "2018" publish = false authors = ["Aayush Attri "] diff --git a/examples/page-visit-counter/Trunk.toml b/examples/page-visit-counter/Trunk.toml deleted file mode 100644 index 8a78695be..000000000 --- a/examples/page-visit-counter/Trunk.toml +++ /dev/null @@ -1,9 +0,0 @@ -[build] -target = "index.html" -dist = "dist" - -[[hooks]] -stage = "build" -command = "sh" -command_arguments = ["-c", "npx tailwindcss -o ./dist/.stage/tailwind.css"] - diff --git a/examples/page-visit-counter/src/main.rs b/examples/page-visit-counter/src/main.rs index a019bfa9e..967879fcd 100644 --- a/examples/page-visit-counter/src/main.rs +++ b/examples/page-visit-counter/src/main.rs @@ -25,15 +25,11 @@ async fn fetch_visits(id: &str) -> Result { #[component(RenderVisits)] fn render_visits(counter: ReadSignal) -> View { view! { - div(class="flex h-screen") { - div(class="m-auto") { - h1(class="text-3xl text-blue-700 mb-2 p-1") { "Page Visit Counter" } - p(class="text-xl p-2") { - "Total visits: " - span(class="text-green-500"){ - (counter.get()) - } - } + div { + h1 { "Page Visit Counter" } + p { + "Total visits: " + (counter.get()) } } } diff --git a/examples/page-visit-counter/tailwind.css b/examples/page-visit-counter/tailwind.css deleted file mode 100644 index b5c61c956..000000000 --- a/examples/page-visit-counter/tailwind.css +++ /dev/null @@ -1,3 +0,0 @@ -@tailwind base; -@tailwind components; -@tailwind utilities; From 967872202c6228395fbb746ce743e4c694313f6e Mon Sep 17 00:00:00 2001 From: LilUziCert Date: Tue, 30 Nov 2021 10:15:40 +0000 Subject: [PATCH 4/8] extending the page visit counter example to include tailwindcss setup --- .../page-visit-counter-tailwindcss/Cargo.toml | 17 +++++ .../page-visit-counter-tailwindcss/Trunk.toml | 9 +++ .../page-visit-counter-tailwindcss/index.html | 13 ++++ .../page-visit-counter-tailwindcss/index.scss | 3 + .../src/main.rs | 63 +++++++++++++++++++ .../tailwind.css | 3 + 6 files changed, 108 insertions(+) create mode 100644 examples/page-visit-counter-tailwindcss/Cargo.toml create mode 100644 examples/page-visit-counter-tailwindcss/Trunk.toml create mode 100644 examples/page-visit-counter-tailwindcss/index.html create mode 100644 examples/page-visit-counter-tailwindcss/index.scss create mode 100644 examples/page-visit-counter-tailwindcss/src/main.rs create mode 100644 examples/page-visit-counter-tailwindcss/tailwind.css diff --git a/examples/page-visit-counter-tailwindcss/Cargo.toml b/examples/page-visit-counter-tailwindcss/Cargo.toml new file mode 100644 index 000000000..a305493e4 --- /dev/null +++ b/examples/page-visit-counter-tailwindcss/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "page-visit-counter-tailwindcss" +version = "0.1.0" +edition = "2018" +publish = false +authors = ["Aayush Attri "] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +reqwasm = "0.3.1" +anyhow = "1.0" +serde = "1.0" +console_error_panic_hook = "0.1.7" +console_log = "0.2.0" +log = "0.4.14" +sycamore = { path = "../../packages/sycamore", features = ["futures"] } diff --git a/examples/page-visit-counter-tailwindcss/Trunk.toml b/examples/page-visit-counter-tailwindcss/Trunk.toml new file mode 100644 index 000000000..8a78695be --- /dev/null +++ b/examples/page-visit-counter-tailwindcss/Trunk.toml @@ -0,0 +1,9 @@ +[build] +target = "index.html" +dist = "dist" + +[[hooks]] +stage = "build" +command = "sh" +command_arguments = ["-c", "npx tailwindcss -o ./dist/.stage/tailwind.css"] + diff --git a/examples/page-visit-counter-tailwindcss/index.html b/examples/page-visit-counter-tailwindcss/index.html new file mode 100644 index 000000000..6c93dbf51 --- /dev/null +++ b/examples/page-visit-counter-tailwindcss/index.html @@ -0,0 +1,13 @@ + + + + + + + + Http Example App + + + + + diff --git a/examples/page-visit-counter-tailwindcss/index.scss b/examples/page-visit-counter-tailwindcss/index.scss new file mode 100644 index 000000000..82cea1a15 --- /dev/null +++ b/examples/page-visit-counter-tailwindcss/index.scss @@ -0,0 +1,3 @@ +body { + font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; +} diff --git a/examples/page-visit-counter-tailwindcss/src/main.rs b/examples/page-visit-counter-tailwindcss/src/main.rs new file mode 100644 index 000000000..7ab94d0a6 --- /dev/null +++ b/examples/page-visit-counter-tailwindcss/src/main.rs @@ -0,0 +1,63 @@ +use sycamore::prelude::*; + +use anyhow::Result; +use reqwasm::http::Request; +use serde::{Deserialize, Serialize}; + +use sycamore::futures::spawn_local_in_scope; + +// API that counts visits to the web-page +const API_BASE_URL: &str = "https://api.countapi.xyz/hit"; + +#[derive(Serialize, Deserialize, Debug)] +struct Visits { + value: u64, +} + +async fn fetch_visits(id: &str) -> Result { + let url = format!("{}/{}/hits", API_BASE_URL, id); + let resp = Request::get(&url).send().await?; + + let body = resp.json::().await?; + Ok(body) +} + +#[component(RenderVisits)] +fn render_visits(counter: ReadSignal) -> View { + view! { + div(class="flex h-screen") { + div(class="m-auto") { + h1(class="text-3xl text-blue-700 mb-2 p-1") { "Page Visit Counter" } + p(class="text-xl p-2") { + "Total visits: " + span(class="text-green-500"){ + (counter.get()) + } + } + } + } + } +} + +#[component(App)] +fn app() -> View { + let counter = Signal::::new(0); + + create_effect(cloned!((counter) => move || { + spawn_local_in_scope(cloned!((counter) => async move { + let website_id = "page-visit-counter-tailwindcss.tyz"; + let visits = fetch_visits(website_id).await.unwrap(); + + counter.set(visits.value); + })) + })); + + view! { RenderVisits(counter.handle()) } +} + +fn main() { + console_error_panic_hook::set_once(); + console_log::init_with_level(log::Level::Debug).unwrap(); + + sycamore::render(|| view! { App() }); +} diff --git a/examples/page-visit-counter-tailwindcss/tailwind.css b/examples/page-visit-counter-tailwindcss/tailwind.css new file mode 100644 index 000000000..b5c61c956 --- /dev/null +++ b/examples/page-visit-counter-tailwindcss/tailwind.css @@ -0,0 +1,3 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; From fd29983761a3ed046de17665deca3e7d2a4ab561 Mon Sep 17 00:00:00 2001 From: LilUziCert Date: Tue, 30 Nov 2021 10:16:03 +0000 Subject: [PATCH 5/8] updating the root cargo.toml to include new examples --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index bf14215cf..6d3165999 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = [ "examples/context", "examples/counter", "examples/page-visit-counter", + "examples/page-visit-counter-tailwindcss", "examples/hello", "examples/hello-builder", "examples/hydrate", From 80cfb805efef5de5519f08190be463ad320b2b2f Mon Sep 17 00:00:00 2001 From: LilUziCert Date: Tue, 30 Nov 2021 10:18:37 +0000 Subject: [PATCH 6/8] removing link to tailwind Update examples/page-visit-counter-tailwindcss/Trunk.toml Co-authored-by: Luke Chu <37006668+lukechu10@users.noreply.github.com> Update examples/page-visit-counter-tailwindcss/src/main.rs Co-authored-by: Luke Chu <37006668+lukechu10@users.noreply.github.com> Update examples/page-visit-counter-tailwindcss/src/main.rs Co-authored-by: Luke Chu <37006668+lukechu10@users.noreply.github.com> --- examples/page-visit-counter-tailwindcss/Trunk.toml | 2 +- examples/page-visit-counter-tailwindcss/src/main.rs | 10 ++++------ examples/page-visit-counter/index.html | 1 - 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/examples/page-visit-counter-tailwindcss/Trunk.toml b/examples/page-visit-counter-tailwindcss/Trunk.toml index 8a78695be..53a1e6606 100644 --- a/examples/page-visit-counter-tailwindcss/Trunk.toml +++ b/examples/page-visit-counter-tailwindcss/Trunk.toml @@ -5,5 +5,5 @@ dist = "dist" [[hooks]] stage = "build" command = "sh" -command_arguments = ["-c", "npx tailwindcss -o ./dist/.stage/tailwind.css"] +command_arguments = ["-c", "npx -y tailwindcss -o ./dist/.stage/tailwind.css"] diff --git a/examples/page-visit-counter-tailwindcss/src/main.rs b/examples/page-visit-counter-tailwindcss/src/main.rs index 7ab94d0a6..b8aa90a61 100644 --- a/examples/page-visit-counter-tailwindcss/src/main.rs +++ b/examples/page-visit-counter-tailwindcss/src/main.rs @@ -1,10 +1,8 @@ -use sycamore::prelude::*; - use anyhow::Result; use reqwasm::http::Request; use serde::{Deserialize, Serialize}; - use sycamore::futures::spawn_local_in_scope; +use sycamore::prelude::*; // API that counts visits to the web-page const API_BASE_URL: &str = "https://api.countapi.xyz/hit"; @@ -30,9 +28,9 @@ fn render_visits(counter: ReadSignal) -> View { h1(class="text-3xl text-blue-700 mb-2 p-1") { "Page Visit Counter" } p(class="text-xl p-2") { "Total visits: " - span(class="text-green-500"){ - (counter.get()) - } + span(class="text-green-500") { + (counter.get()) + } } } } diff --git a/examples/page-visit-counter/index.html b/examples/page-visit-counter/index.html index 6c93dbf51..f720d663f 100644 --- a/examples/page-visit-counter/index.html +++ b/examples/page-visit-counter/index.html @@ -4,7 +4,6 @@ - Http Example App From 3ab3e2c1ee942c459f5471ccda66fac5ae3c3379 Mon Sep 17 00:00:00 2001 From: LilUziCert Date: Tue, 30 Nov 2021 21:39:23 +0000 Subject: [PATCH 7/8] addressing comments --- examples/page-visit-counter-tailwindcss/Trunk.toml | 2 +- examples/page-visit-counter-tailwindcss/index.html | 4 +--- examples/page-visit-counter-tailwindcss/index.scss | 3 --- examples/page-visit-counter-tailwindcss/src/main.rs | 4 ++-- examples/page-visit-counter/src/main.rs | 4 ++-- 5 files changed, 6 insertions(+), 11 deletions(-) delete mode 100644 examples/page-visit-counter-tailwindcss/index.scss diff --git a/examples/page-visit-counter-tailwindcss/Trunk.toml b/examples/page-visit-counter-tailwindcss/Trunk.toml index 53a1e6606..a1d380a58 100644 --- a/examples/page-visit-counter-tailwindcss/Trunk.toml +++ b/examples/page-visit-counter-tailwindcss/Trunk.toml @@ -5,5 +5,5 @@ dist = "dist" [[hooks]] stage = "build" command = "sh" -command_arguments = ["-c", "npx -y tailwindcss -o ./dist/.stage/tailwind.css"] +command_arguments = ["-c", "npx -y -c tailwindcss -o ./dist/.stage/tailwind.css"] diff --git a/examples/page-visit-counter-tailwindcss/index.html b/examples/page-visit-counter-tailwindcss/index.html index 6c93dbf51..823cbdb21 100644 --- a/examples/page-visit-counter-tailwindcss/index.html +++ b/examples/page-visit-counter-tailwindcss/index.html @@ -5,9 +5,7 @@ - Http Example App - - + TailwindCSS Example App diff --git a/examples/page-visit-counter-tailwindcss/index.scss b/examples/page-visit-counter-tailwindcss/index.scss deleted file mode 100644 index 82cea1a15..000000000 --- a/examples/page-visit-counter-tailwindcss/index.scss +++ /dev/null @@ -1,3 +0,0 @@ -body { - font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; -} diff --git a/examples/page-visit-counter-tailwindcss/src/main.rs b/examples/page-visit-counter-tailwindcss/src/main.rs index b8aa90a61..5e8d16fa5 100644 --- a/examples/page-visit-counter-tailwindcss/src/main.rs +++ b/examples/page-visit-counter-tailwindcss/src/main.rs @@ -7,7 +7,7 @@ use sycamore::prelude::*; // API that counts visits to the web-page const API_BASE_URL: &str = "https://api.countapi.xyz/hit"; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Default, Debug)] struct Visits { value: u64, } @@ -44,7 +44,7 @@ fn app() -> View { create_effect(cloned!((counter) => move || { spawn_local_in_scope(cloned!((counter) => async move { let website_id = "page-visit-counter-tailwindcss.tyz"; - let visits = fetch_visits(website_id).await.unwrap(); + let visits = fetch_visits(website_id).await.unwrap_or_default(); counter.set(visits.value); })) diff --git a/examples/page-visit-counter/src/main.rs b/examples/page-visit-counter/src/main.rs index 967879fcd..83ae88989 100644 --- a/examples/page-visit-counter/src/main.rs +++ b/examples/page-visit-counter/src/main.rs @@ -9,7 +9,7 @@ use sycamore::futures::spawn_local_in_scope; // API that counts visits to the web-page const API_BASE_URL: &str = "https://api.countapi.xyz/hit"; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Default, Debug)] struct Visits { value: u64, } @@ -42,7 +42,7 @@ fn app() -> View { create_effect(cloned!((counter) => move || { spawn_local_in_scope(cloned!((counter) => async move { let website_id = "page-visit-counter.tyz"; - let visits = fetch_visits(website_id).await.unwrap(); + let visits = fetch_visits(website_id).await.unwrap_or_default(); counter.set(visits.value); })) From dd1d2f11efbba31e7689b01d60031f4dac88cee4 Mon Sep 17 00:00:00 2001 From: Luke Chu <37006668+lukechu10@users.noreply.github.com> Date: Tue, 28 Dec 2021 13:29:36 -0800 Subject: [PATCH 8/8] Update reqwasm to v0.4.0 and remove non-tailwind example The non-tailwind example is not needed and is only duplicate code --- Cargo.toml | 1 - .../page-visit-counter-tailwindcss/Cargo.toml | 2 +- examples/page-visit-counter/Cargo.toml | 17 ------ examples/page-visit-counter/index.html | 12 ---- examples/page-visit-counter/index.scss | 3 - examples/page-visit-counter/src/main.rs | 59 ------------------- website/sitemap_index.xml | 1 + 7 files changed, 2 insertions(+), 93 deletions(-) delete mode 100644 examples/page-visit-counter/Cargo.toml delete mode 100644 examples/page-visit-counter/index.html delete mode 100644 examples/page-visit-counter/index.scss delete mode 100644 examples/page-visit-counter/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 6d3165999..07d70cf06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ members = [ "examples/components", "examples/context", "examples/counter", - "examples/page-visit-counter", "examples/page-visit-counter-tailwindcss", "examples/hello", "examples/hello-builder", diff --git a/examples/page-visit-counter-tailwindcss/Cargo.toml b/examples/page-visit-counter-tailwindcss/Cargo.toml index a305493e4..5aaa39cac 100644 --- a/examples/page-visit-counter-tailwindcss/Cargo.toml +++ b/examples/page-visit-counter-tailwindcss/Cargo.toml @@ -8,7 +8,7 @@ authors = ["Aayush Attri "] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -reqwasm = "0.3.1" +reqwasm = "0.4.0" anyhow = "1.0" serde = "1.0" console_error_panic_hook = "0.1.7" diff --git a/examples/page-visit-counter/Cargo.toml b/examples/page-visit-counter/Cargo.toml deleted file mode 100644 index 45d0d8a2a..000000000 --- a/examples/page-visit-counter/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "page-visit-counter" -version = "0.1.0" -edition = "2018" -publish = false -authors = ["Aayush Attri "] - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -reqwasm = "0.3.1" -anyhow = "1.0" -serde = "1.0" -console_error_panic_hook = "0.1.7" -console_log = "0.2.0" -log = "0.4.14" -sycamore = { path = "../../packages/sycamore", features = ["futures"] } diff --git a/examples/page-visit-counter/index.html b/examples/page-visit-counter/index.html deleted file mode 100644 index f720d663f..000000000 --- a/examples/page-visit-counter/index.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - Http Example App - - - - - diff --git a/examples/page-visit-counter/index.scss b/examples/page-visit-counter/index.scss deleted file mode 100644 index 82cea1a15..000000000 --- a/examples/page-visit-counter/index.scss +++ /dev/null @@ -1,3 +0,0 @@ -body { - font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; -} diff --git a/examples/page-visit-counter/src/main.rs b/examples/page-visit-counter/src/main.rs deleted file mode 100644 index 83ae88989..000000000 --- a/examples/page-visit-counter/src/main.rs +++ /dev/null @@ -1,59 +0,0 @@ -use sycamore::prelude::*; - -use anyhow::Result; -use reqwasm::http::Request; -use serde::{Deserialize, Serialize}; - -use sycamore::futures::spawn_local_in_scope; - -// API that counts visits to the web-page -const API_BASE_URL: &str = "https://api.countapi.xyz/hit"; - -#[derive(Serialize, Deserialize, Default, Debug)] -struct Visits { - value: u64, -} - -async fn fetch_visits(id: &str) -> Result { - let url = format!("{}/{}/hits", API_BASE_URL, id); - let resp = Request::get(&url).send().await?; - - let body = resp.json::().await?; - Ok(body) -} - -#[component(RenderVisits)] -fn render_visits(counter: ReadSignal) -> View { - view! { - div { - h1 { "Page Visit Counter" } - p { - "Total visits: " - (counter.get()) - } - } - } -} - -#[component(App)] -fn app() -> View { - let counter = Signal::::new(0); - - create_effect(cloned!((counter) => move || { - spawn_local_in_scope(cloned!((counter) => async move { - let website_id = "page-visit-counter.tyz"; - let visits = fetch_visits(website_id).await.unwrap_or_default(); - - counter.set(visits.value); - })) - })); - - view! { RenderVisits(counter.handle()) } -} - -fn main() { - console_error_panic_hook::set_once(); - console_log::init_with_level(log::Level::Debug).unwrap(); - - sycamore::render(|| view! { App() }); -} diff --git a/website/sitemap_index.xml b/website/sitemap_index.xml index 599b2f71d..7fff9a41a 100644 --- a/website/sitemap_index.xml +++ b/website/sitemap_index.xml @@ -78,6 +78,7 @@ https://sycamore-rs.netlify.app/examples/higher-order-componentsmonthly0.5 https://sycamore-rs.netlify.app/examples/hydratemonthly0.5 https://sycamore-rs.netlify.app/examples/iterationmonthly0.5 +https://sycamore-rs.netlify.app/examples/page-visit-counter-tailwindcssmonthly0.5 https://sycamore-rs.netlify.app/examples/ssrmonthly0.5 https://sycamore-rs.netlify.app/examples/todomvcmonthly0.5 https://sycamore-rs.netlify.app/examples/tweenedmonthly0.5