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
|
|
|
|
2022-12-26 22:22:05 -05:00
|
|
|
use crate::app_state::{self, Message, StateHandler};
|
2022-10-21 14:57:50 -04:00
|
|
|
use recipes::{self, RecipeEntry};
|
2022-09-05 17:49:00 -04:00
|
|
|
|
2022-11-08 17:32:51 -05:00
|
|
|
fn check_recipe_parses(
|
|
|
|
text: &str,
|
|
|
|
error_text: &Signal<String>,
|
|
|
|
aria_hint: &Signal<&'static str>,
|
|
|
|
) -> 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);
|
2022-11-08 17:32:51 -05:00
|
|
|
aria_hint.set("true");
|
2022-09-05 17:49:00 -04:00
|
|
|
false
|
|
|
|
} else {
|
2022-11-08 17:32:51 -05:00
|
|
|
error_text.set(String::from("No parse errors..."));
|
|
|
|
aria_hint.set("false");
|
2022-09-05 17:49:00 -04:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-26 22:22:05 -05:00
|
|
|
#[derive(Props)]
|
|
|
|
pub struct RecipeComponentProps<'ctx> {
|
|
|
|
recipe_id: String,
|
|
|
|
sh: StateHandler<'ctx>,
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
#[component]
|
2022-12-26 22:22:05 -05:00
|
|
|
pub fn Editor<'ctx, G: Html>(cx: Scope<'ctx>, props: RecipeComponentProps<'ctx>) -> View<G> {
|
|
|
|
let RecipeComponentProps { recipe_id, sh } = props;
|
2022-11-07 16:47:46 -05:00
|
|
|
let store = crate::api::HttpStore::get_from_context(cx);
|
|
|
|
let recipe: &Signal<RecipeEntry> =
|
|
|
|
create_signal(cx, RecipeEntry::new(&recipe_id, String::new()));
|
|
|
|
let text = create_signal(cx, String::new());
|
2022-11-08 17:32:51 -05:00
|
|
|
let error_text = create_signal(cx, String::from("Parse results..."));
|
|
|
|
let aria_hint = create_signal(cx, "false");
|
|
|
|
|
2022-11-07 16:47:46 -05:00
|
|
|
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");
|
|
|
|
if let Some(entry) = entry {
|
|
|
|
text.set(entry.recipe_text().to_owned());
|
|
|
|
recipe.set(entry);
|
|
|
|
} else {
|
2022-11-08 17:32:51 -05:00
|
|
|
error_text.set("Unable to find recipe".to_owned());
|
2022-11-07 16:47:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let id = create_memo(cx, || recipe.get().recipe_id().to_owned());
|
2022-09-25 17:04:46 -04:00
|
|
|
let save_signal = create_signal(cx, ());
|
2022-10-26 15:25:01 -04:00
|
|
|
let dirty = create_signal(cx, false);
|
2022-09-08 20:17:46 -04:00
|
|
|
|
2022-11-01 20:38:14 -04:00
|
|
|
debug!("Creating effect");
|
2022-09-25 17:04:46 -04:00
|
|
|
create_effect(cx, move || {
|
|
|
|
save_signal.track();
|
2022-11-01 20:38:14 -04:00
|
|
|
if !*dirty.get_untracked() {
|
|
|
|
debug!("Recipe text is unchanged");
|
2022-10-26 15:25:01 -04:00
|
|
|
return;
|
|
|
|
}
|
2022-11-01 20:38:14 -04:00
|
|
|
debug!("Recipe text is changed");
|
2022-09-25 17:04:46 -04:00
|
|
|
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-11-01 20:38:14 -04:00
|
|
|
debug!("Attempting to save recipe");
|
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-10-26 15:25:01 -04:00
|
|
|
} else {
|
2022-11-07 16:47:46 -05:00
|
|
|
// We also need to set recipe in our state
|
2022-10-26 15:25:01 -04:00
|
|
|
dirty.set(false);
|
2022-11-07 16:47:46 -05:00
|
|
|
if let Ok(recipe) = recipes::parse::as_recipe(text.get_untracked().as_ref()) {
|
2022-12-28 12:59:11 -06:00
|
|
|
sh.dispatch(
|
|
|
|
cx,
|
|
|
|
Message::SetRecipe(id.get_untracked().as_ref().to_owned(), recipe),
|
|
|
|
);
|
2022-11-07 16:47:46 -05:00
|
|
|
}
|
2022-09-25 17:04:46 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-09-05 17:49:00 -04:00
|
|
|
|
2022-11-01 20:38:14 -04:00
|
|
|
debug!("creating editor view");
|
2022-09-25 17:04:46 -04:00
|
|
|
view! {cx,
|
2022-11-08 17:32:51 -05:00
|
|
|
div(class="grid") {
|
|
|
|
textarea(bind:value=text, aria-invalid=aria_hint.get(), rows=20, on:change=move |_| {
|
|
|
|
dirty.set(true);
|
|
|
|
})
|
|
|
|
div(class="parse") { (error_text.get()) }
|
|
|
|
}
|
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();
|
2022-11-08 17:32:51 -05:00
|
|
|
check_recipe_parses(unparsed.as_str(), error_text, aria_hint);
|
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();
|
2022-11-08 17:32:51 -05:00
|
|
|
if check_recipe_parses(unparsed.as_str(), error_text, aria_hint) {
|
2022-09-08 20:17:46 -04:00
|
|
|
debug!("triggering a save");
|
|
|
|
save_signal.trigger_subscribers();
|
2022-11-08 17:32:51 -05:00
|
|
|
} else {
|
|
|
|
}
|
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]
|
2022-11-07 16:47:46 -05:00
|
|
|
fn Steps<G: Html>(cx: Scope, steps: Vec<recipes::Step>) -> View<G> {
|
|
|
|
let step_fragments = View::new_fragment(steps.iter().map(|step| {
|
|
|
|
let mut step = step.clone();
|
|
|
|
let ingredient_fragments = View::new_fragment(step.ingredients.drain(0..).map(|i| {
|
|
|
|
view! {cx,
|
|
|
|
li {
|
|
|
|
(i.amt) " " (i.name) " " (i.form.as_ref().map(|f| format!("({})", f)).unwrap_or(String::new()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).collect());
|
|
|
|
view! {cx,
|
|
|
|
div {
|
|
|
|
h3 { "Instructions" }
|
|
|
|
ul(class="ingredients") {
|
|
|
|
(ingredient_fragments)
|
|
|
|
}
|
|
|
|
div(class="instructions") {
|
|
|
|
(step.instructions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).collect());
|
2022-09-25 17:04:46 -04:00
|
|
|
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-11-07 16:47:46 -05:00
|
|
|
(step_fragments)
|
2022-02-02 16:29:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
#[component]
|
2022-12-26 22:22:05 -05:00
|
|
|
pub fn Viewer<'ctx, G: Html>(cx: Scope<'ctx>, props: RecipeComponentProps<'ctx>) -> View<G> {
|
|
|
|
let RecipeComponentProps { recipe_id, sh } = props;
|
2022-10-21 14:57:50 -04:00
|
|
|
let state = app_state::State::get_from_context(cx);
|
2022-09-25 17:04:46 -04:00
|
|
|
let view = create_signal(cx, View::empty());
|
2022-12-27 22:08:50 -06:00
|
|
|
let recipe_signal = sh.get_selector(cx, move |state| {
|
2022-12-26 22:22:05 -05:00
|
|
|
if let Some(recipe) = state.get().recipes.get(&recipe_id) {
|
|
|
|
let title = recipe.title.clone();
|
|
|
|
let desc = recipe.desc.clone().unwrap_or_else(|| String::new());
|
|
|
|
let steps = recipe.steps.clone();
|
|
|
|
Some((title, desc, steps))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if let Some((title, desc, steps)) = recipe_signal.get().as_ref().clone() {
|
2022-11-07 16:47:46 -05:00
|
|
|
debug!("Viewing recipe.");
|
|
|
|
view.set(view! {cx,
|
|
|
|
div(class="recipe") {
|
|
|
|
h1(class="recipe_title") { (title) }
|
|
|
|
div(class="recipe_description") {
|
|
|
|
(desc)
|
|
|
|
}
|
|
|
|
Steps(steps)
|
2022-10-17 17:16:33 -04:00
|
|
|
}
|
|
|
|
});
|
2022-09-25 17:04:46 -04:00
|
|
|
}
|
2022-11-07 16:47:46 -05:00
|
|
|
view! {cx, (view.get().as_ref()) }
|
2022-02-02 16:29:27 -05:00
|
|
|
}
|