clean up the event flow in the add recipe page

This commit is contained in:
Jeremy Wall 2022-11-07 16:13:40 -05:00
parent cddaf3beaa
commit efbd5140a8
2 changed files with 34 additions and 17 deletions

View File

@ -247,6 +247,9 @@ impl HttpStore {
}; };
if resp.status() != 200 { if resp.status() != 200 {
Err(format!("Status: {}", resp.status()).into()) Err(format!("Status: {}", resp.status()).into())
} else if resp.status() == 404 {
debug!("Recipe doesn't exist");
Ok(None)
} else { } else {
debug!("We got a valid response back!"); debug!("We got a valid response back!");
let entry: Option<RecipeEntry> = resp.json().await.map_err(|e| format!("{}", e))?; let entry: Option<RecipeEntry> = resp.json().await.map_err(|e| format!("{}", e))?;

View File

@ -12,10 +12,11 @@
// 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 sycamore::{futures::spawn_local_scoped, prelude::*}; use sycamore::{futures::spawn_local_scoped, prelude::*};
use tracing::{error, info};
use recipes::RecipeEntry; use recipes::RecipeEntry;
const STARTER_RECIPE: &'static str = "title: Title Here const STARTER_RECIPE: &'static str = "title: TITLE_PLACEHOLDER
Description here. Description here.
@ -28,22 +29,22 @@ Instructions here
#[component] #[component]
pub fn AddRecipePage<G: Html>(cx: Scope) -> View<G> { pub fn AddRecipePage<G: Html>(cx: Scope) -> View<G> {
let entry = create_signal(cx, RecipeEntry(String::new(), String::from(STARTER_RECIPE))); let recipe_title = create_signal(cx, String::new());
let recipe_id = create_signal(cx, String::new());
let create_recipe_signal = create_signal(cx, ()); let create_recipe_signal = create_signal(cx, ());
let dirty = create_signal(cx, false); let dirty = create_signal(cx, false);
create_effect(cx, || { let entry = create_memo(cx, || {
let mut entry_for_edit = entry.get_untracked().as_ref().clone(); RecipeEntry(
// TODO(jwall): This can probably be done more efficiently. recipe_title
let id = recipe_id
.get() .get()
.as_ref() .as_ref()
.to_lowercase()
.replace(" ", "_") .replace(" ", "_")
.replace("\n", "") .replace("\n", ""),
.replace("\r", ""); STARTER_RECIPE
entry_for_edit.set_recipe_id(id); .replace("TITLE_PLACEHOLDER", recipe_title.get().as_str())
entry.set(entry_for_edit); .replace("\r", ""),
)
}); });
create_effect(cx, move || { create_effect(cx, move || {
@ -56,7 +57,20 @@ pub fn AddRecipePage<G: Html>(cx: Scope) -> View<G> {
async move { async move {
let entry = entry.get_untracked(); let entry = entry.get_untracked();
// TODO(jwall): Better error reporting here. // TODO(jwall): Better error reporting here.
// TODO(jwall): Ensure that this id doesn't already exist. match store.get_recipe_text(entry.recipe_id()).await {
Ok(Some(_)) => {
// TODO(jwall): We should tell the user that this id already exists
info!(recipe_id = entry.recipe_id(), "Recipe already exists");
return;
}
Ok(None) => {
// noop
}
Err(err) => {
// TODO(jwall): We should tell the user that this is failing
error!(?err)
}
}
store store
.save_recipes(vec![entry.as_ref().clone()]) .save_recipes(vec![entry.as_ref().clone()])
.await .await
@ -67,8 +81,8 @@ pub fn AddRecipePage<G: Html>(cx: Scope) -> View<G> {
}); });
}); });
view! {cx, view! {cx,
label(for="recipe_id") { "Recipe Id" } label(for="recipe_title") { "Recipe Title" }
input(bind:value=recipe_id, type="text", name="recipe_id", id="recipe_id", on:change=move |_| { input(bind:value=recipe_title, type="text", name="recipe_title", id="recipe_title", on:change=move |_| {
dirty.set(true); dirty.set(true);
}) })
button(on:click=move |_| { button(on:click=move |_| {