fix: Issue when loading user state from local storage

This commit is contained in:
Jeremy Wall 2023-09-23 13:44:16 -04:00
parent 4cefe42072
commit 0b7f513f27
3 changed files with 18 additions and 4 deletions

View File

@ -25,7 +25,7 @@ use recipes::{IngredientKey, RecipeEntry};
use wasm_bindgen::JsValue; use wasm_bindgen::JsValue;
use web_sys::Storage; use web_sys::Storage;
use crate::{app_state::AppState, js_lib}; use crate::{app_state::{AppState, parse_recipes}, js_lib};
#[derive(Debug)] #[derive(Debug)]
pub struct Error(String); pub struct Error(String);
@ -100,8 +100,21 @@ impl LocalStore {
} }
pub fn fetch_app_state(&self) -> Option<AppState> { pub fn fetch_app_state(&self) -> Option<AppState> {
debug!("Loading state from local store");
self.store.get("app_state").map_or(None, |val| { 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
})
}) })
} }

View File

@ -142,7 +142,7 @@ pub struct StateMachine {
} }
#[instrument] #[instrument]
fn parse_recipes( pub fn parse_recipes(
recipe_entries: &Option<Vec<RecipeEntry>>, recipe_entries: &Option<Vec<RecipeEntry>>,
) -> Result<Option<BTreeMap<String, Recipe>>, String> { ) -> Result<Option<BTreeMap<String, Recipe>>, String> {
match recipe_entries { match recipe_entries {

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use sycamore::{futures::spawn_local_scoped, prelude::*}; use sycamore::{futures::spawn_local_scoped, prelude::*};
use tracing::{info, instrument}; use tracing::{info, debug, instrument};
use crate::app_state::Message; use crate::app_state::Message;
use crate::{api, routing::Handler as RouteHandler}; use crate::{api, routing::Handler as RouteHandler};
@ -29,6 +29,7 @@ pub fn UI<G: Html>(cx: Scope) -> View<G> {
} else { } else {
crate::app_state::AppState::new() 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 sh = crate::app_state::get_state_handler(cx, app_state, store);
let view = create_signal(cx, View::empty()); let view = create_signal(cx, View::empty());
spawn_local_scoped(cx, { spawn_local_scoped(cx, {