From b4626f9c6d52163306a46c4f1678ec5c2aa23b38 Mon Sep 17 00:00:00 2001 From: Baitinq Date: Sun, 6 Nov 2022 19:54:23 +0100 Subject: Frontend: Store custom SearchResult struct in the app state This allows us to store both the original query and the original results in the struct, being able to reference them while displaying. This fixes the search query below the search bar being updated with it. --- frontend/src/app.rs | 102 +++++++++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 45 deletions(-) diff --git a/frontend/src/app.rs b/frontend/src/app.rs index fb86e8a..02f46a3 100644 --- a/frontend/src/app.rs +++ b/frontend/src/app.rs @@ -56,9 +56,14 @@ fn result_component(props: &ResultComponentProps) -> Html { } } +pub struct SearchResult { + query: String, + results: Result, String>, +} + pub struct OSSE { - pub search_query: String, - pub results: Option, String>>, //TODO: some loading? + pub current_search_query: String, + pub results: Option, //TODO: some loading? } #[derive(Properties, PartialEq, Eq)] @@ -88,7 +93,7 @@ impl Component for OSSE { //WE may have data race between the future and the actual creation. OSSE { - search_query: urlencoding::decode(search_query.as_str()) + current_search_query: urlencoding::decode(search_query.as_str()) .to_owned() .unwrap() .to_string(), @@ -97,10 +102,10 @@ impl Component for OSSE { } fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { + let search_query = self.current_search_query.clone(); match msg { OSSEMessage::SearchSubmitted => { let api_endpoint = ctx.props().api_endpoint.clone(); - let search_query = self.search_query.clone(); let navigator = ctx.link().navigator().unwrap(); navigator.push(&Route::OSSESearch { @@ -135,17 +140,23 @@ impl Component for OSSE { false } OSSEMessage::SearchChanged(search_query) => { - self.search_query = search_query; + self.current_search_query = search_query; true } OSSEMessage::SearchFinished(search_results) => { match search_results { Ok(results) => { - self.results = Some(Ok(results)); + self.results = Some(SearchResult { + query: search_query, + results: Ok(results), + }); } Err(error) => { - self.results = Some(Err(error)); + self.results = Some(SearchResult { + query: search_query, + results: Err(error), + }); } }; @@ -170,49 +181,50 @@ impl Component for OSSE { OSSEMessage::SearchChanged(input) }); - let display_results = - |maybe_results: &Option, String>>| -> Html { - if maybe_results.is_none() { - return html! {}; - } + let display_results = |maybe_results: &Option| -> Html { + if maybe_results.is_none() { + return html! {}; + } - let results = maybe_results.as_ref().unwrap(); + let result = maybe_results.as_ref().unwrap(); + let search_query = &result.query; + let results = &result.results; - if results.is_err() { - return html! { -

{format!("ERROR: {}", results.as_ref().err().unwrap())}

- }; - } + if results.is_err() { + return html! { +

{format!("ERROR: {}", results.as_ref().err().unwrap())}

+ }; + } - let results = results.as_ref().unwrap(); + let results = results.as_ref().unwrap(); - if results.is_empty() { - return html! { -

{"No results!"}

- }; - } + if results.is_empty() { + return html! { +

{"No results!"}

+ }; + } - html! { - <> - //Problem with margin: When no results or early return then mb not applied -
- {format!("{} Results for \"{}\"", results.len(), self.search_query)} -
+ html! { + <> + //Problem with margin: When no results or early return then mb not applied +
+ {format!("{} Results for \"{}\"", results.len(), search_query)} +
- {results - .iter() - .sorted() - .map(|r| { - html! { -
- -
- } - }) - .collect::()} - - } - }; + {results + .iter() + .sorted() + .map(|r| { + html! { +
+ +
+ } + }) + .collect::()} + + } + }; html! { <> @@ -239,7 +251,7 @@ impl Component for OSSE {

{"Your favorite independent search engine."}

- +
-- cgit 1.4.1