This commit is contained in:
2022-10-07 23:30:30 +05:30
parent 23799cbade
commit 48ab7519f2

View File

@@ -1,6 +1,8 @@
use log::info;
//use log::Level; //use log::Level;
use reqwasm::http::Request; use reqwasm::http::Request;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sycamore::futures::spawn_local;
use sycamore::prelude::*; use sycamore::prelude::*;
use sycamore::suspense::Suspense; use sycamore::suspense::Suspense;
use sycamore_router::Route; use sycamore_router::Route;
@@ -35,17 +37,6 @@ pub struct AppState {
pub search: RcSignal<String>, pub search: RcSignal<String>,
} }
impl AppState {
// #[tokio::main]
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)] #[derive(Route)]
enum AppRoutes { enum AppRoutes {
#[to("/")] #[to("/")]
@@ -78,9 +69,10 @@ pub fn Header<G: Html>(cx: Scope) -> View<G> {
task = task.trim().to_string(); task = task.trim().to_string();
if !task.is_empty() { if !task.is_empty() {
app_state.set_search(task); app_state.search.set(task);
println!("Fetching search\n"); println!("Fetching search\n");
info!("Fetching search\n");
value.set("".to_string()); value.set("".to_string());
input_ref input_ref
.get::<DomNode>() .get::<DomNode>()
@@ -106,20 +98,40 @@ pub fn Header<G: Html>(cx: Scope) -> View<G> {
#[component] #[component]
async fn VisitsCount<G: Html>(cx: Scope<'_>) -> View<G> { 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, || { /* create_effect(cx, || {
// println!("The state changed. New value: {}", app_state.search.get()) info!(
//}); "The state changed. New value: {} {}",
//let search = create_memo(cx, || app_state.search.get()); app_state.search.get(),
app_state.books.get().num_found
)
});
*/
// app_state.set_books(search.to_string()).await; // app_state.set_books(search.to_string()).await;
//app_state.set_books("Lord of the rings".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(); create_effect(cx, || {
print!("Visitcounts\n"); //info!(
// "The state changed. New value: {} {}",
// app_state.search.get(),
// app_state.books.get().num_found
//);
let app_state = app_state.clone();
spawn_local(async move {
app_state.books.set(
fetch_books(app_state.search.get().to_string())
.await
.unwrap(),
)
});
});
// let lbooks = fetch_books("lord of the rings".to_string()).await.unwrap();
view! {cx, view! {cx,
p { p {
"Total visits: " "Total visits: "
span { span {
(lbooks.num_found) //(search)
(app_state.books.get().num_found)
} }
} }
} }
@@ -135,12 +147,11 @@ fn App<G: Html>(cx: Scope) -> View<G> {
view! { view! {
cx, cx,
div { div {
//Header {} Header {}
p { "Page Visit Counter" } p { "Page Visit Counter" }
Suspense(fallback=view! { cx, "Loading..." }) { Suspense(fallback=view! { cx, "Loading..." }) {
VisitsCount {} VisitsCount {}
} }
//create_effect(cx, || println!("The state changed. New value: {}", state.get()));
} }
/* /*
Router { Router {