From 87c53ef747935fe47602d0792bf8f5202b3cc19b Mon Sep 17 00:00:00 2001 From: Baitinq Date: Fri, 28 Oct 2022 11:11:57 +0200 Subject: Frontend: Make the results state Optional We now return "No result!" if the user has actually searched for something and no results were found. --- frontend/src/main.rs | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/frontend/src/main.rs b/frontend/src/main.rs index 27c30da..605046d 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -47,28 +47,40 @@ impl Hash for CrawledResource { #[derive(Debug, Clone)] struct State { pub search_query: String, - pub results: Vec, //TODO: some loading? + pub results: Option>, //TODO: some loading? } #[function_component(OSSE)] fn osse() -> Html { let state = use_state(|| State { search_query: "".to_string(), - results: vec![], + results: None, }); - let display_results = |results: &Vec| -> Html { - results - .iter() - .sorted() - .map(|r| { - html! { -
- {r.url.to_owned()}{"--"}{r.priority} -
- } - }) - .collect::() + let display_results = |maybe_results: &Option>| -> Html { + let maybe_results = maybe_results.as_ref(); + if maybe_results.is_none() { + return html! {}; + } + + let results = maybe_results.unwrap(); + if !results.is_empty() { + results + .iter() + .sorted() + .map(|r| { + html! { + + } + }) + .collect::() + } else { + html! { +

{"No results!"}

+ } + } }; let search_query_changed = { @@ -107,7 +119,7 @@ fn osse() -> Html { Ok(json) => json, }; - state.results = fetched_json; + state.results = Some(fetched_json); cloned_state.set(state); }); -- cgit 1.4.1