2022-02-02 16:29:27 -05: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-09-25 17:04:46 -04:00
|
|
|
use sycamore::{futures::spawn_local_scoped, prelude::*};
|
2022-09-08 20:17:46 -04:00
|
|
|
use tracing::{debug, error};
|
2022-09-05 17:49:00 -04:00
|
|
|
use web_sys::HtmlDialogElement;
|
|
|
|
|
2022-10-21 14:57:50 -04:00
|
|
|
use crate::{app_state, js_lib::get_element_by_id};
|
|
|
|
use recipes::{self, RecipeEntry};
|
2022-09-05 17:49:00 -04:00
|
|
|
|
|
|
|
fn get_error_dialog() -> HtmlDialogElement {
|
|
|
|
get_element_by_id::<HtmlDialogElement>("error-dialog")
|
|
|
|
.expect("error-dialog isn't an html dialog element!")
|
2022-10-11 17:18:54 -04:00
|
|
|
.expect("error-dialog element isn't present")
|
2022-09-05 17:49:00 -04:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
fn check_recipe_parses(text: &str, error_text: &Signal<String>) -> bool {
|
2022-09-05 17:49:00 -04:00
|
|
|
if let Err(e) = recipes::parse::as_recipe(text) {
|
|
|
|
error!(?e, "Error parsing recipe");
|
|
|
|
error_text.set(e);
|
|
|
|
let el = get_error_dialog();
|
|
|
|
el.show();
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
error_text.set(String::new());
|
|
|
|
let el = get_error_dialog();
|
|
|
|
el.close();
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
#[component]
|
2022-10-17 17:16:33 -04:00
|
|
|
fn Editor<G: Html>(cx: Scope, recipe: RecipeEntry) -> View<G> {
|
2022-09-25 17:04:46 -04:00
|
|
|
let id = create_signal(cx, recipe.recipe_id().to_owned());
|
|
|
|
let text = create_signal(cx, recipe.recipe_text().to_owned());
|
|
|
|
let error_text = create_signal(cx, String::new());
|
|
|
|
let save_signal = create_signal(cx, ());
|
2022-09-08 20:17:46 -04:00
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
create_effect(cx, move || {
|
|
|
|
// TODO(jwall): This is triggering on load which is not desired.
|
|
|
|
save_signal.track();
|
|
|
|
spawn_local_scoped(cx, {
|
2022-10-21 14:57:50 -04:00
|
|
|
let store = crate::api::HttpStore::get_from_context(cx);
|
2022-09-25 17:04:46 -04:00
|
|
|
async move {
|
2022-10-21 14:57:50 -04:00
|
|
|
if let Err(e) = store
|
2022-09-25 17:04:46 -04:00
|
|
|
.save_recipes(vec![RecipeEntry(
|
|
|
|
id.get_untracked().as_ref().clone(),
|
|
|
|
text.get_untracked().as_ref().clone(),
|
|
|
|
)])
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
error!(?e, "Failed to save recipe");
|
|
|
|
error_text.set(format!("{:?}", e));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-09-05 17:49:00 -04:00
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
let dialog_view = view! {cx,
|
2022-09-05 17:49:00 -04:00
|
|
|
dialog(id="error-dialog") {
|
|
|
|
article{
|
|
|
|
header {
|
|
|
|
a(href="#", on:click=|_| {
|
|
|
|
let el = get_error_dialog();
|
|
|
|
el.close();
|
|
|
|
}, class="close")
|
|
|
|
"Invalid Recipe"
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
(error_text.get().clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 17:04:46 -04:00
|
|
|
};
|
2022-08-12 21:41:19 -04:00
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
view! {cx,
|
2022-09-05 17:49:00 -04:00
|
|
|
(dialog_view)
|
2022-09-25 17:04:46 -04:00
|
|
|
textarea(bind:value=text, rows=20)
|
2022-10-17 17:16:33 -04:00
|
|
|
span(role="button", on:click=move |_| {
|
2022-09-05 17:49:00 -04:00
|
|
|
let unparsed = text.get();
|
|
|
|
check_recipe_parses(unparsed.as_str(), error_text.clone());
|
2022-09-25 17:04:46 -04:00
|
|
|
}) { "Check" } " "
|
2022-10-17 17:16:33 -04:00
|
|
|
span(role="button", on:click=move |_| {
|
2022-09-05 17:49:00 -04:00
|
|
|
let unparsed = text.get();
|
|
|
|
if check_recipe_parses(unparsed.as_str(), error_text.clone()) {
|
2022-09-08 20:17:46 -04:00
|
|
|
debug!("triggering a save");
|
|
|
|
save_signal.trigger_subscribers();
|
2022-09-05 17:49:00 -04:00
|
|
|
};
|
2022-09-25 17:04:46 -04:00
|
|
|
}) { "Save" }
|
|
|
|
}
|
2022-09-05 17:49:00 -04:00
|
|
|
}
|
2022-02-02 16:29:27 -05:00
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
#[component]
|
|
|
|
fn Steps<'ctx, G: Html>(cx: Scope<'ctx>, steps: &'ctx ReadSignal<Vec<recipes::Step>>) -> View<G> {
|
|
|
|
view! {cx,
|
2022-02-02 16:29:27 -05:00
|
|
|
h2 { "Steps: " }
|
2022-02-02 16:57:24 -05:00
|
|
|
div(class="recipe_steps") {
|
2022-09-25 17:04:46 -04:00
|
|
|
Indexed(
|
|
|
|
iterable=steps,
|
|
|
|
view = |cx, step: recipes::Step| { view! {cx,
|
2022-02-02 16:57:24 -05:00
|
|
|
div {
|
2022-02-12 15:31:35 -05:00
|
|
|
h3 { "Instructions" }
|
2022-02-02 16:29:27 -05:00
|
|
|
ul(class="ingredients") {
|
2022-09-25 17:04:46 -04:00
|
|
|
Indexed(
|
|
|
|
iterable = create_signal(cx, step.ingredients),
|
|
|
|
view = |cx, i| { view! {cx,
|
2022-02-02 16:29:27 -05:00
|
|
|
li {
|
2022-02-14 15:54:26 -05:00
|
|
|
(i.amt) " " (i.name) " " (i.form.as_ref().map(|f| format!("({})", f)).unwrap_or(String::new()))
|
2022-02-02 16:29:27 -05:00
|
|
|
}
|
|
|
|
}}
|
2022-09-25 17:04:46 -04:00
|
|
|
)
|
2022-02-02 16:29:27 -05:00
|
|
|
}
|
2022-02-12 15:31:35 -05:00
|
|
|
div(class="instructions") {
|
|
|
|
(step.instructions)
|
|
|
|
}
|
2022-02-02 16:29:27 -05:00
|
|
|
}}
|
|
|
|
}
|
2022-09-25 17:04:46 -04:00
|
|
|
)
|
2022-02-02 16:29:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
#[component]
|
|
|
|
pub fn Recipe<'ctx, G: Html>(cx: Scope<'ctx>, recipe_id: String) -> View<G> {
|
2022-10-21 14:57:50 -04:00
|
|
|
let state = app_state::State::get_from_context(cx);
|
|
|
|
let store = crate::api::HttpStore::get_from_context(cx);
|
2022-09-25 17:04:46 -04:00
|
|
|
let view = create_signal(cx, View::empty());
|
|
|
|
let show_edit = create_signal(cx, false);
|
2022-10-21 14:57:50 -04:00
|
|
|
if let Some(recipe) = state.recipes.get_untracked().get(&recipe_id) {
|
|
|
|
// FIXME(jwall): This should be create_effect rather than create_signal
|
|
|
|
let recipe_text: &Signal<Option<RecipeEntry>> = create_signal(cx, None);
|
|
|
|
spawn_local_scoped(cx, {
|
|
|
|
let store = store.clone();
|
|
|
|
async move {
|
|
|
|
let entry = store
|
|
|
|
.get_recipe_text(recipe_id.as_str())
|
|
|
|
.await
|
|
|
|
.expect("Failure getting recipe");
|
|
|
|
recipe_text.set(entry);
|
|
|
|
}
|
|
|
|
});
|
2022-09-25 17:04:46 -04:00
|
|
|
let recipe = create_signal(cx, recipe.clone());
|
|
|
|
let title = create_memo(cx, move || recipe.get().title.clone());
|
|
|
|
let desc = create_memo(cx, move || {
|
|
|
|
recipe
|
|
|
|
.clone()
|
|
|
|
.get()
|
|
|
|
.desc
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| String::new())
|
|
|
|
});
|
|
|
|
let steps = create_memo(cx, move || recipe.get().steps.clone());
|
|
|
|
create_effect(cx, move || {
|
2022-10-17 17:16:33 -04:00
|
|
|
debug!("Choosing edit or view for recipe.");
|
2022-09-25 17:04:46 -04:00
|
|
|
if *show_edit.get() {
|
2022-10-17 17:16:33 -04:00
|
|
|
{
|
|
|
|
debug!("Showing editor for recipe.");
|
|
|
|
view.set(view! {cx,
|
|
|
|
Editor(recipe_text.get().as_ref().clone().unwrap())
|
|
|
|
});
|
2022-09-25 17:04:46 -04:00
|
|
|
}
|
2022-10-17 17:16:33 -04:00
|
|
|
} else {
|
|
|
|
debug!("Showing text for recipe.");
|
2022-09-25 17:04:46 -04:00
|
|
|
view.set(view! {cx,
|
2022-10-17 17:16:33 -04:00
|
|
|
div(class="recipe") {
|
|
|
|
h1(class="recipe_title") { (title.get()) }
|
|
|
|
div(class="recipe_description") {
|
|
|
|
(desc.get())
|
|
|
|
}
|
|
|
|
Steps(steps)
|
|
|
|
}
|
2022-09-25 17:04:46 -04:00
|
|
|
});
|
2022-10-17 17:16:33 -04:00
|
|
|
}
|
|
|
|
});
|
2022-09-25 17:04:46 -04:00
|
|
|
}
|
|
|
|
view! {cx,
|
2022-10-17 17:16:33 -04:00
|
|
|
span(role="button", on:click=move |_| { show_edit.set(true); }) { "Edit" } " "
|
|
|
|
span(role="button", on:click=move |_| { show_edit.set(false); }) { "View" }
|
2022-07-21 16:33:55 -04:00
|
|
|
(view.get().as_ref())
|
2022-02-02 16:29:27 -05:00
|
|
|
}
|
|
|
|
}
|