This commit is contained in:
2022-10-06 23:35:33 +05:30
parent 51d455b9c2
commit 23799cbade
2 changed files with 26 additions and 15 deletions

View File

@@ -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<Books>,
pub search: RcSignal<String>,
}
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<Books, reqwasm::Error> {
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::<Books>().await?;
Ok(body)
}
@@ -73,7 +78,9 @@ pub fn Header<G: Html>(cx: Scope) -> View<G> {
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::<DomNode>()
@@ -93,18 +100,26 @@ pub fn Header<G: Html>(cx: Scope) -> View<G> {
on:keyup=handle_submit,
)
}
//VisitsCount {}
}
}
#[component]
async fn VisitsCount<G: Html>(cx: Scope<'_>) -> View<G> {
let app_state = use_context::<AppState>(cx);
//let app_state = use_context::<AppState>(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<G: Html>(cx: Scope<'_>) -> View<G> {
fn App<G: Html>(cx: Scope) -> View<G> {
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<G: Html>(cx: Scope) -> View<G> {
}
}
#[component(HelloServer<G>)]
fn HelloServer<G: Html>(cx: Scope) -> View<G> {
view! {cx,
div{"No server response"}
}
}
fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();