-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recipes are loaded through the API. This was implemented in the style suggested by the Dioxus documentation where shared state is written to from a "resolve" function.
- Loading branch information
1 parent
a2d0d3d
commit 38203e9
Showing
6 changed files
with
49 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use crate::state::AppState; | ||
use annapurna_data::types::Recipe; | ||
use dioxus::hooks::UseSharedState; | ||
|
||
pub const BASE_API_URL: &str = "/api"; | ||
pub const RECIPE_API_URL: &str = "/recipes"; | ||
|
||
/// Format a relative path to an absolute URL for the API. | ||
fn format_url(path: &str) -> String { | ||
// determine the absolute path to base of the current page from the browser | ||
let current_url = web_sys::window().unwrap().location().origin().unwrap(); | ||
|
||
let url = format!("{}{}{}", current_url, BASE_API_URL, path); | ||
url | ||
} | ||
|
||
/// Retrieves recipes from the API. | ||
pub async fn get_recipes() -> Result<Vec<Recipe>, reqwest::Error> { | ||
let url = format_url(RECIPE_API_URL); | ||
reqwest::get(&url).await?.json().await | ||
} | ||
|
||
/// Retrieves recipes from the API and updates the app state. | ||
pub async fn resolve_recipes(app_state: UseSharedState<AppState>) { | ||
let recipes = get_recipes().await.unwrap(); | ||
app_state.write().recipes = recipes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters