This commit is contained in:
2022-10-03 00:05:40 +05:30
parent 0423e142e0
commit f47102cc7d
4 changed files with 113 additions and 31 deletions

View File

@@ -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<u32>,
number_of_pages_median: Option<u32>,
isbn: Option<Vec<String>>,
cover_i: Option<u32>,
author_name: Option<Vec<String>>,
person: Option<Vec<String>>,
place: Option<Vec<String>>,
subject: Option<Vec<String>>,
time: Option<Vec<String>>,
}
#[derive(Deserialize, Serialize, Debug)]
struct Books {
num_found: u32,
docs: Vec<Docs>,
}
#[derive(Route)]
enum AppRoutes {
@@ -15,41 +37,68 @@ enum AppRoutes {
NotFound,
}
async fn fetch_books(search: &str) -> Result<Books, reqwasm::Error> {
let url = format!("http://127.0.0.1:8080/api/hello?search={}", search);
let resp = Request::get(&url).send().await?;
let body = resp.json::<Books>().await?;
Ok(body)
}
#[component]
fn App<G: Html>(cx: Scope) -> View<G> {
view! {cx,
div{"Test"}
/*
Router {
integration: {HistoryIntegration::new()},
view: |cx, route: &ReadSignal<AppRoutes>| {
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<G: Html>(cx: Scope<'_>) -> View<G> {
let books = fetch_books("foundation").await.unwrap();
print!("Visitcounts\n");
view! {cx,
p {
"Total visits: "
span {
(books.num_found)
}
}
}
*/
}
#[component]
fn App<G: Html>(cx: Scope) -> View<G> {
view! {
cx,
div {
p { "Page Visit Counter" }
Suspense(fallback=view! { cx, "Loading..." }) {
VisitsCount {}
}
}
/*
Router {
integration: {HistoryIntegration::new()},
view: |cx, route: &ReadSignal<AppRoutes>| {
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<G>)]
fn HelloServer<G: Html>(cx: Scope) -> View<G> {
view! {cx,
div{"No server response"}
}
view! {cx,
div{"No server response"}
}
}
fn main() {