mirror of
https://github.com/zaphar/kitchen.git
synced 2025-07-22 19:40:14 -04:00
Working shopping list creation
This commit is contained in:
parent
1a188f45aa
commit
c212a453e3
@ -32,11 +32,12 @@ fn recipe_check_box(props: RecipeCheckBoxProps) -> View<G> {
|
|||||||
let id_as_str = Rc::new(format!("{}", i));
|
let id_as_str = Rc::new(format!("{}", i));
|
||||||
let id_cloned = id_as_str.clone();
|
let id_cloned = id_as_str.clone();
|
||||||
let id_cloned_2 = id_as_str.clone();
|
let id_cloned_2 = id_as_str.clone();
|
||||||
|
let count = Signal::new(String::from("0"));
|
||||||
view! {
|
view! {
|
||||||
input(type="checkbox", name="recipe_id", value=id_as_str.clone(), on:click=move |_| {
|
input(type="number", min="0", bind:value=count.clone(), name="recipe_id", value=id_as_str.clone(), on:change=move |_| {
|
||||||
let mut app_service = app_service.clone();
|
let mut app_service = app_service.clone();
|
||||||
console_log!("clicked checkbox for id {}", id_cloned);
|
console_log!("setting recipe id: {} to count: {}", i, *count.get());
|
||||||
app_service.add_recipe_by_index(i);
|
app_service.set_recipe_count_by_index(i, count.get().parse().unwrap());
|
||||||
})
|
})
|
||||||
label(for=id_cloned_2) { (props.title) }
|
label(for=id_cloned_2) { (props.title) }
|
||||||
}
|
}
|
||||||
@ -67,11 +68,8 @@ pub fn recipe_selector() -> View<G> {
|
|||||||
fn shopping_list() -> View<G> {
|
fn shopping_list() -> View<G> {
|
||||||
let app_service = use_context::<AppService>();
|
let app_service = use_context::<AppService>();
|
||||||
let ingredients = create_memo(move || {
|
let ingredients = create_memo(move || {
|
||||||
let mut acc = IngredientAccumulator::new();
|
let ingredients = app_service.get_menu_list();
|
||||||
for r in app_service.get_menu_list().get().iter() {
|
ingredients
|
||||||
acc.accumulate_from(r);
|
|
||||||
}
|
|
||||||
acc.ingredients()
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(k, v)| (k.clone(), v.clone()))
|
.map(|(k, v)| (k.clone(), v.clone()))
|
||||||
.collect::<Vec<(IngredientKey, Ingredient)>>()
|
.collect::<Vec<(IngredientKey, Ingredient)>>()
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// 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 std::collections::BTreeMap;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::{console_debug, console_error};
|
use crate::{console_debug, console_error};
|
||||||
@ -18,20 +19,20 @@ use crate::{console_debug, console_error};
|
|||||||
use reqwasm::http;
|
use reqwasm::http;
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
|
|
||||||
use recipes::{parse, Recipe};
|
use recipes::{parse, Ingredient, IngredientAccumulator, IngredientKey, Recipe};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AppService {
|
pub struct AppService {
|
||||||
// TODO(jwall): Should each Recipe also be a Signal?
|
// TODO(jwall): Should each Recipe also be a Signal?
|
||||||
recipes: Signal<Vec<(usize, Recipe)>>,
|
recipes: Signal<Vec<(usize, Recipe)>>,
|
||||||
menu_list: Signal<Vec<Recipe>>,
|
menu_list: Signal<BTreeMap<usize, usize>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppService {
|
impl AppService {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
recipes: Signal::new(Vec::new()),
|
recipes: Signal::new(Vec::new()),
|
||||||
menu_list: Signal::new(Vec::new()),
|
menu_list: Signal::new(BTreeMap::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,13 +65,24 @@ impl AppService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_menu_list(&self) -> Signal<Vec<Recipe>> {
|
pub fn get_recipe_by_index(&self, idx: usize) -> Option<Recipe> {
|
||||||
self.menu_list.clone()
|
self.recipes.get().get(idx).map(|(_, r)| r.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_recipe_by_index(&mut self, i: usize) {
|
pub fn get_menu_list(&self) -> BTreeMap<IngredientKey, Ingredient> {
|
||||||
|
let mut acc = IngredientAccumulator::new();
|
||||||
|
let recipe_counts = self.menu_list.get();
|
||||||
|
for (idx, count) in recipe_counts.iter() {
|
||||||
|
for _ in 0..*count {
|
||||||
|
acc.accumulate_from(&self.get_recipe_by_index(*idx).unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
acc.ingredients()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_recipe_count_by_index(&mut self, i: usize, count: usize) {
|
||||||
let mut v = (*self.menu_list.get()).clone();
|
let mut v = (*self.menu_list.get()).clone();
|
||||||
v.push(self.recipes.get()[i].1.clone());
|
v.insert(i, count);
|
||||||
self.menu_list.set(v);
|
self.menu_list.set(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user