Simplify web API (put ids in url)
This commit is contained in:
parent
0c43935bef
commit
6e017e41a3
14 changed files with 403 additions and 588 deletions
|
|
@ -30,18 +30,14 @@ pub fn setup_page(recipe_id: i64) {
|
|||
None => return,
|
||||
};
|
||||
|
||||
// Check if the recipe has been loaded.
|
||||
|
||||
let mut current_title = title.value();
|
||||
EventListener::new(&title.clone(), "blur", move |_event| {
|
||||
if title.value() != current_title {
|
||||
current_title = title.value();
|
||||
let body = ron_api::SetRecipeTitle {
|
||||
recipe_id,
|
||||
title: title.value(),
|
||||
};
|
||||
let title = title.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/title", body).await;
|
||||
let _ =
|
||||
request::patch::<(), _>(&format!("recipe/{recipe_id}/title"), title).await;
|
||||
reload_recipes_list(recipe_id).await;
|
||||
});
|
||||
}
|
||||
|
|
@ -57,12 +53,13 @@ pub fn setup_page(recipe_id: i64) {
|
|||
EventListener::new(&description.clone(), "blur", move |_event| {
|
||||
if description.value() != current_description {
|
||||
current_description = description.value();
|
||||
let body = ron_api::SetRecipeDescription {
|
||||
recipe_id,
|
||||
description: description.value(),
|
||||
};
|
||||
let description = description.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/description", body).await;
|
||||
let _ = request::patch::<(), _>(
|
||||
&format!("recipe/{recipe_id}/description"),
|
||||
description,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -87,12 +84,10 @@ pub fn setup_page(recipe_id: i64) {
|
|||
Some(n)
|
||||
};
|
||||
current_servings = n;
|
||||
let body = ron_api::SetRecipeServings {
|
||||
recipe_id,
|
||||
servings,
|
||||
};
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/servings", body).await;
|
||||
let _ =
|
||||
request::patch::<(), _>(&format!("recipe/{recipe_id}/servings"), servings)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -118,12 +113,12 @@ pub fn setup_page(recipe_id: i64) {
|
|||
Some(n)
|
||||
};
|
||||
current_time = n;
|
||||
let body = ron_api::SetRecipeEstimatedTime {
|
||||
recipe_id,
|
||||
estimated_time: time,
|
||||
};
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/estimated_time", body).await;
|
||||
let _ = request::patch::<(), _>(
|
||||
&format!("recipe/{recipe_id}/estimated_time"),
|
||||
time,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -138,16 +133,14 @@ pub fn setup_page(recipe_id: i64) {
|
|||
EventListener::new(&difficulty.clone(), "blur", move |_event| {
|
||||
if difficulty.value() != current_difficulty {
|
||||
current_difficulty = difficulty.value();
|
||||
|
||||
let body = ron_api::SetRecipeDifficulty {
|
||||
recipe_id,
|
||||
difficulty: ron_api::Difficulty::try_from(
|
||||
current_difficulty.parse::<u32>().unwrap(),
|
||||
)
|
||||
.unwrap(),
|
||||
};
|
||||
let difficulty =
|
||||
ron_api::Difficulty::try_from(difficulty.value().parse::<u32>().unwrap());
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/difficulty", body).await;
|
||||
let _ = request::patch::<(), _>(
|
||||
&format!("recipe/{recipe_id}/difficulty"),
|
||||
difficulty,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -157,24 +150,21 @@ pub fn setup_page(recipe_id: i64) {
|
|||
// Tags.
|
||||
{
|
||||
spawn_local(async move {
|
||||
let tags: ron_api::Tags = request::get("recipe/tags", ron_api::Id { id: recipe_id })
|
||||
let tags: Vec<String> = request::get(&format!("recipe/{recipe_id}/tags"))
|
||||
.await
|
||||
.unwrap();
|
||||
create_tag_elements(recipe_id, &tags.tags);
|
||||
create_tag_elements(recipe_id, &tags);
|
||||
});
|
||||
|
||||
fn add_tags(recipe_id: i64, tags: String) {
|
||||
spawn_local(async move {
|
||||
let tag_list: Vec<String> =
|
||||
tags.split_whitespace().map(str::to_lowercase).collect();
|
||||
if !tag_list.is_empty() {
|
||||
let body = ron_api::Tags {
|
||||
recipe_id,
|
||||
tags: tag_list.clone(),
|
||||
};
|
||||
let _ = request::post::<(), _>("recipe/tags", body).await;
|
||||
create_tag_elements(recipe_id, &tag_list);
|
||||
}
|
||||
let _ =
|
||||
request::post::<(), _>(&format!("recipe/{recipe_id}/tags"), Some(&tag_list))
|
||||
.await;
|
||||
create_tag_elements(recipe_id, &tag_list);
|
||||
|
||||
by_id::<HtmlInputElement>("input-tags").set_value("");
|
||||
});
|
||||
}
|
||||
|
|
@ -214,13 +204,11 @@ pub fn setup_page(recipe_id: i64) {
|
|||
EventListener::new(&language.clone(), "blur", move |_event| {
|
||||
if language.value() != current_language {
|
||||
current_language = language.value();
|
||||
|
||||
let body = ron_api::SetRecipeLanguage {
|
||||
recipe_id,
|
||||
lang: language.value(),
|
||||
};
|
||||
let language = language.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/language", body).await;
|
||||
let _ =
|
||||
request::patch::<(), _>(&format!("recipe/{recipe_id}/language"), language)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -231,12 +219,11 @@ pub fn setup_page(recipe_id: i64) {
|
|||
{
|
||||
let is_public: HtmlInputElement = by_id("input-is-public");
|
||||
EventListener::new(&is_public.clone(), "input", move |_event| {
|
||||
let body = ron_api::SetRecipeIsPublic {
|
||||
recipe_id,
|
||||
is_public: is_public.checked(),
|
||||
};
|
||||
let is_public = is_public.checked();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/is_public", body).await;
|
||||
let _ =
|
||||
request::patch::<(), _>(&format!("recipe/{recipe_id}/is_public"), is_public)
|
||||
.await;
|
||||
reload_recipes_list(recipe_id).await;
|
||||
});
|
||||
})
|
||||
|
|
@ -261,8 +248,8 @@ pub fn setup_page(recipe_id: i64) {
|
|||
.await
|
||||
.is_some()
|
||||
{
|
||||
let body = ron_api::Id { id: recipe_id };
|
||||
if let Ok(()) = request::delete::<(), _>("recipe", body).await {
|
||||
if let Ok(()) = request::delete::<_, ()>(&format!("recipe/{recipe_id}"), None).await
|
||||
{
|
||||
window()
|
||||
.location()
|
||||
.set_href(&format!(
|
||||
|
|
@ -284,7 +271,7 @@ pub fn setup_page(recipe_id: i64) {
|
|||
{
|
||||
spawn_local(async move {
|
||||
let groups: Vec<common::ron_api::Group> =
|
||||
request::get("recipe/groups", ron_api::Id { id: recipe_id })
|
||||
request::get(&format!("recipe/{recipe_id}/groups"))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
|
@ -310,11 +297,12 @@ pub fn setup_page(recipe_id: i64) {
|
|||
{
|
||||
let button_add_group: HtmlInputElement = by_id("input-add-group");
|
||||
EventListener::new(&button_add_group, "click", move |_event| {
|
||||
let body = ron_api::Id { id: recipe_id };
|
||||
spawn_local(async move {
|
||||
let response: ron_api::Id = request::post("recipe/group", body).await.unwrap();
|
||||
let id: i64 = request::post::<_, ()>(&format!("recipe/{recipe_id}/group"), None)
|
||||
.await
|
||||
.unwrap();
|
||||
create_group_element(&ron_api::Group {
|
||||
id: response.id,
|
||||
id,
|
||||
name: "".to_string(),
|
||||
comment: "".to_string(),
|
||||
steps: vec![],
|
||||
|
|
@ -335,14 +323,13 @@ fn create_group_element(group: &ron_api::Group) -> Element {
|
|||
|
||||
set_draggable(&group_element, "group", |_element| {
|
||||
spawn_local(async move {
|
||||
let ids = by_id::<Element>("groups-container")
|
||||
let ids: Vec<i64> = by_id::<Element>("groups-container")
|
||||
.selector_all::<Element>(".group")
|
||||
.into_iter()
|
||||
.map(|e| e.id()[6..].parse::<i64>().unwrap())
|
||||
.collect();
|
||||
|
||||
let body = ron_api::Ids { ids };
|
||||
let _ = request::patch::<(), _>("recipe/groups_order", body).await;
|
||||
let _ = request::patch::<(), _>("groups/order", ids).await;
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -353,12 +340,9 @@ fn create_group_element(group: &ron_api::Group) -> Element {
|
|||
EventListener::new(&name.clone(), "blur", move |_event| {
|
||||
if name.value() != current_name {
|
||||
current_name = name.value();
|
||||
let body = ron_api::SetGroupName {
|
||||
group_id,
|
||||
name: name.value(),
|
||||
};
|
||||
let name = name.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/group_name", body).await;
|
||||
let _ = request::patch::<(), _>(&format!("group/{group_id}/name"), name).await;
|
||||
})
|
||||
}
|
||||
})
|
||||
|
|
@ -371,12 +355,10 @@ fn create_group_element(group: &ron_api::Group) -> Element {
|
|||
EventListener::new(&comment.clone(), "blur", move |_event| {
|
||||
if comment.value() != current_comment {
|
||||
current_comment = comment.value();
|
||||
let body = ron_api::SetGroupComment {
|
||||
group_id,
|
||||
comment: comment.value(),
|
||||
};
|
||||
let comment = comment.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/group_comment", body).await;
|
||||
let _ =
|
||||
request::patch::<(), _>(&format!("group/{group_id}/comment"), comment).await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -401,9 +383,8 @@ fn create_group_element(group: &ron_api::Group) -> Element {
|
|||
.await
|
||||
.is_some()
|
||||
{
|
||||
let body = ron_api::Id { id: group_id };
|
||||
let _ = request::delete::<(), _>("recipe/group", body).await;
|
||||
let group_element = by_id::<Element>(&format!("group-{}", group_id));
|
||||
let _ = request::delete::<(), ()>(&format!("group/{group_id}"), None).await;
|
||||
let group_element = by_id::<Element>(&format!("group-{group_id}"));
|
||||
group_element.next_element_sibling().unwrap().remove();
|
||||
group_element.remove();
|
||||
}
|
||||
|
|
@ -415,12 +396,13 @@ fn create_group_element(group: &ron_api::Group) -> Element {
|
|||
let add_step_button: HtmlInputElement = group_element.selector(".input-add-step");
|
||||
EventListener::new(&add_step_button, "click", move |_event| {
|
||||
spawn_local(async move {
|
||||
let body = ron_api::Id { id: group_id };
|
||||
let response: ron_api::Id = request::post("recipe/step", body).await.unwrap();
|
||||
let id: i64 = request::post::<_, ()>(&format!("group/{group_id}/step"), None)
|
||||
.await
|
||||
.unwrap();
|
||||
create_step_element(
|
||||
&selector::<Element>(&format!("#group-{} .steps", group_id)),
|
||||
&selector::<Element>(&format!("#group-{group_id} .steps")),
|
||||
&ron_api::Step {
|
||||
id: response.id,
|
||||
id,
|
||||
action: "".to_string(),
|
||||
ingredients: vec![],
|
||||
},
|
||||
|
|
@ -474,11 +456,9 @@ where
|
|||
let tag_span = tag_span.clone();
|
||||
let tag = tag.clone();
|
||||
spawn_local(async move {
|
||||
let body = ron_api::Tags {
|
||||
recipe_id,
|
||||
tags: vec![tag],
|
||||
};
|
||||
let _ = request::delete::<(), _>("recipe/tags", body).await;
|
||||
let _ =
|
||||
request::delete::<(), _>(&format!("recipe/{recipe_id}/tags"), Some(vec![tag]))
|
||||
.await;
|
||||
tag_span.remove();
|
||||
});
|
||||
})
|
||||
|
|
@ -495,7 +475,7 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
|
|||
set_draggable(&step_element, "step", |element| {
|
||||
let element = element.clone();
|
||||
spawn_local(async move {
|
||||
let ids = element
|
||||
let ids: Vec<i64> = element
|
||||
.parent_element()
|
||||
.unwrap()
|
||||
.selector_all::<Element>(".step")
|
||||
|
|
@ -503,8 +483,7 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
|
|||
.map(|e| e.id()[5..].parse::<i64>().unwrap())
|
||||
.collect();
|
||||
|
||||
let body = ron_api::Ids { ids };
|
||||
let _ = request::patch::<(), _>("recipe/steps_order", body).await;
|
||||
let _ = request::patch::<(), _>("/steps/order", ids).await;
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -515,12 +494,9 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
|
|||
EventListener::new(&action.clone(), "blur", move |_event| {
|
||||
if action.value() != current_action {
|
||||
current_action = action.value();
|
||||
let body = ron_api::SetStepAction {
|
||||
step_id,
|
||||
action: action.value(),
|
||||
};
|
||||
let action = action.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/step_action", body).await;
|
||||
let _ = request::patch::<(), _>(&format!("/step/{step_id}/action"), action).await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -545,9 +521,8 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
|
|||
.await
|
||||
.is_some()
|
||||
{
|
||||
let body = ron_api::Id { id: step_id };
|
||||
let _ = request::delete::<(), _>("recipe/step", body).await;
|
||||
let step_element = by_id::<Element>(&format!("step-{}", step_id));
|
||||
let _ = request::delete::<(), ()>(&format!("step/{step_id}"), None).await;
|
||||
let step_element = by_id::<Element>(&format!("step-{step_id}"));
|
||||
step_element.next_element_sibling().unwrap().remove();
|
||||
step_element.remove();
|
||||
}
|
||||
|
|
@ -559,12 +534,13 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
|
|||
let add_ingredient_button: HtmlInputElement = step_element.selector(".input-add-ingredient");
|
||||
EventListener::new(&add_ingredient_button, "click", move |_event| {
|
||||
spawn_local(async move {
|
||||
let body = ron_api::Id { id: step_id };
|
||||
let response: ron_api::Id = request::post("recipe/ingredient", body).await.unwrap();
|
||||
let id: i64 = request::post::<_, ()>(&format!("step/{step_id}/ingredient"), None)
|
||||
.await
|
||||
.unwrap();
|
||||
create_ingredient_element(
|
||||
&selector::<Element>(&format!("#step-{} .ingredients", step_id)),
|
||||
&ron_api::Ingredient {
|
||||
id: response.id,
|
||||
id,
|
||||
name: "".to_string(),
|
||||
comment: "".to_string(),
|
||||
quantity_value: None,
|
||||
|
|
@ -587,7 +563,7 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
set_draggable(&ingredient_element, "ingredient", |element| {
|
||||
let element = element.clone();
|
||||
spawn_local(async move {
|
||||
let ids = element
|
||||
let ids: Vec<i64> = element
|
||||
.parent_element()
|
||||
.unwrap()
|
||||
.selector_all::<Element>(".ingredient")
|
||||
|
|
@ -595,8 +571,7 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
.map(|e| e.id()[11..].parse::<i64>().unwrap())
|
||||
.collect();
|
||||
|
||||
let body = ron_api::Ids { ids };
|
||||
let _ = request::patch::<(), _>("recipe/ingredients_order", body).await;
|
||||
let _ = request::patch::<(), _>("ingredients/order", ids).await;
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -607,12 +582,10 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
EventListener::new(&name.clone(), "blur", move |_event| {
|
||||
if name.value() != current_name {
|
||||
current_name = name.value();
|
||||
let body = ron_api::SetIngredientName {
|
||||
ingredient_id,
|
||||
name: name.value(),
|
||||
};
|
||||
let name = name.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/ingredient_name", body).await;
|
||||
let _ = request::patch::<(), _>(&format!("ingredient/{ingredient_id}/name"), name)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -625,12 +598,13 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
EventListener::new(&comment.clone(), "blur", move |_event| {
|
||||
if comment.value() != current_comment {
|
||||
current_comment = comment.value();
|
||||
let body = ron_api::SetIngredientComment {
|
||||
ingredient_id,
|
||||
comment: comment.value(),
|
||||
};
|
||||
let comment = comment.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/ingredient_comment", body).await;
|
||||
let _ = request::patch::<(), _>(
|
||||
&format!("ingredient/{ingredient_id}/comment"),
|
||||
comment,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -652,12 +626,9 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
if n != current_quantity {
|
||||
let q = if n.is_nan() { None } else { Some(n) };
|
||||
current_quantity = n;
|
||||
let body = ron_api::SetIngredientQuantity {
|
||||
ingredient_id,
|
||||
quantity: q,
|
||||
};
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/ingredient_quantity", body).await;
|
||||
let _ = request::patch::<(), _>(&format!("ingredient/{ingredient_id}/quantity"), q)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -670,12 +641,10 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
EventListener::new(&unit.clone(), "blur", move |_event| {
|
||||
if unit.value() != current_unit {
|
||||
current_unit = unit.value();
|
||||
let body = ron_api::SetIngredientUnit {
|
||||
ingredient_id,
|
||||
unit: unit.value(),
|
||||
};
|
||||
let unit = unit.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>("recipe/ingredient_unit", body).await;
|
||||
let _ = request::patch::<(), _>(&format!("ingredient/{ingredient_id}/unit"), unit)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -700,9 +669,9 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
.await
|
||||
.is_some()
|
||||
{
|
||||
let body = ron_api::Id { id: ingredient_id };
|
||||
let _ = request::delete::<(), _>("recipe/ingredient", body).await;
|
||||
let ingredient_element = by_id::<Element>(&format!("ingredient-{}", ingredient_id));
|
||||
let _ =
|
||||
request::delete::<(), ()>(&format!("ingredient/{ingredient_id}"), None).await;
|
||||
let ingredient_element = by_id::<Element>(&format!("ingredient-{ingredient_id}"));
|
||||
ingredient_element.next_element_sibling().unwrap().remove();
|
||||
ingredient_element.remove();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue