v0.11-updatingP1

This commit is contained in:
2022-11-06 22:21:29 +05:30
parent d0caca5e59
commit dfa83b68b0
3 changed files with 234 additions and 20 deletions

View File

@@ -50,6 +50,7 @@ pub struct AppState {
pub search: RcSignal<String>,
pub openlibrary: RcSignal<bool>,
pub adding: RcSignal<bool>,
pub updating: RcSignal<bool>,
pub addingbook: RcSignal<BookUI>,
}
@@ -77,6 +78,12 @@ async fn add_book(record: BookUI) -> Result<reqwasm::http::Response, reqwasm::Er
Ok(resp)
}
async fn update_book(record: BookUI) -> Result<reqwasm::http::Response, reqwasm::Error> {
let url = format!("http://localhost:8081/api/update");
let resp = Request::post(&url).body(serde_wasm_bindgen::to_value(&serde_json::to_string(&record).unwrap()).unwrap()).header("content-type","application/json").send().await?;
Ok(resp)
}
async fn delete_book(id: i32) -> Result<reqwasm::http::Response, reqwasm::Error> {
let url = format!("http://localhost:8081/api/delete/{}", id);
let resp = Request::get(&url).send().await?;
@@ -177,15 +184,23 @@ async fn ListDB<G: Html>(cx: Scope<'_>) -> View<G> {
#[component(inline_props)]
pub fn BookDB<G: Html>(cx: Scope, bookitem: BookUIProp) -> View<G> {
let app_state = use_context::<AppState>(cx);
let book = bookitem.bookitem.clone();
let coverurl = book.cover.clone().unwrap_or("http://localhost:8081/images/placeholder.jpg".to_string());
let bookdelete = bookitem.bookitem.clone();
let coverurl = book.cover.clone().unwrap_or("http://localhost:8081/images/placeholder.jpg".to_string());
let handle_delete = move |_| {
spawn_local(async move {
let temp = delete_book(book.id).await.unwrap();
println!("{}",temp.status());
});
};
let handle_update = move |_| {
app_state.adding.set(false);
app_state.updating.set(true);
app_state.addingbook.set(bookdelete.clone());
};
view! { cx,
div(class="column"){
div(class="card"){
@@ -193,6 +208,8 @@ pub fn BookDB<G: Html>(cx: Scope, bookitem: BookUIProp) -> View<G> {
(format!("{:?}",book))
button(class="delete", on:click=handle_delete){ "-" }
button(class="update", on:click=handle_update){ "=" }
}
}
@@ -316,7 +333,7 @@ info!("Adding book");
let times: Vec<String> = (*inp_time.get()).clone().split(",").map(|x| x.to_string()).collect::<Vec<String>>();
let isbns: Vec<String> = (*inp_isbn.get()).clone().split(",").map(|x| x.to_string()).collect::<Vec<String>>();
let record : BookUI = BookUI{
id: 1,
id: app_state.addingbook.get().id.clone(),
title: (*inp_title.get()).clone(),
open_library_key: Some((*inp_olkey.get()).clone()),
edition_count: Some(inp_editioncount.get().parse::<i32>().unwrap_or(0)),
@@ -337,18 +354,27 @@ info!("Adding book");
isbn: Some(isbns),
};
app_state.adding.set(false);
app_state.addingbook.set(BookUI::default());
if *app_state.updating.get() == false {
spawn_local(async move {
let temp = add_book(record).await.unwrap();
println!("{}",temp.status());
println!("{}",temp.status());
});
} else {
spawn_local(async move {
let temp = update_book(record).await.unwrap();
println!("{}",temp.status());
});
}
app_state.addingbook.set(BookUI::default());
app_state.updating.set(false);
app_state.adding.set(false);
};
view! {cx,
p {
(if *app_state.adding.get() == true {
(if *app_state.adding.get() == true || *app_state.updating.get() == true {
view!{ cx,
input(bind:value=inp_title, placeholder="Title" )
input(bind:value=inp_olkey, placeholder="OpenLibrary Key" )
@@ -385,6 +411,7 @@ fn App<G: Html>(cx: Scope) -> View<G> {
search: create_rc_signal(String::default()),
openlibrary: create_rc_signal(bool::default()),
adding: create_rc_signal(bool::default()),
updating: create_rc_signal(bool::default()),
addingbook: create_rc_signal(BookUI::default()),
};
provide_context(cx, app_state);