2022-09-19 17:19:29 -04:00
|
|
|
// Copyright 2022 Jeremy Wall (Jeremy@marzhilsltudios.com)
|
|
|
|
//
|
|
|
|
// 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-12-28 14:55:15 -06:00
|
|
|
use crate::{
|
|
|
|
app_state::{Message, StateHandler},
|
|
|
|
js_lib::get_element_by_id,
|
|
|
|
};
|
2022-09-25 17:04:46 -04:00
|
|
|
use sycamore::{futures::spawn_local_scoped, prelude::*};
|
2022-09-19 17:19:29 -04:00
|
|
|
use tracing::{debug, error, instrument};
|
|
|
|
use web_sys::HtmlDialogElement;
|
|
|
|
|
|
|
|
use recipes::parse;
|
|
|
|
|
|
|
|
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("No error-dialog element present")
|
2022-09-19 17:19:29 -04:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
fn check_category_text_parses(unparsed: &str, error_text: &Signal<String>) -> bool {
|
2022-09-19 17:19:29 -04:00
|
|
|
let el = get_error_dialog();
|
|
|
|
if let Err(e) = parse::as_categories(unparsed) {
|
|
|
|
error!(?e, "Error parsing categories");
|
|
|
|
error_text.set(e);
|
|
|
|
el.show();
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
el.close();
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-28 14:55:15 -06:00
|
|
|
#[instrument(skip_all)]
|
2022-09-25 17:04:46 -04:00
|
|
|
#[component]
|
2022-12-28 14:55:15 -06:00
|
|
|
pub fn Categories<'ctx, G: Html>(cx: Scope<'ctx>, sh: StateHandler<'ctx>) -> View<G> {
|
2022-09-25 17:04:46 -04:00
|
|
|
let save_signal = create_signal(cx, ());
|
|
|
|
let error_text = create_signal(cx, String::new());
|
2022-10-21 14:57:50 -04:00
|
|
|
let category_text: &Signal<String> = create_signal(cx, String::new());
|
2022-10-26 15:25:01 -04:00
|
|
|
let dirty = create_signal(cx, false);
|
|
|
|
|
2022-10-21 14:57:50 -04:00
|
|
|
spawn_local_scoped(cx, {
|
|
|
|
let store = crate::api::HttpStore::get_from_context(cx);
|
|
|
|
async move {
|
|
|
|
if let Some(js) = store
|
|
|
|
.get_categories()
|
|
|
|
.await
|
|
|
|
.expect("Failed to get categories.")
|
|
|
|
{
|
|
|
|
category_text.set(js);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
2022-09-19 17:19:29 -04:00
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
create_effect(cx, move || {
|
|
|
|
save_signal.track();
|
2022-10-26 15:25:01 -04:00
|
|
|
if !*dirty.get() {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-25 17:04:46 -04:00
|
|
|
spawn_local_scoped(cx, {
|
|
|
|
async move {
|
2022-12-28 14:55:15 -06:00
|
|
|
sh.dispatch(
|
|
|
|
cx,
|
|
|
|
Message::SetCategoryMap(category_text.get_untracked().as_ref().clone()),
|
|
|
|
);
|
2022-09-25 17:04:46 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-09-19 17:19:29 -04:00
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
let dialog_view = view! {cx,
|
2022-09-19 17:19:29 -04:00
|
|
|
dialog(id="error-dialog") {
|
|
|
|
article{
|
|
|
|
header {
|
|
|
|
a(href="#", on:click=|_| {
|
|
|
|
let el = get_error_dialog();
|
|
|
|
el.close();
|
|
|
|
}, class="close")
|
|
|
|
"Invalid Categories"
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
(error_text.get().clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 17:04:46 -04:00
|
|
|
};
|
2022-09-19 17:19:29 -04:00
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
view! {cx,
|
2022-09-19 17:19:29 -04:00
|
|
|
(dialog_view)
|
2022-10-26 15:25:01 -04:00
|
|
|
textarea(bind:value=category_text, rows=20, on:change=move |_| {
|
|
|
|
dirty.set(true);
|
|
|
|
})
|
2022-10-25 16:27:23 -04:00
|
|
|
span(role="button", on:click=move |_| {
|
2022-09-25 17:04:46 -04:00
|
|
|
check_category_text_parses(category_text.get().as_str(), error_text);
|
|
|
|
}) { "Check" } " "
|
2022-10-25 16:27:23 -04:00
|
|
|
span(role="button", on:click=move |_| {
|
2022-09-19 17:19:29 -04:00
|
|
|
// TODO(jwall): check and then save the categories.
|
2022-09-25 17:04:46 -04:00
|
|
|
if check_category_text_parses(category_text.get().as_str(), error_text) {
|
2022-09-19 17:19:29 -04:00
|
|
|
debug!("triggering category save");
|
|
|
|
save_signal.trigger_subscribers();
|
|
|
|
}
|
2022-09-25 17:04:46 -04:00
|
|
|
}) { "Save" }
|
|
|
|
}
|
2022-09-19 17:19:29 -04:00
|
|
|
}
|