From 850744e2049d5b48d474e35717974f420cc4ed7d Mon Sep 17 00:00:00 2001
From: Baitinq
Date: Sun, 6 Nov 2022 23:45:08 +0100
Subject: Frontend: Display a loading animation when results have not been
loaded
We now have a way to represent unloaded results in the SearchResult
struct. We simply wrap an Option over the results, meaning that None
signifies that the results have not yet been fetched.
---
frontend/src/app.rs | 34 ++++++++++++++++++++++++++--------
1 file changed, 26 insertions(+), 8 deletions(-)
diff --git a/frontend/src/app.rs b/frontend/src/app.rs
index 4e6e62d..d3edfa5 100644
--- a/frontend/src/app.rs
+++ b/frontend/src/app.rs
@@ -54,7 +54,7 @@ fn result_component(props: &ResultComponentProps) -> Html {
{match props.result.title.clone() {
None => "No Title".to_string(),
Some(title) => {
- truncate(&title, 70).to_string()
+ truncate(&title, 70).to_string()//TODO: add ... if truncate
},
}}
@@ -62,7 +62,7 @@ fn result_component(props: &ResultComponentProps) -> Html {
{match props.result.description.clone() {
None => "No Description.".to_string(),
Some(description) => {
- truncate(&description, 200).to_string()
+ truncate(&description, 200).to_string() //TODO: add ... if truncate
},
}}{format!("PRIO: {}", props.result.priority)}
@@ -72,12 +72,12 @@ fn result_component(props: &ResultComponentProps) -> Html {
pub struct SearchResult {
query: String,
- results: Result, String>,
+ results: Option, String>>, //none signifies not finished loading
}
pub struct OSSE {
pub current_search_query: String,
- pub results: Option, //TODO: some loading?
+ pub results: Option, //none signifies no query yet
}
#[derive(Properties, PartialEq, Eq)]
@@ -126,8 +126,9 @@ impl Component for OSSE {
query: urlencoding::encode(search_query.as_str()).to_string(),
});
+ let search_query_clone = search_query.clone();
ctx.link().send_future(async move {
- let endpoint = format!("{}/search/{}", api_endpoint, search_query);
+ let endpoint = format!("{}/search/{}", &api_endpoint, &search_query_clone);
let fetched_response = match Request::get(endpoint.as_str()).send().await {
Ok(response) => response,
@@ -151,7 +152,12 @@ impl Component for OSSE {
OSSEMessage::SearchFinished(Ok(fetched_results))
});
- false
+ self.results = Some(SearchResult {
+ query: search_query,
+ results: None, //none yet
+ });
+
+ true
}
OSSEMessage::SearchChanged(search_query) => {
self.current_search_query = search_query;
@@ -163,13 +169,13 @@ impl Component for OSSE {
Ok(results) => {
self.results = Some(SearchResult {
query: search_query,
- results: Ok(results),
+ results: Some(Ok(results)),
});
}
Err(error) => {
self.results = Some(SearchResult {
query: search_query,
- results: Err(error),
+ results: Some(Err(error)),
});
}
};
@@ -196,6 +202,7 @@ impl Component for OSSE {
});
let display_results = |maybe_results: &Option| -> Html {
+ //not yet searched
if maybe_results.is_none() {
return html! {};
}
@@ -204,6 +211,17 @@ impl Component for OSSE {
let search_query = &result.query;
let results = &result.results;
+ //not yet loaded results
+ if results.is_none() {
+ return html! {
+
+ {"Loading..."}
+
+ };
+ }
+
+ let results = results.as_ref().unwrap();
+
if results.is_err() {
return html! {
{format!("ERROR: {}", results.as_ref().err().unwrap())}
--
cgit 1.4.1