diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2022-10-27 02:56:38 +0200 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2022-10-27 16:02:17 +0200 |
commit | e82a426123584362b3043765d4f8d8cc49a002c1 (patch) | |
tree | 3629f9aab9bb41c340bd68d24d2179cf38106160 | |
parent | Crawler: Abstract database word fetching with search_word_in_db() (diff) | |
download | OSSE-e82a426123584362b3043765d4f8d8cc49a002c1.tar.gz OSSE-e82a426123584362b3043765d4f8d8cc49a002c1.tar.bz2 OSSE-e82a426123584362b3043765d4f8d8cc49a002c1.zip |
Frontend: Fetch results from indexer
-rw-r--r-- | Cargo.lock | 3 | ||||
-rw-r--r-- | frontend/Cargo.toml | 5 | ||||
-rw-r--r-- | frontend/src/main.rs | 78 |
3 files changed, 53 insertions, 33 deletions
diff --git a/Cargo.lock b/Cargo.lock index 63e25f0..6f48c0b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -617,7 +617,10 @@ name = "frontend" version = "0.1.0" dependencies = [ "gloo 0.8.0", + "gloo-net", + "serde", "wasm-bindgen", + "wasm-bindgen-futures", "web-sys", "yew", ] diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index b6de6ad..c1be331 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -9,4 +9,7 @@ edition = "2021" yew = "0.19" gloo = "0.8.0" web-sys = "0.3.60" -wasm-bindgen = "0.2.83" \ No newline at end of file +wasm-bindgen = "0.2.83" +gloo-net = "0.2" +serde = { version = "1.0", features = ["derive", "rc"] } +wasm-bindgen-futures = "0.4" \ No newline at end of file diff --git a/frontend/src/main.rs b/frontend/src/main.rs index e09b259..26fb7e4 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -1,4 +1,6 @@ use gloo::console::log; +use gloo_net::http::Request; +use serde::Deserialize; use std::hash::{Hash, Hasher}; use std::{ops::Deref, sync::Arc}; use wasm_bindgen::*; @@ -6,7 +8,7 @@ use web_sys::{EventTarget, HtmlInputElement}; use yew::prelude::*; //TODO: we should import this from the indexer -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Deserialize)] struct CrawledResource { url: String, priority: u32, //how do we even calculate this @@ -40,38 +42,50 @@ fn osse() -> Html { results: vec![], }); - let cloned_state = state.clone(); - let search_query_changed = Callback::from(move |event: InputEvent| { - let target: EventTarget = event - .target() - .expect("Event should have a target when dispatched"); - let input = target.unchecked_into::<HtmlInputElement>().value(); - log!("Input changed: {}", &input); - let mut state = cloned_state.deref().clone(); - state.search_query = input; - cloned_state.set(state); - }); + let search_query_changed = { + let cloned_state = state.clone(); + Callback::from(move |event: InputEvent| { + let target: EventTarget = event + .target() + .expect("Event should have a target when dispatched"); + let input = target.unchecked_into::<HtmlInputElement>().value(); + log!("Input changed: {}", &input); + let mut state = cloned_state.deref().clone(); + state.search_query = input; + cloned_state.set(state); + }) + }; - let cloned_state = state.clone(); - let on_submit = Callback::from(move |event: FocusEvent| { - event.prevent_default(); - let mut state = cloned_state.deref().clone(); - log!("Submit:{}", state.search_query); - state.search_query = "".to_string(); - state.results = vec![ - CrawledResource { - url: "http://example.com".to_string(), - priority: 12, - word: Arc::new("example".to_string()), - }, - CrawledResource { - url: "http://test.com".to_string(), - priority: 17, - word: Arc::new("test".to_string()), - }, - ]; - cloned_state.set(state); - }); + let on_submit = { + let cloned_state = state.clone(); + + Callback::from(move |event: FocusEvent| { + event.prevent_default(); + let state = cloned_state.deref().clone(); + + //fetch + { + let cloned_state = cloned_state.clone(); + wasm_bindgen_futures::spawn_local(async move { + let mut state = cloned_state.deref().clone(); + let endpoint = format!("http://127.0.0.1:4444/search/{}", &state.search_query); + + let fetched_results = Request::get(endpoint.as_str()).send().await.unwrap(); + + let fetched_json: Vec<CrawledResource> = match fetched_results.json().await { + Err(e) => panic!("Im panic: {}", e), + Ok(json) => json, + }; + + state.results = fetched_json; + + cloned_state.set(state); + }); + } + + log!("Submit: {}", state.search_query); + }) + }; let curr_state = state.deref().to_owned(); |