From 23799cbade40fdcc187e2c1c1ff6e81ad00b276c Mon Sep 17 00:00:00 2001 From: Vinod J M Date: Thu, 6 Oct 2022 23:35:33 +0530 Subject: [PATCH] v0.04a --- frontend/Cargo.toml | 1 + frontend/src/main.rs | 40 +++++++++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index 32d6d7d..4c97a78 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -11,6 +11,7 @@ env_logger = "0.9.0" gloo-net = "^0.2" log = "0.4" console_log = { version = "0.2", features = ["color"] } +#reqwest = {version = "0.11.11", features = ["blocking", "json"]} reqwasm = {version = "0.5.0", features = ["json"]} wasm-bindgen-futures = "^0.4" wasm-logger = "0.2.0" diff --git a/frontend/src/main.rs b/frontend/src/main.rs index a0153f8..7dc10f5 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -1,9 +1,9 @@ -use log::Level; +//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 sycamore_router::Route; //use tokio; use wasm_bindgen::JsCast; use web_sys::{Event, HtmlInputElement, KeyboardEvent}; // 0.3.5 @@ -32,6 +32,7 @@ pub struct Books { #[derive(Debug, Default, Clone)] pub struct AppState { pub books: RcSignal, + pub search: RcSignal, } impl AppState { @@ -39,6 +40,10 @@ impl AppState { async fn set_books(&self, search: String) { self.books.set(fetch_books(search).await.unwrap()); } + + fn set_search(&self, search: String) { + self.search.set(search); + } } #[derive(Route)] @@ -52,9 +57,9 @@ enum AppRoutes { } async fn fetch_books(search: String) -> Result { - let url = format!("http://127.0.0.1:8080/api/hello?search={}", search); + let url = format!("http://localhost:8081/api/hello?search={}", search); let resp = Request::get(&url).send().await?; - + println!("Fetching books\n"); let body = resp.json::().await?; Ok(body) } @@ -73,7 +78,9 @@ pub fn Header(cx: Scope) -> View { task = task.trim().to_string(); if !task.is_empty() { - app_state.set_books(task); + app_state.set_search(task); + + println!("Fetching search\n"); value.set("".to_string()); input_ref .get::() @@ -93,18 +100,26 @@ pub fn Header(cx: Scope) -> View { on:keyup=handle_submit, ) } + //VisitsCount {} } } #[component] async fn VisitsCount(cx: Scope<'_>) -> View { - let app_state = use_context::(cx); + //let app_state = use_context::(cx); + //create_effect(cx, || { + // println!("The state changed. New value: {}", app_state.search.get()) + //}); + //let search = create_memo(cx, || app_state.search.get()); + // app_state.set_books(search.to_string()).await; + //app_state.set_books("Lord of the rings".to_string()).await; + let lbooks = fetch_books("lord of the rings".to_string()).await.unwrap(); print!("Visitcounts\n"); view! {cx, p { "Total visits: " span { - (app_state.books.get().num_found) + (lbooks.num_found) } } } @@ -114,16 +129,18 @@ async fn VisitsCount(cx: Scope<'_>) -> View { fn App(cx: Scope) -> View { let app_state = AppState { books: create_rc_signal(Books::default()), + search: create_rc_signal(String::default()), }; provide_context(cx, app_state); view! { cx, div { - Header {} + //Header {} p { "Page Visit Counter" } Suspense(fallback=view! { cx, "Loading..." }) { VisitsCount {} } + //create_effect(cx, || println!("The state changed. New value: {}", state.get())); } /* Router { @@ -150,13 +167,6 @@ fn App(cx: Scope) -> View { } } -#[component(HelloServer)] -fn HelloServer(cx: Scope) -> View { - view! {cx, - div{"No server response"} - } -} - fn main() { console_error_panic_hook::set_once(); console_log::init_with_level(log::Level::Debug).unwrap();