diff --git a/web/src/api.rs b/web/src/api.rs index 3048e3f..2ee8f17 100644 --- a/web/src/api.rs +++ b/web/src/api.rs @@ -57,79 +57,6 @@ fn filter_recipes( } } -#[instrument(skip(state))] -pub async fn init_page_state(store: &HttpStore, state: &app_state::State) -> Result<(), String> { - info!("Synchronizing Recipes"); - // TODO(jwall): Make our caching logic using storage more robust. - let recipes = store.get_recipes().await.map_err(|e| format!("{:?}", e))?; - if let Ok((staples, recipes)) = filter_recipes(&recipes) { - state.staples.set(staples); - if let Some(recipes) = recipes { - state.recipes.set(recipes); - } - } - - if let Ok(Some(plan)) = store.get_plan().await { - // set the counts. - for (id, count) in plan { - state.set_recipe_count_by_index(&id, count as usize); - } - } else { - // Initialize things to zero - if let Some(rs) = recipes { - for r in rs { - if !state.recipe_counts.get().contains_key(r.recipe_id()) { - state.set_recipe_count_by_index(&r.recipe_id().to_owned(), 0); - } - } - } - } - info!("Checking for user_data in local storage"); - let storage = js_lib::get_storage(); - let user_data = storage - .get("user_data") - .expect("Couldn't read from storage"); - if let Some(data) = user_data { - if let Ok(user_data) = from_str(&data) { - state.auth.set(user_data) - } - } - info!("Synchronizing categories"); - match store.get_categories().await { - Ok(Some(categories_content)) => { - debug!(categories=?categories_content); - let category_map = recipes::parse::as_categories(&categories_content)?; - state.category_map.set(category_map); - } - Ok(None) => { - warn!("There is no category file"); - } - Err(e) => { - error!("{:?}", e); - } - } - info!("Synchronizing inventory data"); - match store.get_inventory_data().await { - Ok((filtered_ingredients, modified_amts, mut extra_items)) => { - state.reset_modified_amts(modified_amts); - state.filtered_ingredients.set(filtered_ingredients); - state.extras.set( - extra_items - .drain(0..) - .enumerate() - .map(|(idx, (amt, name))| { - (idx, (create_rc_signal(amt.clone()), create_rc_signal(name))) - }) - .collect(), - ) - } - Err(e) => { - error!("{:?}", e); - } - } - Ok(()) -} - #[derive(Debug)] pub struct Error(String); diff --git a/web/src/app_state.rs b/web/src/app_state.rs index 4f31053..d91399b 100644 --- a/web/src/app_state.rs +++ b/web/src/app_state.rs @@ -17,7 +17,7 @@ use client_api::UserData; use recipes::{parse, Ingredient, IngredientAccumulator, IngredientKey, Recipe, RecipeEntry}; use serde_json::from_str; use sycamore::futures::spawn_local_scoped; -use sycamore::{futures::spawn_local, prelude::*}; +use sycamore::prelude::*; use sycamore_state::{Handler, MessageMapper}; use tracing::{debug, error, info, instrument, warn}; diff --git a/web/src/components/add_recipe.rs b/web/src/components/add_recipe.rs deleted file mode 100644 index fc49fb7..0000000 --- a/web/src/components/add_recipe.rs +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright 2022 Jeremy Wall (Jeremy@marzhilsltudios.com) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -use sycamore::{futures::spawn_local_scoped, prelude::*}; -use tracing::{error, info}; - -use recipes::RecipeEntry; - -const STARTER_RECIPE: &'static str = "title: TITLE_PLACEHOLDER - -Description here. - -step: - -1 ingredient - -Instructions here -"; - -#[component] -pub fn AddRecipe(cx: Scope) -> View { - let recipe_title = create_signal(cx, String::new()); - let create_recipe_signal = create_signal(cx, ()); - let dirty = create_signal(cx, false); - - let entry = create_memo(cx, || { - RecipeEntry( - recipe_title - .get() - .as_ref() - .to_lowercase() - .replace(" ", "_") - .replace("\n", ""), - STARTER_RECIPE - .replace("TITLE_PLACEHOLDER", recipe_title.get().as_str()) - .replace("\r", ""), - ) - }); - - create_effect(cx, move || { - create_recipe_signal.track(); - if !*dirty.get_untracked() { - return; - } - spawn_local_scoped(cx, { - let store = crate::api::HttpStore::get_from_context(cx); - async move { - let entry = entry.get_untracked(); - // TODO(jwall): Better error reporting here. - match store.get_recipe_text(entry.recipe_id()).await { - Ok(Some(_)) => { - // TODO(jwall): We should tell the user that this id already exists - info!(recipe_id = entry.recipe_id(), "Recipe already exists"); - return; - } - Ok(None) => { - // noop - } - Err(err) => { - // TODO(jwall): We should tell the user that this is failing - error!(?err) - } - } - store - .save_recipes(vec![entry.as_ref().clone()]) - .await - .expect("Unable to save New Recipe"); - crate::js_lib::navigate_to_path(&format!("/ui/recipe/{}", entry.recipe_id())) - .expect("Unable to navigate to recipe"); - } - }); - }); - view! {cx, - label(for="recipe_title") { "Recipe Title" } - input(bind:value=recipe_title, type="text", name="recipe_title", id="recipe_title", on:change=move |_| { - dirty.set(true); - }) - button(on:click=move |_| { - create_recipe_signal.trigger_subscribers(); - }) { "Create" } - } -} diff --git a/web/src/components/recipe.rs b/web/src/components/recipe.rs index eefc862..cb1c951 100644 --- a/web/src/components/recipe.rs +++ b/web/src/components/recipe.rs @@ -14,7 +14,7 @@ use sycamore::{futures::spawn_local_scoped, prelude::*}; use tracing::{debug, error}; -use crate::app_state::{self, Message, StateHandler}; +use crate::app_state::{Message, StateHandler}; use recipes::{self, RecipeEntry}; fn check_recipe_parses( @@ -162,7 +162,6 @@ fn Steps(cx: Scope, steps: Vec) -> View { #[component] pub fn Viewer<'ctx, G: Html>(cx: Scope<'ctx>, props: RecipeComponentProps<'ctx>) -> View { let RecipeComponentProps { recipe_id, sh } = props; - let state = app_state::State::get_from_context(cx); let view = create_signal(cx, View::empty()); let recipe_signal = sh.get_selector(cx, move |state| { if let Some(recipe) = state.get().recipes.get(&recipe_id) { diff --git a/web/src/pages/manage/add_recipe.rs b/web/src/pages/manage/add_recipe.rs deleted file mode 100644 index 68830b7..0000000 --- a/web/src/pages/manage/add_recipe.rs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2022 Jeremy Wall (Jeremy@marzhilsltudios.com) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -use super::ManagePage; -use crate::{app_state::StateHandler, components::add_recipe::AddRecipe}; - -use sycamore::prelude::*; - -#[component] -pub fn AddRecipePage<'ctx, G: Html>(cx: Scope<'ctx>, sh: StateHandler<'ctx>) -> View { - view! {cx, - ManagePage( - selected=Some("New Recipe".to_owned()), - ) { AddRecipe() } - } -} diff --git a/web/src/web.rs b/web/src/web.rs index 733c1cb..a50ea74 100644 --- a/web/src/web.rs +++ b/web/src/web.rs @@ -31,7 +31,6 @@ pub fn UI(cx: Scope) -> View { // FIXME(jwall): We need a way to trigger refreshes when required. Turn this // into a create_effect with a refresh signal stored as a context. spawn_local_scoped(cx, { - let store = api::HttpStore::get_from_context(cx); async move { sh.dispatch(cx, Message::LoadState); // TODO(jwall): This needs to be moved into the RouteHandler