Skip to content

Commit

Permalink
feat: introduce AppState
Browse files Browse the repository at this point in the history
A simple shared state for recipes so that they can be displayed and
updated in different places. This may need to be split into further
structs but there is no point in doing so yet.
  • Loading branch information
justinrubek committed Sep 24, 2023
1 parent ba4a63e commit a2d0d3d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
15 changes: 14 additions & 1 deletion crates/ui/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::routing::Route;
use crate::{routing::Route, state::AppState};
use annapurna_data::types::{Ingredient, Recipe};
use dioxus::prelude::*;
use dioxus_router::components::Router;
use wasm_bindgen::prelude::*;

mod components;
mod routing;
mod state;
pub mod util;

#[wasm_bindgen(start)]
Expand All @@ -18,6 +20,17 @@ pub fn launch_app() {
}

fn app(cx: Scope) -> Element {
let recipes = vec![Recipe::new(
"pizza dough".to_string(),
vec![
Ingredient::new("flour".to_string()),
Ingredient::new("water".to_string()),
Ingredient::new("salt".to_string()),
Ingredient::new("yeast".to_string()),
],
)];
use_shared_state_provider(cx, || AppState { recipes });

render! {
Router::<Route> { }
}
Expand Down
29 changes: 26 additions & 3 deletions crates/ui/src/routing.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use crate::{components::Recipe, util};
use crate::{components::Recipe, state::AppState, util};
use dioxus::prelude::*;
use dioxus_router::prelude::*;

#[derive(Debug, Clone, Routable)]
pub(crate) enum Route {
#[route("/")]
Index {},
/*
#[route("/app")]
AppIndex {},
#[route("/app/recipes")]
AppRecipes {},
*/
}

#[allow(non_snake_case)]
pub(crate) fn Index(cx: Scope) -> Element {
cx.render(rsx! {
div { "hello, wasm!" }
Link {
to: Route::AppRecipes {},
"recipes"
}
button {
onclick: |_| async move {
let filename = "test.txt";
Expand All @@ -32,3 +34,24 @@ pub(crate) fn Index(cx: Scope) -> Element {
}
})
}

#[allow(non_snake_case)]
pub(crate) fn AppIndex(cx: Scope) -> Element {
cx.render(rsx! {
div { "app index" }
})
}

#[allow(non_snake_case)]
pub(crate) fn AppRecipes(cx: Scope) -> Element {
let app_state = use_shared_state::<AppState>(cx).unwrap();

cx.render(rsx! {
app_state.read().recipes.iter().map(|recipe| rsx! {
Recipe {
name: recipe.name.clone(),
ingredients: recipe.ingredients.iter().map(|ingredient| ingredient.name.clone()).collect(),
}
})
})
}
7 changes: 7 additions & 0 deletions crates/ui/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use annapurna_data::types::Recipe;
use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub(crate) struct AppState {
pub recipes: Vec<Recipe>,
}

0 comments on commit a2d0d3d

Please sign in to comment.