v0.10-frontend-compiles2

This commit is contained in:
2022-10-31 00:24:15 +05:30
parent fe40782c0e
commit bc58e586f7
3 changed files with 45 additions and 39 deletions

View File

@@ -65,16 +65,16 @@ async fn fetch_books(search: String) -> Result<Vec<BookUI>, reqwasm::Error> {
Ok(body)
}
async fn add_book(record: BookUI) -> Result<String, reqwasm::Error> {
async fn add_book(record: BookUI) -> Result<reqwasm::http::Response, reqwasm::Error> {
let url = format!("http://localhost:8081/api/create");
let _resp = Request::post(&url).body(serde_wasm_bindgen::to_value(&record).unwrap()).send().await?;
Ok("OK".to_string())
let resp = Request::post(&url).body(serde_wasm_bindgen::to_value(&record).unwrap()).header("content-type","application/x-www-form-urlencoded").send().await?;
Ok(resp)
}
async fn delete_book(id: i32) -> Result<String, reqwasm::Error> {
async fn delete_book(id: i32) -> Result<reqwasm::http::Response, reqwasm::Error> {
let url = format!("http://localhost:8081/api/delete/{}", id);
let _resp = Request::post(&url).send().await?;
Ok("OK".to_string())
let resp = Request::post(&url).send().await?;
Ok(resp)
}
async fn list_books() -> Result<Vec<BookUI>, reqwasm::Error> {
@@ -175,7 +175,8 @@ pub fn BookDB<G: Html>(cx: Scope, bookitem: BookUIProp) -> View<G> {
let book = bookitem.bookitem.clone();
let handle_delete = move |_| {
spawn_local(async move {
delete_book(book.id).await.unwrap();
let temp = delete_book(book.id).await.unwrap();
println!("{}",temp.status());
});
};
@@ -198,11 +199,13 @@ async fn ListOL<G: Html>(cx: Scope<'_>) -> View<G> {
let app_state = app_state.clone();
app_state.search.track();
spawn_local(async move {
if *app_state.search.get() != "" {
app_state.books.set(
fetch_books(app_state.search.get().to_string())
.await
.unwrap(),
)
}
});
});
@@ -271,6 +274,7 @@ let inp_time = create_signal(cx, String::new());
let inp_isbn = create_signal(cx, String::new());
let handle_add = |_| {
info!("Adding book");
let authors: Vec<String> = (*inp_author.get()).clone().split(",").map(str::to_string).collect::<Vec<String>>();
let persons: Vec<String> = (*inp_person.get()).clone().split(",").map(str::to_string).collect::<Vec<String>>();
let places: Vec<String> = (*inp_place.get()).clone().split(",").map(|x| x.to_string()).collect::<Vec<String>>();
@@ -281,15 +285,15 @@ let inp_isbn = create_signal(cx, String::new());
id: 1,
title: (*inp_title.get()).clone(),
open_library_key: Some((*inp_olkey.get()).clone()),
edition_count: Some(inp_editioncount.get().parse::<i32>().unwrap()),
first_publish_year: Some(inp_publishyear.get().parse::<i32>().unwrap()),
median_page_count: Some(inp_medianpage.get().parse::<i32>().unwrap()),
edition_count: Some(inp_editioncount.get().parse::<i32>().unwrap_or(0)),
first_publish_year: Some(inp_publishyear.get().parse::<i32>().unwrap_or(0)),
median_page_count: Some(inp_medianpage.get().parse::<i32>().unwrap_or(0)),
goodread_id: Some((*inp_goodread.get()).clone()),
description: Some((*inp_desc.get()).clone()),
cover: Some((*inp_cover.get()).clone()),
location: Some((*inp_location.get()).clone()),
time_added: Some("NA".to_string()),
rating: Some(inp_rating.get().parse::<i32>().unwrap()),
rating: Some(inp_rating.get().parse::<i32>().unwrap_or(0)),
comments: Some((*inp_comments.get()).clone()),
author_name: Some(authors),
person: Some(persons),
@@ -298,37 +302,37 @@ let inp_isbn = create_signal(cx, String::new());
time: Some(times),
isbn: Some(isbns),
};
spawn_local(async move {
add_book(record).await.unwrap();
});
app_state.adding.set(false);
app_state.addingbook.set(BookUI::default());
spawn_local(async move {
let temp = add_book(record).await.unwrap();
println!("{}",temp.status());
});
};
view! {cx,
p {
(if *app_state.adding.get() == true {
view!{ cx,
"Hello"
input(bind:value=inp_title, placeholder= (*app_state.addingbook.get()).clone().title)
input(bind:value=inp_olkey, placeholder= (*app_state.addingbook.get()).clone().open_library_key.unwrap_or("".to_string()))
input(bind:value=inp_editioncount, placeholder= (*app_state.addingbook.get()).clone().edition_count.unwrap_or(0).to_string())
input(bind:value=inp_publishyear, placeholder= (*app_state.addingbook.get()).clone().first_publish_year.unwrap_or(0).to_string())
input(bind:value=inp_medianpage, placeholder= (*app_state.addingbook.get()).clone().median_page_count.unwrap_or(0).to_string())
input(bind:value=inp_goodread, placeholder= (*app_state.addingbook.get()).clone().goodread_id.unwrap_or("".to_string()))
input(bind:value=inp_desc, placeholder= (*app_state.addingbook.get()).clone().description.unwrap_or("".to_string()))
input(bind:value=inp_cover, placeholder= (*app_state.addingbook.get()).clone().cover.unwrap_or("".to_string()))
input(bind:value=inp_location, placeholder= (*app_state.addingbook.get()).clone().location.unwrap_or("".to_string()))
input(bind:value=inp_rating, placeholder= (*app_state.addingbook.get()).clone().rating.unwrap_or(0).to_string())
input(bind:value=inp_comments, placeholder= (*app_state.addingbook.get()).clone().comments.unwrap_or("".to_string()))
input(bind:value=inp_author, placeholder=(*app_state.addingbook.get()).clone().author_name.unwrap_or(vec!["".to_string()]).join(", "))
input(bind:value=inp_person, placeholder=(*app_state.addingbook.get()).clone().person.unwrap_or(vec!["".to_string()]).join(", "))
input(bind:value=inp_place, placeholder=(*app_state.addingbook.get()).clone().place.unwrap_or(vec!["".to_string()]).join(", "))
input(bind:value=inp_subject, placeholder=(*app_state.addingbook.get()).clone().subject.unwrap_or(vec!["".to_string()]).join(", "))
input(bind:value=inp_time, placeholder=(*app_state.addingbook.get()).clone().time.unwrap_or(vec!["".to_string()]).join(", "))
input(bind:value=inp_isbn, placeholder=(*app_state.addingbook.get()).clone().isbn.unwrap_or(vec!["".to_string()]).join(", "))
input(bind:value=inp_title, placeholder="Title" ,value = (*app_state.addingbook.get()).clone().title)
input(bind:value=inp_olkey, placeholder="OpenLibrary Key" ,value= (*app_state.addingbook.get()).clone().open_library_key.unwrap_or("".to_string()))
input(bind:value=inp_editioncount, placeholder="Number of editions" ,value= (*app_state.addingbook.get()).clone().edition_count.unwrap_or(0).to_string())
input(bind:value=inp_publishyear, placeholder="First publish year" ,value= (*app_state.addingbook.get()).clone().first_publish_year.unwrap_or(0).to_string())
input(bind:value=inp_medianpage, placeholder="Page count" ,value= (*app_state.addingbook.get()).clone().median_page_count.unwrap_or(0).to_string())
input(bind:value=inp_goodread, placeholder="GoodRead ID" ,value= (*app_state.addingbook.get()).clone().goodread_id.unwrap_or("".to_string()))
input(bind:value=inp_desc, placeholder="Description" ,value= (*app_state.addingbook.get()).clone().description.unwrap_or("".to_string()))
input(bind:value=inp_cover, placeholder="Cover URL" ,value= (*app_state.addingbook.get()).clone().cover.unwrap_or("".to_string()))
input(bind:value=inp_location, placeholder="Location" ,value= (*app_state.addingbook.get()).clone().location.unwrap_or("".to_string()))
input(bind:value=inp_rating, placeholder="Rating (/10)" ,value= (*app_state.addingbook.get()).clone().rating.unwrap_or(0).to_string())
input(bind:value=inp_comments, placeholder="Comments" ,value= (*app_state.addingbook.get()).clone().comments.unwrap_or("".to_string()))
input(bind:value=inp_author, placeholder="Authors" ,value=(*app_state.addingbook.get()).clone().author_name.unwrap_or(vec!["".to_string()]).join(", "))
input(bind:value=inp_person, placeholder="Persons" ,value=(*app_state.addingbook.get()).clone().person.unwrap_or(vec!["".to_string()]).join(", "))
input(bind:value=inp_place, placeholder="Places" ,value=(*app_state.addingbook.get()).clone().place.unwrap_or(vec!["".to_string()]).join(", "))
input(bind:value=inp_subject, placeholder="Subjects" ,value=(*app_state.addingbook.get()).clone().subject.unwrap_or(vec!["".to_string()]).join(", "))
input(bind:value=inp_time, placeholder="Times" ,value=(*app_state.addingbook.get()).clone().time.unwrap_or(vec!["".to_string()]).join(", "))
input(bind:value=inp_isbn, placeholder="ISBNs" ,value=(*app_state.addingbook.get()).clone().isbn.unwrap_or(vec!["".to_string()]).join(", "))
button(class="add", on:click=handle_add){ "Add book to DB" }
}
} else {