v0.12-searchP3

This commit is contained in:
2022-11-27 17:48:14 +05:30
parent f297d8e4e0
commit db0d976192
6 changed files with 393 additions and 166 deletions

View File

@@ -1,5 +1,7 @@
use log::info;
//use log::Level;
use std::env;
use reqwasm::http::Request;
use serde::{Deserialize, Serialize};
use serde_json;
@@ -69,41 +71,63 @@ enum AppRoutes {
}
async fn fetch_books(search: String) -> Result<Vec<BookUI>, reqwasm::Error> {
let url = format!("http://localhost:8081/api/search_openlibrary?search={}", search);
dotenvy::dotenv().ok();
let backend_url = env::var("BACKEND_URL").expect("BACKEND_URL is not set in .env file");
let url = format!("{}/api/search_openlibrary?search={}", backend_url, search);
let resp = Request::get(&url).send().await?;
println!("Fetching books\n");
let body = resp.json::<Vec<BookUI>>().await?;
Ok(body)
}
async fn search_books(search: String, page: u32) -> Result<Vec<BookUI>, reqwasm::Error> {
let url = format!("http://localhost:8081/api/list_search?search={}&page={}", search, page);
async fn search_books(search: String, page: u32) -> Result<PaginatedBookUIList, reqwasm::Error> {
dotenvy::dotenv().ok();
let backend_url = env::var("BACKEND_URL").expect("BACKEND_URL is not set in .env file");
let url = format!("{}/api/list_search?search={}&page={}",backend_url, search, page);
let resp = Request::get(&url).send().await?;
println!("Fetching books\n");
let body = resp.json::<Vec<PaginatedBookUIList>().await?;
let body = resp.json::<PaginatedBookUIList>().await?;
Ok(body)
}
async fn add_book(record: BookUI) -> Result<reqwasm::http::Response, reqwasm::Error> {
let url = format!("http://localhost:8081/api/create");
dotenvy::dotenv().ok();
let backend_url = env::var("BACKEND_URL").expect("BACKEND_URL is not set in .env file");
let url = format!("{}/api/create", backend_url);
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 add_books_isbns(isbns: String) -> Result<reqwasm::http::Response, reqwasm::Error> {
dotenvy::dotenv().ok();
let backend_url = env::var("BACKEND_URL").expect("BACKEND_URL is not set in .env file");
let url = format!("{}/api/create_by_isbn?isbns={}", backend_url, isbns);
let resp = Request::get(&url).send().await?;
println!("Adding multiple books\n");
Ok(resp)
}
async fn update_book(record: BookUI) -> Result<reqwasm::http::Response, reqwasm::Error> {
let url = format!("http://localhost:8081/api/update");
dotenvy::dotenv().ok();
let backend_url = env::var("BACKEND_URL").expect("BACKEND_URL is not set in .env file");
let url = format!("{}/api/update", backend_url);
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);
dotenvy::dotenv().ok();
let backend_url = env::var("BACKEND_URL").expect("BACKEND_URL is not set in .env file");
let url = format!("{}/api/delete/{}", backend_url, id);
let resp = Request::get(&url).send().await?;
Ok(resp)
}
async fn list_books(page: u32) -> Result<PaginatedBookUIList, reqwasm::Error> {
let url = format!("http://localhost:8081/api/list?page={}",page);
dotenvy::dotenv().ok();
let backend_url = env::var("BACKEND_URL").expect("BACKEND_URL is not set in .env file");
let url = format!("{}/api/list?page={}", backend_url, page);
let resp = Request::get(&url).send().await?;
println!("Fetching books\n");
let body = resp.json::<PaginatedBookUIList>().await?;
@@ -116,6 +140,13 @@ pub fn Header<G: Html>(cx: Scope) -> View<G> {
let value = create_signal(cx, String::new());
let input_ref = create_node_ref(cx);
let value2 = create_signal(cx, String::new());
let input_ref2 = create_node_ref(cx);
let value3 = create_signal(cx, String::new());
let input_ref3 = create_node_ref(cx);
let handle_submit = |event: Event| {
let event: KeyboardEvent = event.unchecked_into();
@@ -137,26 +168,53 @@ pub fn Header<G: Html>(cx: Scope) -> View<G> {
}
}
};
let handle_submit_seachall = |event: Event| {
let event: KeyboardEvent = event.unchecked_into();
if event.key() == "Enter" {
let mut task = value2.get().as_ref().clone();
task = task.trim().to_string();
if !task.is_empty() {
app_state.search.set(task);
app_state.openlibrary.set(false);
app_state.internalsearch.set(true);
info!("Fetching search\n");
}
}
};
let click_listall = |_| {
app_state.openlibrary.set(false);
app_state.internalsearch.set(false);
};
let click_searchall = |_| {
let click_searchOL = |_| {
let mut task = value.get().as_ref().clone();
task = task.trim().to_string();
if !task.is_empty() {
app_state.search.set(task);
app_state.openlibrary.set(true);
app_state.internalsearch.set(false);
}
};
let click_searchall = |_| {
let mut task = value2.get().as_ref().clone();
task = task.trim().to_string();
if !task.is_empty() {
app_state.search.set(task);
app_state.openlibrary.set(false);
app_state.internalsearch.set(true);
value.set("".to_string());
input_ref
.get::<DomNode>()
.unchecked_into::<HtmlInputElement>()
.set_value("");
}
};
@@ -165,6 +223,35 @@ pub fn Header<G: Html>(cx: Scope) -> View<G> {
app_state.addingbook.set(BookUI::default());
};
let handle_submit_addbulk = |event: Event| {
let event: KeyboardEvent = event.unchecked_into();
if event.key() == "Enter" {
let mut task = value3.get().as_ref().clone();
task = task.trim().to_string();
if !task.is_empty() {
spawn_local(async move {
let temp = add_books_isbns(task).await.unwrap();
});
}
}
};
let click_addbulk = |_| {
let mut task = value3.get().as_ref().clone();
task = task.trim().to_string();
if !task.is_empty() {
spawn_local(async move {
let temp = add_books_isbns(task).await.unwrap();
});
}
};
view! { cx,
header(class="header") {
h1 { "Search OpenLibrary" }
@@ -174,9 +261,27 @@ pub fn Header<G: Html>(cx: Scope) -> View<G> {
bind:value=value,
on:keyup=handle_submit,
)
button(on:click=click_searchOL) { "Search internal" }
button(on:click=click_listall) { "List All DB" }
input(ref=input_ref2,
class="new-todo",
placeholder="Search internal DB",
bind:value=value2,
on:keyup=handle_submit_seachall,
)
button(on:click=click_searchall) { "Search internal" }
button(on:click=click_addbook) { "+ Add New" }
input(ref=input_ref3,
class="new-todo",
placeholder="Add bulk ISBNs",
bind:value=value3,
on:keyup=handle_submit_addbulk,
)
button(on:click=click_addbulk) { "Add bulk ISBNs" }
}
}
}
@@ -193,19 +298,18 @@ async fn ListDB<G: Html>(cx: Scope<'_>) -> View<G> {
if *app_state.internalsearch.get() == false {
spawn_local(async move {
app_state.books.set(
list_books(app_state.pagenum.get())
list_books(*app_state.pagenum.get())
.await
.unwrap().books,
.unwrap().books
)
});
} else {
spawn_local(async move {
app_state.books.set(
search_books(app_state.search.get().to_string(), app_state.pagenum.get())
search_books(app_state.search.get().to_string(), *app_state.pagenum.get())
.await
.unwrap(),
)
}
.unwrap().books)
})
}
}
@@ -242,7 +346,7 @@ pub fn BookDB<G: Html>(cx: Scope, bookitem: BookUIProp) -> View<G> {
let bookupdate = bookitem.bookitem.clone();
let bookdisplay = bookitem.bookitem.clone();
let coverurl = book.cover.clone().unwrap_or("http://localhost:8081/images/placeholder.jpg".to_string());
let coverurl = bookdisplay.cover.clone().unwrap_or("http://localhost:8081/images/placeholder.jpg".to_string());
let handle_delete = move |_| {
spawn_local(async move {
let temp = delete_book(bookdelete.id).await.unwrap();
@@ -266,7 +370,7 @@ pub fn BookDB<G: Html>(cx: Scope, bookitem: BookUIProp) -> View<G> {
div(class="card"){
img(src=coverurl,width="100")
(format!("{:?}",book))
(format!("{:?}",bookdisplay))
button(class="delete", on:click=handle_delete){ "-" }
button(class="update", on:click=handle_update){ "=" }
button(class="info", on:click=handle_display){ "+" }
@@ -474,20 +578,54 @@ async fn SelectedUI<G: Html>(cx: Scope<'_>) -> View<G> {
let app_state = use_context::<AppState>(cx);
let displ_book = create_signal(cx, (*app_state.displayingbook.get()).clone());
let handle_update = move |_| {
let handle_close = move |_| {
app_state.displaying.set(false);
};
view! {cx,
p{
(if *app_state.displaying.get() == true {
p {
div(class="select-book"){
view!{ cx,
div(class="select-book"){
img(src=coverurl,width="100")
(format!("{:?}",book))
button(class="close", on:click=handle_close){ "CLOSE" }
button(class="close", on:click=handle_close){ "CLOSE" }
}
}
}
} else {
view!{cx,""}
})
}
}
}
#[component]
async fn PageBar<G: Html>(cx: Scope<'_>) -> View<G> {
let app_state = use_context::<AppState>(cx);
let handle_add = move |_| {
app_state.pagenum.set(*app_state.pagenum.get()+1);
};
let handle_sub = move |_| {
app_state.pagenum.set(*app_state.pagenum.get()-1);
};
view! {cx,
p {
(if *app_state.openlibrary.get() == true || *app_state.internalsearch.get() == true {
view!{ cx,
button(class="page", on:click=handle_sub){ "Page -" }
(format!("{:?}",app_state.pagenum.get()))
button(class="page", on:click=handle_add){ "Page +" }
}
}else {
view!{cx,""}
})
}
}
})
}
@@ -514,7 +652,8 @@ fn App<G: Html>(cx: Scope) -> View<G> {
SelectedUI{}
ListOL {}
ListDB{}
}
PageBar{}
}
}
}