diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2022-11-01 15:13:09 +0100 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2022-11-01 15:14:59 +0100 |
commit | cb013e2d89227d141fddeec074d755678ec39a18 (patch) | |
tree | 5fd384061b1a9f15d68a7fef68c002f44029a72c | |
parent | Frontend: Handle and show API errors (diff) | |
download | OSSE-cb013e2d89227d141fddeec074d755678ec39a18.tar.gz OSSE-cb013e2d89227d141fddeec074d755678ec39a18.tar.bz2 OSSE-cb013e2d89227d141fddeec074d755678ec39a18.zip |
Frontend: Reduce code duplication in the initial_search_query path of component creation
We now push a SearchSumbitted message if the initial_search_query is not none instead of duplicating the code present in the SearchSubmitted handler.
-rw-r--r-- | frontend/src/app.rs | 31 |
1 files changed, 3 insertions, 28 deletions
diff --git a/frontend/src/app.rs b/frontend/src/app.rs index 20e081c..03f466d 100644 --- a/frontend/src/app.rs +++ b/frontend/src/app.rs @@ -45,38 +45,13 @@ impl Component for OSSE { type Message = OSSEMessage; type Properties = OSSEProps; - //TODO: No code duplication for fetching in create() and update() fn create(ctx: &Context<Self>) -> Self { let mut search_query = String::from(""); - //we push an update message if inital_search_query is not none + //we push an SearchSubmitted message if inital_search_query is not none if let Some(initial_search_query) = ctx.props().initial_search_query.clone() { - search_query = initial_search_query.clone(); - - let api_endpoint = ctx.props().api_endpoint.clone(); - ctx.link().send_future(async move { - let endpoint = format!("{}/search/{}", api_endpoint, initial_search_query); - - let fetched_response = match Request::get(endpoint.as_str()).send().await { - Ok(response) => response, - Err(_) => { - return OSSEMessage::SearchFinished(Err( - "Failed to connect to the API!".to_string() - )) - } - }; - - let fetched_results: Vec<IndexedResource> = match fetched_response.json().await { - Err(_) => { - return OSSEMessage::SearchFinished(Err( - "Internal API Error!".to_string() - )) - } - Ok(json) => json, - }; - - OSSEMessage::SearchFinished(Ok(fetched_results)) - }); + search_query = initial_search_query; + ctx.link().send_message(OSSEMessage::SearchSubmitted); } //WE may have data race between the future and the actual creation. |