diff --git a/web/src/api.rs b/web/src/api.rs index 48bf81e..a4a90db 100644 --- a/web/src/api.rs +++ b/web/src/api.rs @@ -25,7 +25,7 @@ use recipes::{IngredientKey, RecipeEntry}; use wasm_bindgen::JsValue; use web_sys::Storage; -use crate::{app_state::AppState, js_lib}; +use crate::{app_state::{AppState, parse_recipes}, js_lib}; #[derive(Debug)] pub struct Error(String); @@ -100,8 +100,21 @@ impl LocalStore { } pub fn fetch_app_state(&self) -> Option { + debug!("Loading state from local store"); self.store.get("app_state").map_or(None, |val| { - val.map(|s| from_str(&s).expect("Failed to deserialize app state")) + val.map(|s| { + debug!("Found an app_state object"); + let mut app_state: AppState = from_str(&s).expect("Failed to deserialize app state"); + let recipes = parse_recipes(&self.get_recipes()).expect("Failed to parse recipes"); + if let Some(recipes) = recipes { + debug!("Populating recipes"); + for (id, recipe) in recipes { + debug!(id, "Adding recipe from local storage"); + app_state.recipes.insert(id, recipe); + } + } + app_state + }) }) } diff --git a/web/src/app_state.rs b/web/src/app_state.rs index bb5212c..09385b3 100644 --- a/web/src/app_state.rs +++ b/web/src/app_state.rs @@ -142,7 +142,7 @@ pub struct StateMachine { } #[instrument] -fn parse_recipes( +pub fn parse_recipes( recipe_entries: &Option>, ) -> Result>, String> { match recipe_entries { diff --git a/web/src/web.rs b/web/src/web.rs index 90ac17b..c2b6883 100644 --- a/web/src/web.rs +++ b/web/src/web.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. use sycamore::{futures::spawn_local_scoped, prelude::*}; -use tracing::{info, instrument}; +use tracing::{info, debug, instrument}; use crate::app_state::Message; use crate::{api, routing::Handler as RouteHandler}; @@ -29,6 +29,7 @@ pub fn UI(cx: Scope) -> View { } else { crate::app_state::AppState::new() }; + debug!(?app_state, "Loaded app state from local storage"); let sh = crate::app_state::get_state_handler(cx, app_state, store); let view = create_signal(cx, View::empty()); spawn_local_scoped(cx, {