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