diff --git a/frontend/src/main.rs b/frontend/src/main.rs index b0a11b6..651fbd9 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -9,8 +9,32 @@ use sycamore_router::Route; use wasm_bindgen::JsCast; use web_sys::{Event, HtmlInputElement, KeyboardEvent}; // 0.3.5 +#[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq, Hash, Eq)] +pub struct BookUI { + id: i32, + open_library_key: Option, + title: String, + edition_count: Option, + first_publish_year: Option, + median_page_count: Option, + goodread_id: Option, + description: Option, + cover: Option, + location: Option, + time_added: Option, + rating: Option, + comments: Option, + author_name: Option>, + person: Option>, + place: Option>, + subject: Option>, + time: Option>, + isbn: Option>, +} + #[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq, Hash, Eq)] pub struct Docs { + id: u32, key: String, title: String, first_publish_year: Option, @@ -32,7 +56,7 @@ pub struct Books { #[derive(Debug, Default, Clone)] pub struct AppState { - pub books: RcSignal, + pub books: RcSignal>, pub search: RcSignal, } @@ -46,11 +70,11 @@ enum AppRoutes { NotFound, } -async fn fetch_books(search: String) -> Result { - let url = format!("http://localhost:8081/api/hello?search={}", search); +async fn fetch_books(search: String) -> Result, reqwasm::Error> { + let url = format!("http://localhost:8081/api/search_openlibrary?search={}", search); let resp = Request::get(&url).send().await?; println!("Fetching books\n"); - let body = resp.json::().await?; + let body = resp.json::>().await?; Ok(body) } @@ -112,19 +136,26 @@ async fn VisitsCount(cx: Scope<'_>) -> View { ) }); }); - let docs = create_memo(cx, || app_state.books.get().docs.clone()); + let state = create_signal(cx, 0i32); +// let increment = |x: Docs| (state.set(x.first_publish_year.unwrap().try_into().unwrap())); + let increment = |_| (state.set(2)); + + let docs = create_memo(cx, || app_state.books.get().iter().cloned().collect::>()); //let docs = create_signal(cx, vec![1, 2]); view! {cx, p { (if !app_state.search.get().is_empty() { view!{ cx, - span {(app_state.books.get().num_found)} + //span {(app_state.books.get().num_found)} ul { - Indexed( + Keyed( iterable=docs, - view=|cx, x| view! { cx, - li { (format!("{:?}",x)) } - }) + view=move |cx, x| view! { cx, + li { (format!("{:?}",x)) + button(on:click=increment) { "+" } + } + }, + key =|x| x.id ) } } } @@ -138,7 +169,7 @@ async fn VisitsCount(cx: Scope<'_>) -> View { #[component] fn App(cx: Scope) -> View { let app_state = AppState { - books: create_rc_signal(Books::default()), + books: create_rc_signal(Vec::new()), search: create_rc_signal(String::default()), }; provide_context(cx, app_state);