mirror of
https://github.com/zaphar/kitchen.git
synced 2025-07-26 20:19:47 -04:00
This commit is contained in:
commit
4f3e58ba48
@ -135,12 +135,17 @@ impl IngredientAccumulator {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn accumulate_from(&mut self, r: &Recipe) {
|
||||
for i in r.steps.iter().map(|s| s.ingredients.iter()).flatten() {
|
||||
pub fn accumulate_ingredients<'a, I, S>(&'a mut self, recipe_title: S, ingredients: I)
|
||||
where
|
||||
I: Iterator<Item = &'a Ingredient>,
|
||||
S: Into<String>,
|
||||
{
|
||||
let recipe_title = recipe_title.into();
|
||||
for i in ingredients {
|
||||
let key = i.key();
|
||||
if !self.inner.contains_key(&key) {
|
||||
let mut set = BTreeSet::new();
|
||||
set.insert(r.title.clone());
|
||||
set.insert(recipe_title.clone());
|
||||
self.inner.insert(key, (i.clone(), set));
|
||||
} else {
|
||||
let amt = match (self.inner[&key].0.amt, i.amt) {
|
||||
@ -151,12 +156,19 @@ impl IngredientAccumulator {
|
||||
};
|
||||
self.inner.get_mut(&key).map(|(i, set)| {
|
||||
i.amt = amt;
|
||||
set.insert(r.title.clone());
|
||||
set.insert(recipe_title.clone());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn accumulate_from(&mut self, r: &Recipe) {
|
||||
self.accumulate_ingredients(
|
||||
&r.title,
|
||||
r.steps.iter().map(|s| s.ingredients.iter()).flatten(),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn ingredients(self) -> BTreeMap<IngredientKey, (Ingredient, BTreeSet<String>)> {
|
||||
self.inner
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ use std::{
|
||||
};
|
||||
|
||||
use client_api::UserData;
|
||||
use recipes::{parse, IngredientKey, Recipe, RecipeEntry};
|
||||
use recipes::{parse, Ingredient, IngredientKey, Recipe, RecipeEntry};
|
||||
use sycamore::futures::spawn_local_scoped;
|
||||
use sycamore::prelude::*;
|
||||
use sycamore_state::{Handler, MessageMapper};
|
||||
@ -30,7 +30,7 @@ use crate::api::{HttpStore, LocalStore};
|
||||
pub struct AppState {
|
||||
pub recipe_counts: BTreeMap<String, usize>,
|
||||
pub extras: Vec<(String, String)>,
|
||||
pub staples: Option<Recipe>,
|
||||
pub staples: BTreeSet<Ingredient>,
|
||||
pub recipes: BTreeMap<String, Recipe>,
|
||||
pub category_map: BTreeMap<String, String>,
|
||||
pub filtered_ingredients: BTreeSet<IngredientKey>,
|
||||
@ -43,7 +43,7 @@ impl AppState {
|
||||
Self {
|
||||
recipe_counts: BTreeMap::new(),
|
||||
extras: Vec::new(),
|
||||
staples: None,
|
||||
staples: BTreeSet::new(),
|
||||
recipes: BTreeMap::new(),
|
||||
category_map: BTreeMap::new(),
|
||||
filtered_ingredients: BTreeSet::new(),
|
||||
@ -160,9 +160,9 @@ impl StateMachine {
|
||||
let recipe_entries = &store.fetch_recipes().await?;
|
||||
let (staples, recipes) = filter_recipes(&recipe_entries)?;
|
||||
if let Some(recipes) = recipes {
|
||||
state.staples = staples;
|
||||
state.recipes = recipes;
|
||||
};
|
||||
state.staples = BTreeSet::new();
|
||||
if let Some(recipe_entries) = recipe_entries {
|
||||
local_store.set_all_recipes(recipe_entries);
|
||||
}
|
||||
|
@ -103,14 +103,12 @@ pub fn Categories<'ctx, G: Html>(cx: Scope<'ctx>, sh: StateHandler<'ctx>) -> Vie
|
||||
.insert(recipe_id.clone());
|
||||
}
|
||||
}
|
||||
if let Some(staples) = &state.staples {
|
||||
for (_, i) in staples.get_ingredients().iter() {
|
||||
let ingredient_name = i.name.clone();
|
||||
ingredients
|
||||
.entry(ingredient_name)
|
||||
.or_insert(BTreeSet::new())
|
||||
.insert("Staples".to_owned());
|
||||
}
|
||||
for i in state.staples.iter() {
|
||||
let ingredient_name = i.name.clone();
|
||||
ingredients
|
||||
.entry(ingredient_name)
|
||||
.or_insert(BTreeSet::new())
|
||||
.insert("Staples".to_owned());
|
||||
}
|
||||
ingredients
|
||||
});
|
||||
@ -124,10 +122,8 @@ pub fn Categories<'ctx, G: Html>(cx: Scope<'ctx>, sh: StateHandler<'ctx>) -> Vie
|
||||
ingredients.insert(i.name.clone());
|
||||
}
|
||||
}
|
||||
if let Some(staples) = &state.staples {
|
||||
for (_, i) in staples.get_ingredients().iter() {
|
||||
ingredients.insert(i.name.clone());
|
||||
}
|
||||
for i in state.staples.iter() {
|
||||
ingredients.insert(i.name.clone());
|
||||
}
|
||||
let mut mapping_list = Vec::new();
|
||||
for i in ingredients.iter() {
|
||||
|
@ -42,9 +42,7 @@ fn make_ingredients_rows<'ctx, G: Html>(
|
||||
}
|
||||
}
|
||||
if *show_staples.get() {
|
||||
if let Some(staples) = &state.staples {
|
||||
acc.accumulate_from(staples);
|
||||
}
|
||||
acc.accumulate_ingredients("Staples", state.staples.iter());
|
||||
}
|
||||
let mut ingredients = acc
|
||||
.ingredients()
|
||||
|
Loading…
x
Reference in New Issue
Block a user