From f47102cc7d7a914490737b86ccec5ef243ccb874 Mon Sep 17 00:00:00 2001 From: Vinod J M Date: Mon, 3 Oct 2022 00:05:40 +0530 Subject: [PATCH] v0.02ui --- backend/src/main.rs | 2 +- frontend/Cargo.lock | 33 ++++++++++++- frontend/Cargo.toml | 2 + frontend/src/main.rs | 107 +++++++++++++++++++++++++++++++------------ 4 files changed, 113 insertions(+), 31 deletions(-) diff --git a/backend/src/main.rs b/backend/src/main.rs index 08b6c2e..cc0148a 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -42,7 +42,7 @@ struct Opt { addr: String, /// set the listen port - #[clap(short = 'p', long = "port", default_value = "8080")] + #[clap(short = 'p', long = "port", default_value = "8081")] port: u16, /// set the directory where static files are to be found diff --git a/frontend/Cargo.lock b/frontend/Cargo.lock index 4265031..8e6a908 100644 --- a/frontend/Cargo.lock +++ b/frontend/Cargo.lock @@ -92,8 +92,10 @@ dependencies = [ "console_error_panic_hook", "console_log", "env_logger", - "gloo-net", + "gloo-net 0.2.4", "log", + "reqwasm", + "serde", "sycamore", "sycamore-router", "wasm-bindgen-futures", @@ -200,6 +202,26 @@ dependencies = [ "wasi", ] +[[package]] +name = "gloo-net" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2899cb1a13be9020b010967adc6b2a8a343b6f1428b90238c9d53ca24decc6db" +dependencies = [ + "futures-channel", + "futures-core", + "futures-sink", + "gloo-utils", + "js-sys", + "pin-project", + "serde", + "serde_json", + "thiserror", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "gloo-net" version = "0.2.4" @@ -404,6 +426,15 @@ version = "0.6.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" +[[package]] +name = "reqwasm" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05b89870d729c501fa7a68c43bf4d938bbb3a8c156d333d90faa0e8b3e3212fb" +dependencies = [ + "gloo-net 0.1.0", +] + [[package]] name = "ryu" version = "1.0.11" diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index 5044f96..e366c07 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -11,8 +11,10 @@ env_logger = "0.9.0" gloo-net = "^0.2" log = "0.4" console_log = { version = "0.2", features = ["color"] } +reqwasm = {version = "0.5.0", features = ["json"]} wasm-bindgen-futures = "^0.4" wasm-logger = "0.2.0" +serde = { version = "^1.0", features = ["derive"] } sycamore = {version = "0.8.0-beta.7", features = ["suspense"]} sycamore-router = "0.8.0-beta.7" #yew = "0.19.3" diff --git a/frontend/src/main.rs b/frontend/src/main.rs index 5e213a3..1a7f603 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -1,9 +1,31 @@ -use gloo_net::http::Request; -use wasm_bindgen_futures::spawn_local; +use log::Level; +use reqwasm::http::Request; +use serde::{Deserialize, Serialize}; use sycamore::prelude::*; use sycamore::suspense::Suspense; use sycamore_router::{Route, Router, RouterProps}; -use log::Level; +use wasm_bindgen_futures::spawn_local; + +#[derive(Deserialize, Serialize, Debug)] +struct Docs { + key: String, + title: String, + first_publish_year: Option, + number_of_pages_median: Option, + isbn: Option>, + cover_i: Option, + author_name: Option>, + person: Option>, + place: Option>, + subject: Option>, + time: Option>, +} + +#[derive(Deserialize, Serialize, Debug)] +struct Books { + num_found: u32, + docs: Vec, +} #[derive(Route)] enum AppRoutes { @@ -15,41 +37,68 @@ enum AppRoutes { NotFound, } +async fn fetch_books(search: &str) -> Result { + let url = format!("http://127.0.0.1:8080/api/hello?search={}", search); + let resp = Request::get(&url).send().await?; + + let body = resp.json::().await?; + Ok(body) +} + #[component] -fn App(cx: Scope) -> View { -view! {cx, - div{"Test"} -/* -Router { - integration: {HistoryIntegration::new()}, - view: |cx, route: &ReadSignal| { - view! { - div(class="app") { - (match route.get().as_ref() { - AppRoutes::Home => view! { cx, - "This is the index page" - }, - AppRoutes::HelloServer => view! { cx, - "About this website" - }, - AppRoutes::NotFound => view! { cx, - "404 Not Found" - }, - }) - } +async fn VisitsCount(cx: Scope<'_>) -> View { + let books = fetch_books("foundation").await.unwrap(); + print!("Visitcounts\n"); + view! {cx, + p { + "Total visits: " + span { + (books.num_found) } } } - */ +} + +#[component] +fn App(cx: Scope) -> View { + view! { + cx, + div { + p { "Page Visit Counter" } + Suspense(fallback=view! { cx, "Loading..." }) { + VisitsCount {} + } + } + /* + Router { + integration: {HistoryIntegration::new()}, + view: |cx, route: &ReadSignal| { + view! { + div(class="app") { + (match route.get().as_ref() { + AppRoutes::Home => view! { cx, + "This is the index page" + }, + AppRoutes::HelloServer => view! { cx, + "About this website" + }, + AppRoutes::NotFound => view! { cx, + "404 Not Found" + }, + }) + } + } + } + } + */ } } #[component(HelloServer)] fn HelloServer(cx: Scope) -> View { - - view! {cx, - div{"No server response"} - } + view! {cx, + div{"No server response"} + } } fn main() {