2022-03-15 18:01:25 -04:00
|
|
|
// Copyright 2022 Jeremy Wall
|
|
|
|
//
|
|
|
|
// 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.
|
2022-08-15 19:37:08 -04:00
|
|
|
use recipes::Recipe;
|
2022-09-25 17:04:46 -04:00
|
|
|
use sycamore::{futures::spawn_local_scoped, prelude::*};
|
2022-07-18 18:50:11 -04:00
|
|
|
use tracing::{error, instrument};
|
2022-03-15 18:01:25 -04:00
|
|
|
|
2022-08-12 21:41:19 -04:00
|
|
|
use crate::components::recipe_selection::*;
|
2022-10-21 14:57:50 -04:00
|
|
|
use crate::{api::*, app_state};
|
2022-08-12 21:41:19 -04:00
|
|
|
|
2022-10-10 20:49:18 -04:00
|
|
|
#[allow(non_snake_case)]
|
2022-07-18 18:50:11 -04:00
|
|
|
#[instrument]
|
2022-09-25 17:04:46 -04:00
|
|
|
pub fn RecipeSelector<G: Html>(cx: Scope) -> View<G> {
|
|
|
|
let rows = create_memo(cx, move || {
|
2022-10-21 14:57:50 -04:00
|
|
|
let state = app_state::State::get_from_context(cx);
|
2022-03-15 18:01:25 -04:00
|
|
|
let mut rows = Vec::new();
|
2022-10-21 14:57:50 -04:00
|
|
|
for row in state
|
|
|
|
.recipes
|
|
|
|
.get()
|
|
|
|
.as_ref()
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| create_signal(cx, (k.clone(), v.clone())))
|
|
|
|
.collect::<Vec<&Signal<(String, Recipe)>>>()
|
|
|
|
.chunks(4)
|
2022-10-12 08:45:31 -04:00
|
|
|
{
|
2022-10-21 14:57:50 -04:00
|
|
|
rows.push(create_signal(cx, Vec::from(row)));
|
2022-03-15 18:01:25 -04:00
|
|
|
}
|
|
|
|
rows
|
2022-09-25 17:04:46 -04:00
|
|
|
});
|
|
|
|
let clicked = create_signal(cx, false);
|
|
|
|
create_effect(cx, move || {
|
|
|
|
clicked.track();
|
2022-10-21 14:57:50 -04:00
|
|
|
let store = HttpStore::get_from_context(cx);
|
|
|
|
let state = app_state::State::get_from_context(cx);
|
2022-09-25 17:04:46 -04:00
|
|
|
spawn_local_scoped(cx, {
|
2022-03-15 18:01:25 -04:00
|
|
|
async move {
|
2022-10-21 14:57:50 -04:00
|
|
|
if let Err(err) = init_page_state(store.as_ref(), state.as_ref()).await {
|
2022-07-18 18:50:11 -04:00
|
|
|
error!(?err);
|
2022-03-15 18:01:25 -04:00
|
|
|
};
|
|
|
|
}
|
2022-09-25 17:04:46 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
view! {cx,
|
2022-08-24 18:32:43 -04:00
|
|
|
table(class="recipe_selector no-print") {
|
2022-03-15 18:01:25 -04:00
|
|
|
(View::new_fragment(
|
|
|
|
rows.get().iter().cloned().map(|r| {
|
2022-09-25 17:04:46 -04:00
|
|
|
view ! {cx,
|
|
|
|
tr { Keyed(
|
|
|
|
iterable=r,
|
|
|
|
view=|cx, sig| {
|
|
|
|
let title = create_memo(cx, move || sig.get().1.title.clone());
|
|
|
|
view! {cx,
|
|
|
|
td { RecipeSelection(i=sig.get().0.to_owned(), title=title) }
|
2022-03-15 18:01:25 -04:00
|
|
|
}
|
|
|
|
},
|
2022-09-25 17:04:46 -04:00
|
|
|
key=|sig| sig.get().0.to_owned(),
|
|
|
|
)}
|
2022-03-15 18:01:25 -04:00
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
))
|
|
|
|
}
|
2022-03-15 18:11:15 -04:00
|
|
|
input(type="button", value="Refresh Recipes", on:click=move |_| {
|
|
|
|
// Poor man's click event signaling.
|
|
|
|
let toggle = !*clicked.get();
|
|
|
|
clicked.set(toggle);
|
|
|
|
})
|
2022-03-15 18:01:25 -04:00
|
|
|
}
|
|
|
|
}
|