Cleanup Unused code after state management refactors

This commit is contained in:
Jeremy Wall 2022-12-28 14:27:45 -06:00
parent 8bcafc385d
commit f3425dedeb
6 changed files with 2 additions and 195 deletions

View File

@ -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);

View File

@ -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};

View File

@ -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<G: Html>(cx: Scope) -> View<G> {
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" }
}
}

View File

@ -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<G: Html>(cx: Scope, steps: Vec<recipes::Step>) -> View<G> {
#[component]
pub fn Viewer<'ctx, G: Html>(cx: Scope<'ctx>, props: RecipeComponentProps<'ctx>) -> View<G> {
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) {

View File

@ -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<G> {
view! {cx,
ManagePage(
selected=Some("New Recipe".to_owned()),
) { AddRecipe() }
}
}

View File

@ -31,7 +31,6 @@ pub fn UI<G: Html>(cx: Scope) -> View<G> {
// 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