Add a RON API to search recipes by title + a bit of refactoring
This commit is contained in:
parent
a3f61e3711
commit
084f7ef445
26 changed files with 499 additions and 333 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use chrono::{Datelike, Days, Months, NaiveDate, Weekday, offset::Local};
|
||||
use common::{ron_api, utils::substitute_with_names};
|
||||
use common::{web_api, utils::substitute_with_names};
|
||||
use gloo::{
|
||||
events::EventListener,
|
||||
utils::{document, window},
|
||||
|
|
@ -13,7 +13,7 @@ use web_sys::{Element, HtmlInputElement};
|
|||
use crate::{
|
||||
modal_dialog,
|
||||
recipe_scheduler::RecipeScheduler,
|
||||
request,
|
||||
ron_request,
|
||||
utils::{SelectorExt, by_id, get_locale, selector, selector_all},
|
||||
};
|
||||
|
||||
|
|
@ -167,12 +167,14 @@ pub fn setup(
|
|||
)
|
||||
.await
|
||||
{
|
||||
let body = ron_api::RemoveScheduledRecipe {
|
||||
let body = web_api::RemoveScheduledRecipe {
|
||||
recipe_id,
|
||||
date,
|
||||
remove_ingredients_from_shopping_list,
|
||||
};
|
||||
let _ = request::delete::<(), _>("calendar/scheduled_recipe", Some(body)).await;
|
||||
let _ =
|
||||
ron_request::delete::<(), _>("/ron-api/calendar/scheduled_recipe", Some(body))
|
||||
.await;
|
||||
window().location().reload().unwrap();
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ mod on_click;
|
|||
mod pages;
|
||||
mod recipe_scheduler;
|
||||
mod request;
|
||||
mod ron_request;
|
||||
mod shopping_list;
|
||||
mod toast;
|
||||
mod utils;
|
||||
|
|
@ -86,7 +87,7 @@ pub fn main() -> Result<(), JsValue> {
|
|||
|
||||
// Request the message to display.
|
||||
spawn_local(async move {
|
||||
let translation: String = request::get(&format!("translation/{mess_id}"))
|
||||
let translation: String = ron_request::get(&format!("/ron-api/translation/{mess_id}"))
|
||||
.await
|
||||
.unwrap();
|
||||
if let Some(level_id) = level_id {
|
||||
|
|
@ -105,10 +106,9 @@ pub fn main() -> Result<(), JsValue> {
|
|||
let select_language: HtmlSelectElement = by_id("select-website-language");
|
||||
EventListener::new(&select_language.clone(), "input", move |_event| {
|
||||
let lang = select_language.value();
|
||||
// let body = ron_api::SetLang { lang: lang.clone() };
|
||||
let location_without_lang = location_without_lang.clone();
|
||||
spawn_local(async move {
|
||||
let _ = request::put::<(), _>("lang", &lang).await;
|
||||
let _ = ron_request::put::<(), _>("/ron-api/lang", &lang).await;
|
||||
|
||||
window()
|
||||
.location()
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use std::{cell::RefCell, rc, sync::Mutex};
|
||||
|
||||
use common::{ron_api, utils::substitute};
|
||||
use common::{web_api, utils::substitute};
|
||||
use gloo::{
|
||||
events::{EventListener, EventListenerOptions},
|
||||
net::http::Request,
|
||||
|
|
@ -14,7 +14,7 @@ use web_sys::{
|
|||
};
|
||||
|
||||
use crate::{
|
||||
modal_dialog, request,
|
||||
modal_dialog, request, ron_request,
|
||||
toast::{self, Level},
|
||||
utils::{SelectorExt, by_id, get_current_lang, selector, selector_and_clone},
|
||||
};
|
||||
|
|
@ -36,8 +36,11 @@ pub fn setup_page(recipe_id: i64) {
|
|||
current_title = title.value();
|
||||
let title = title.value();
|
||||
spawn_local(async move {
|
||||
let _ =
|
||||
request::patch::<(), _>(&format!("recipe/{recipe_id}/title"), title).await;
|
||||
let _ = ron_request::patch::<(), _>(
|
||||
&format!("/ron-api/recipe/{recipe_id}/title"),
|
||||
title,
|
||||
)
|
||||
.await;
|
||||
reload_recipes_list(recipe_id).await;
|
||||
});
|
||||
}
|
||||
|
|
@ -55,8 +58,8 @@ pub fn setup_page(recipe_id: i64) {
|
|||
current_description = description.value();
|
||||
let description = description.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>(
|
||||
&format!("recipe/{recipe_id}/description"),
|
||||
let _ = ron_request::patch::<(), _>(
|
||||
&format!("/ron-api/recipe/{recipe_id}/description"),
|
||||
description,
|
||||
)
|
||||
.await;
|
||||
|
|
@ -85,9 +88,11 @@ pub fn setup_page(recipe_id: i64) {
|
|||
};
|
||||
current_servings = n;
|
||||
spawn_local(async move {
|
||||
let _ =
|
||||
request::patch::<(), _>(&format!("recipe/{recipe_id}/servings"), servings)
|
||||
.await;
|
||||
let _ = ron_request::patch::<(), _>(
|
||||
&format!("/ron-api/recipe/{recipe_id}/servings"),
|
||||
servings,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -114,8 +119,8 @@ pub fn setup_page(recipe_id: i64) {
|
|||
};
|
||||
current_time = n;
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>(
|
||||
&format!("recipe/{recipe_id}/estimated_time"),
|
||||
let _ = ron_request::patch::<(), _>(
|
||||
&format!("/ron-api/recipe/{recipe_id}/estimated_time"),
|
||||
time,
|
||||
)
|
||||
.await;
|
||||
|
|
@ -134,11 +139,11 @@ pub fn setup_page(recipe_id: i64) {
|
|||
if difficulty.value() != current_difficulty {
|
||||
current_difficulty = difficulty.value();
|
||||
let difficulty =
|
||||
ron_api::Difficulty::from_repr(difficulty.value().parse::<u32>().unwrap())
|
||||
.unwrap_or(ron_api::Difficulty::Unknown);
|
||||
web_api::Difficulty::from_repr(difficulty.value().parse::<u32>().unwrap())
|
||||
.unwrap_or(web_api::Difficulty::Unknown);
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>(
|
||||
&format!("recipe/{recipe_id}/difficulty"),
|
||||
let _ = ron_request::patch::<(), _>(
|
||||
&format!("/ron-api/recipe/{recipe_id}/difficulty"),
|
||||
difficulty,
|
||||
)
|
||||
.await;
|
||||
|
|
@ -151,7 +156,7 @@ pub fn setup_page(recipe_id: i64) {
|
|||
// Tags.
|
||||
{
|
||||
spawn_local(async move {
|
||||
let tags: Vec<String> = request::get(&format!("recipe/{recipe_id}/tags"))
|
||||
let tags: Vec<String> = ron_request::get(&format!("/ron-api/recipe/{recipe_id}/tags"))
|
||||
.await
|
||||
.unwrap();
|
||||
create_tag_elements(recipe_id, &tags);
|
||||
|
|
@ -161,9 +166,11 @@ pub fn setup_page(recipe_id: i64) {
|
|||
spawn_local(async move {
|
||||
let tag_list: Vec<String> =
|
||||
tags.split_whitespace().map(str::to_lowercase).collect();
|
||||
let _ =
|
||||
request::post::<(), _>(&format!("recipe/{recipe_id}/tags"), Some(&tag_list))
|
||||
.await;
|
||||
let _ = ron_request::post::<(), _>(
|
||||
&format!("/ron-api/recipe/{recipe_id}/tags"),
|
||||
Some(&tag_list),
|
||||
)
|
||||
.await;
|
||||
create_tag_elements(recipe_id, &tag_list);
|
||||
|
||||
by_id::<HtmlInputElement>("input-tags").set_value("");
|
||||
|
|
@ -207,9 +214,11 @@ pub fn setup_page(recipe_id: i64) {
|
|||
current_language = language.value();
|
||||
let language = language.value();
|
||||
spawn_local(async move {
|
||||
let _ =
|
||||
request::patch::<(), _>(&format!("recipe/{recipe_id}/language"), language)
|
||||
.await;
|
||||
let _ = ron_request::patch::<(), _>(
|
||||
&format!("/ron-api/recipe/{recipe_id}/language"),
|
||||
language,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -222,9 +231,11 @@ pub fn setup_page(recipe_id: i64) {
|
|||
EventListener::new(&is_public.clone(), "input", move |_event| {
|
||||
let is_public = is_public.checked();
|
||||
spawn_local(async move {
|
||||
let _ =
|
||||
request::patch::<(), _>(&format!("recipe/{recipe_id}/is_public"), is_public)
|
||||
.await;
|
||||
let _ = ron_request::patch::<(), _>(
|
||||
&format!("/ron-api/recipe/{recipe_id}/is_public"),
|
||||
is_public,
|
||||
)
|
||||
.await;
|
||||
reload_recipes_list(recipe_id).await;
|
||||
});
|
||||
})
|
||||
|
|
@ -249,7 +260,9 @@ pub fn setup_page(recipe_id: i64) {
|
|||
.await
|
||||
.is_some()
|
||||
{
|
||||
if let Ok(()) = request::delete::<_, ()>(&format!("recipe/{recipe_id}"), None).await
|
||||
if let Ok(()) =
|
||||
ron_request::delete::<_, ()>(&format!("/ron-api/recipe/{recipe_id}"), None)
|
||||
.await
|
||||
{
|
||||
window()
|
||||
.location()
|
||||
|
|
@ -271,8 +284,8 @@ pub fn setup_page(recipe_id: i64) {
|
|||
// Load initial groups, steps and ingredients.
|
||||
{
|
||||
spawn_local(async move {
|
||||
let groups: Vec<common::ron_api::Group> =
|
||||
request::get(&format!("recipe/{recipe_id}/groups"))
|
||||
let groups: Vec<common::web_api::Group> =
|
||||
ron_request::get(&format!("/ron-api/recipe/{recipe_id}/groups"))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
|
@ -299,10 +312,11 @@ 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| {
|
||||
spawn_local(async move {
|
||||
let id: i64 = request::post::<_, ()>(&format!("recipe/{recipe_id}/group"), None)
|
||||
.await
|
||||
.unwrap();
|
||||
create_group_element(&ron_api::Group {
|
||||
let id: i64 =
|
||||
ron_request::post::<_, ()>(&format!("/ron-api/recipe/{recipe_id}/group"), None)
|
||||
.await
|
||||
.unwrap();
|
||||
create_group_element(&web_api::Group {
|
||||
id,
|
||||
name: "".to_string(),
|
||||
comment: "".to_string(),
|
||||
|
|
@ -314,7 +328,7 @@ pub fn setup_page(recipe_id: i64) {
|
|||
}
|
||||
}
|
||||
|
||||
fn create_group_element(group: &ron_api::Group) -> Element {
|
||||
fn create_group_element(group: &web_api::Group) -> Element {
|
||||
let group_id = group.id;
|
||||
let group_element: Element = selector_and_clone("#hidden-templates .group");
|
||||
group_element.set_id(&format!("group-{}", group.id));
|
||||
|
|
@ -330,7 +344,7 @@ fn create_group_element(group: &ron_api::Group) -> Element {
|
|||
.map(|e| e.id()[6..].parse::<i64>().unwrap())
|
||||
.collect();
|
||||
|
||||
let _ = request::patch::<(), _>("groups/order", ids).await;
|
||||
let _ = ron_request::patch::<(), _>("/ron-api/groups/order", ids).await;
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -343,7 +357,9 @@ fn create_group_element(group: &ron_api::Group) -> Element {
|
|||
current_name = name.value();
|
||||
let name = name.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>(&format!("group/{group_id}/name"), name).await;
|
||||
let _ =
|
||||
ron_request::patch::<(), _>(&format!("/ron-api/group/{group_id}/name"), name)
|
||||
.await;
|
||||
})
|
||||
}
|
||||
})
|
||||
|
|
@ -358,8 +374,11 @@ fn create_group_element(group: &ron_api::Group) -> Element {
|
|||
current_comment = comment.value();
|
||||
let comment = comment.value();
|
||||
spawn_local(async move {
|
||||
let _ =
|
||||
request::patch::<(), _>(&format!("group/{group_id}/comment"), comment).await;
|
||||
let _ = ron_request::patch::<(), _>(
|
||||
&format!("/ron-api/group/{group_id}/comment"),
|
||||
comment,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -384,7 +403,8 @@ fn create_group_element(group: &ron_api::Group) -> Element {
|
|||
.await
|
||||
.is_some()
|
||||
{
|
||||
let _ = request::delete::<(), ()>(&format!("group/{group_id}"), None).await;
|
||||
let _ = ron_request::delete::<(), ()>(&format!("/ron-api/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();
|
||||
|
|
@ -397,12 +417,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 id: i64 = request::post::<_, ()>(&format!("group/{group_id}/step"), None)
|
||||
.await
|
||||
.unwrap();
|
||||
let id: i64 =
|
||||
ron_request::post::<_, ()>(&format!("/ron-api/group/{group_id}/step"), None)
|
||||
.await
|
||||
.unwrap();
|
||||
create_step_element(
|
||||
&selector::<Element>(&format!("#group-{group_id} .steps")),
|
||||
&ron_api::Step {
|
||||
&web_api::Step {
|
||||
id,
|
||||
action: "".to_string(),
|
||||
ingredients: vec![],
|
||||
|
|
@ -457,9 +478,11 @@ where
|
|||
let tag_span = tag_span.clone();
|
||||
let tag = tag.clone();
|
||||
spawn_local(async move {
|
||||
let _ =
|
||||
request::delete::<(), _>(&format!("recipe/{recipe_id}/tags"), Some(vec![tag]))
|
||||
.await;
|
||||
let _ = ron_request::delete::<(), _>(
|
||||
&format!("/ron-api/recipe/{recipe_id}/tags"),
|
||||
Some(vec![tag]),
|
||||
)
|
||||
.await;
|
||||
tag_span.remove();
|
||||
});
|
||||
})
|
||||
|
|
@ -467,7 +490,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element {
|
||||
fn create_step_element(group_element: &Element, step: &web_api::Step) -> Element {
|
||||
let step_id = step.id;
|
||||
let step_element: Element = selector_and_clone("#hidden-templates .step");
|
||||
step_element.set_id(&format!("step-{}", step.id));
|
||||
|
|
@ -484,7 +507,7 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
|
|||
.map(|e| e.id()[5..].parse::<i64>().unwrap())
|
||||
.collect();
|
||||
|
||||
let _ = request::patch::<(), _>("/steps/order", ids).await;
|
||||
let _ = ron_request::patch::<(), _>("/ron-api/steps/order", ids).await;
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -497,7 +520,9 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
|
|||
current_action = action.value();
|
||||
let action = action.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>(&format!("/step/{step_id}/action"), action).await;
|
||||
let _ =
|
||||
ron_request::patch::<(), _>(&format!("/ron-api/step/{step_id}/action"), action)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -522,7 +547,8 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
|
|||
.await
|
||||
.is_some()
|
||||
{
|
||||
let _ = request::delete::<(), ()>(&format!("step/{step_id}"), None).await;
|
||||
let _ =
|
||||
ron_request::delete::<(), ()>(&format!("/ron-api/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();
|
||||
|
|
@ -535,12 +561,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 id: i64 = request::post::<_, ()>(&format!("step/{step_id}/ingredient"), None)
|
||||
.await
|
||||
.unwrap();
|
||||
let id: i64 =
|
||||
ron_request::post::<_, ()>(&format!("/ron-api/step/{step_id}/ingredient"), None)
|
||||
.await
|
||||
.unwrap();
|
||||
create_ingredient_element(
|
||||
&selector::<Element>(&format!("#step-{} .ingredients", step_id)),
|
||||
&ron_api::Ingredient {
|
||||
&web_api::Ingredient {
|
||||
id,
|
||||
name: "".to_string(),
|
||||
comment: "".to_string(),
|
||||
|
|
@ -555,7 +582,7 @@ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element
|
|||
step_element
|
||||
}
|
||||
|
||||
fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingredient) -> Element {
|
||||
fn create_ingredient_element(step_element: &Element, ingredient: &web_api::Ingredient) -> Element {
|
||||
let ingredient_id = ingredient.id;
|
||||
let ingredient_element: Element = selector_and_clone("#hidden-templates .ingredient");
|
||||
ingredient_element.set_id(&format!("ingredient-{}", ingredient.id));
|
||||
|
|
@ -572,7 +599,7 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
.map(|e| e.id()[11..].parse::<i64>().unwrap())
|
||||
.collect();
|
||||
|
||||
let _ = request::patch::<(), _>("ingredients/order", ids).await;
|
||||
let _ = ron_request::patch::<(), _>("/ron-api/ingredients/order", ids).await;
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -585,8 +612,11 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
current_name = name.value();
|
||||
let name = name.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>(&format!("ingredient/{ingredient_id}/name"), name)
|
||||
.await;
|
||||
let _ = ron_request::patch::<(), _>(
|
||||
&format!("/ron-api/ingredient/{ingredient_id}/name"),
|
||||
name,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -601,8 +631,8 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
current_comment = comment.value();
|
||||
let comment = comment.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>(
|
||||
&format!("ingredient/{ingredient_id}/comment"),
|
||||
let _ = ron_request::patch::<(), _>(
|
||||
&format!("/ron-api/ingredient/{ingredient_id}/comment"),
|
||||
comment,
|
||||
)
|
||||
.await;
|
||||
|
|
@ -628,8 +658,11 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
let q = if n.is_nan() { None } else { Some(n) };
|
||||
current_quantity = n;
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>(&format!("ingredient/{ingredient_id}/quantity"), q)
|
||||
.await;
|
||||
let _ = ron_request::patch::<(), _>(
|
||||
&format!("/ron-api/ingredient/{ingredient_id}/quantity"),
|
||||
q,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -644,8 +677,11 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
current_unit = unit.value();
|
||||
let unit = unit.value();
|
||||
spawn_local(async move {
|
||||
let _ = request::patch::<(), _>(&format!("ingredient/{ingredient_id}/unit"), unit)
|
||||
.await;
|
||||
let _ = ron_request::patch::<(), _>(
|
||||
&format!("/ron-api/ingredient/{ingredient_id}/unit"),
|
||||
unit,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
})
|
||||
|
|
@ -670,8 +706,11 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
.await
|
||||
.is_some()
|
||||
{
|
||||
let _ =
|
||||
request::delete::<(), ()>(&format!("ingredient/{ingredient_id}"), None).await;
|
||||
let _ = ron_request::delete::<(), ()>(
|
||||
&format!("/ron-api/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();
|
||||
|
|
@ -684,19 +723,17 @@ fn create_ingredient_element(step_element: &Element, ingredient: &ron_api::Ingre
|
|||
}
|
||||
|
||||
async fn reload_recipes_list(current_recipe_id: i64) {
|
||||
match Request::get("/fragments/recipes_list")
|
||||
.query([("current_recipe_id", current_recipe_id.to_string())])
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Err(error) => {
|
||||
toast::show_message_level(Level::Error, &format!("Internal server error: {}", error));
|
||||
}
|
||||
Ok(response) => {
|
||||
let list = document().get_element_by_id("recipes-list").unwrap();
|
||||
list.set_outer_html(&response.text().await.unwrap());
|
||||
}
|
||||
}
|
||||
let fragment: String = request::get_with_params(
|
||||
"/fragments/recipes_list",
|
||||
web_api::RecipesListFragmentsParams {
|
||||
current_recipe_id: Some(current_recipe_id),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let list = document().get_element_by_id("recipes-list").unwrap();
|
||||
list.set_outer_html(&fragment);
|
||||
}
|
||||
|
||||
enum CursorPosition {
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
use chrono::{Datelike, Days, Months, NaiveDate};
|
||||
use common::ron_api;
|
||||
use common::web_api;
|
||||
use gloo::storage::{LocalStorage, Storage};
|
||||
use ron::ser::{PrettyConfig, to_string_pretty};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{calendar, request};
|
||||
use crate::{calendar, ron_request};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Request error: {0}")]
|
||||
Request(#[from] request::Error),
|
||||
Request(#[from] ron_request::Error),
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
|
@ -25,11 +25,11 @@ pub enum ScheduleRecipeResult {
|
|||
RecipeAlreadyScheduledAtThisDate,
|
||||
}
|
||||
|
||||
impl From<ron_api::ScheduleRecipeResult> for ScheduleRecipeResult {
|
||||
fn from(api_res: ron_api::ScheduleRecipeResult) -> Self {
|
||||
impl From<web_api::ScheduleRecipeResult> for ScheduleRecipeResult {
|
||||
fn from(api_res: web_api::ScheduleRecipeResult) -> Self {
|
||||
match api_res {
|
||||
ron_api::ScheduleRecipeResult::Ok => Self::Ok,
|
||||
ron_api::ScheduleRecipeResult::RecipeAlreadyScheduledAtThisDate => {
|
||||
web_api::ScheduleRecipeResult::Ok => Self::Ok,
|
||||
web_api::ScheduleRecipeResult::RecipeAlreadyScheduledAtThisDate => {
|
||||
Self::RecipeAlreadyScheduledAtThisDate
|
||||
}
|
||||
}
|
||||
|
|
@ -80,12 +80,14 @@ impl RecipeScheduler {
|
|||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let titles: Vec<String> = request::get_with_params(
|
||||
"recipe/titles",
|
||||
recipe_ids_and_dates
|
||||
.iter()
|
||||
.map(|r| r.recipe_id)
|
||||
.collect::<Vec<_>>(),
|
||||
let titles: Vec<String> = ron_request::get_with_params(
|
||||
"/ron-api/recipe/titles",
|
||||
web_api::GetTitlesParams {
|
||||
ids: recipe_ids_and_dates
|
||||
.iter()
|
||||
.map(|r| r.recipe_id)
|
||||
.collect::<Vec<_>>(),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
|
|
@ -95,9 +97,9 @@ impl RecipeScheduler {
|
|||
.map(|(id_and_date, title)| (id_and_date.date, title, id_and_date.recipe_id))
|
||||
.collect::<Vec<_>>())
|
||||
} else {
|
||||
let scheduled_recipes: ron_api::ScheduledRecipes = request::get_with_params(
|
||||
"calendar/scheduled_recipes",
|
||||
ron_api::DateRange {
|
||||
let scheduled_recipes: web_api::ScheduledRecipes = ron_request::get_with_params(
|
||||
"/ron-api/calendar/scheduled_recipes",
|
||||
web_api::DateRange {
|
||||
start_date,
|
||||
end_date,
|
||||
},
|
||||
|
|
@ -127,9 +129,9 @@ impl RecipeScheduler {
|
|||
save_scheduled_recipes(recipe_ids_and_dates, date.year(), date.month0());
|
||||
Ok(ScheduleRecipeResult::Ok)
|
||||
} else {
|
||||
request::post::<ron_api::ScheduleRecipeResult, _>(
|
||||
"calendar/scheduled_recipe",
|
||||
Some(ron_api::ScheduleRecipe {
|
||||
ron_request::post::<web_api::ScheduleRecipeResult, _>(
|
||||
"/ron-api/calendar/scheduled_recipe",
|
||||
Some(web_api::ScheduleRecipe {
|
||||
recipe_id,
|
||||
date,
|
||||
servings,
|
||||
|
|
@ -138,7 +140,7 @@ impl RecipeScheduler {
|
|||
)
|
||||
.await
|
||||
.map_err(Error::from)
|
||||
.map(From::<ron_api::ScheduleRecipeResult>::from)
|
||||
.map(From::<web_api::ScheduleRecipeResult>::from)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,7 @@
|
|||
/// This module provides a simple API for making HTTP requests to the server.
|
||||
/// For requests with a body (POST, PUT, PATCH, etc.), it uses the RON format.
|
||||
/// The RON data structures should come from the `ron_api` module.
|
||||
/// For requests with parameters (GET), it uses the HTML form format.
|
||||
use common::ron_api;
|
||||
use gloo::net::http::{Request, RequestBuilder};
|
||||
use serde::{Serialize, de::DeserializeOwned};
|
||||
use std::any::TypeId;
|
||||
|
||||
use gloo::net::http::Request;
|
||||
use serde::Serialize;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::toast::{self, Level};
|
||||
|
|
@ -14,12 +11,6 @@ pub enum Error {
|
|||
#[error("Gloo error: {0}")]
|
||||
Gloo(#[from] gloo::net::Error),
|
||||
|
||||
#[error("RON Spanned error: {0}")]
|
||||
RonSpanned(#[from] ron::error::SpannedError),
|
||||
|
||||
#[error("RON Error: {0}")]
|
||||
Ron(#[from] ron::error::Error),
|
||||
|
||||
#[error("HTTP error: {0}")]
|
||||
Http(String),
|
||||
|
||||
|
|
@ -29,53 +20,26 @@ pub enum Error {
|
|||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
const CONTENT_TYPE: &str = "Content-Type";
|
||||
|
||||
async fn req_with_body<T, U>(
|
||||
api_name: &str,
|
||||
body: U,
|
||||
method_fn: fn(&str) -> RequestBuilder,
|
||||
) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
let url = format!("/ron-api/{}", api_name);
|
||||
let request_builder = method_fn(&url).header(
|
||||
CONTENT_TYPE,
|
||||
common::consts::MIME_TYPE_RON.to_str().unwrap(),
|
||||
);
|
||||
send_req(request_builder.body(ron_api::to_string(body)?)?).await
|
||||
#[allow(dead_code)] // Not used for the moment.
|
||||
pub async fn get(url: &str) -> Result<String> {
|
||||
get_with_params(url, ()).await
|
||||
}
|
||||
|
||||
async fn req_with_params<T, U>(
|
||||
api_name: &str,
|
||||
params: U,
|
||||
method_fn: fn(&str) -> RequestBuilder,
|
||||
) -> Result<T>
|
||||
pub async fn get_with_params<U>(url: &str, params: U) -> Result<String>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
U: Serialize + 'static,
|
||||
{
|
||||
let mut url = format!("/ron-api/{}?", api_name);
|
||||
serde_html_form::ser::push_to_string(&mut url, params).unwrap();
|
||||
let request_builder = method_fn(&url);
|
||||
send_req(request_builder.build()?).await
|
||||
}
|
||||
let request_builder = if TypeId::of::<U>() == TypeId::of::<()>() {
|
||||
Request::get(url)
|
||||
} else {
|
||||
let mut url = url.to_string();
|
||||
url.push('?');
|
||||
serde_html_form::ser::push_to_string(&mut url, params).unwrap();
|
||||
Request::get(&url)
|
||||
};
|
||||
|
||||
async fn req<T>(api_name: &str, method_fn: fn(&str) -> RequestBuilder) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let url = format!("/ron-api/{}", api_name);
|
||||
let request_builder = method_fn(&url);
|
||||
send_req(request_builder.build()?).await
|
||||
}
|
||||
let request = request_builder.build()?;
|
||||
|
||||
async fn send_req<T>(request: Request) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
match request.send().await {
|
||||
Err(error) => {
|
||||
toast::show_message_level(Level::Error, &format!("Internal server error: {}", error));
|
||||
|
|
@ -89,73 +53,8 @@ where
|
|||
);
|
||||
Err(Error::Http(response.status_text()))
|
||||
} else {
|
||||
let mut r = response.binary().await?;
|
||||
// An empty response is considered to be an unit value.
|
||||
if r.is_empty() {
|
||||
r = b"()".to_vec();
|
||||
}
|
||||
Ok(ron::de::from_bytes::<T>(&r)?)
|
||||
Ok(response.text().await?)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sends a request to the server with the given API name and body.
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// use common::ron_api;
|
||||
/// let body = ron_api::SetLang { lang : "en".to_string() };
|
||||
/// request::put::<(), _>("lang", body).await;
|
||||
/// ```
|
||||
pub async fn put<T, U>(api_name: &str, body: U) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
req_with_body(api_name, body, Request::put).await
|
||||
}
|
||||
|
||||
pub async fn patch<T, U>(api_name: &str, body: U) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
req_with_body(api_name, body, Request::patch).await
|
||||
}
|
||||
|
||||
pub async fn post<T, U>(api_name: &str, body: Option<U>) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
match body {
|
||||
Some(body) => req_with_body(api_name, body, Request::post).await,
|
||||
None => req(api_name, Request::post).await,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete<T, U>(api_name: &str, body: Option<U>) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
match body {
|
||||
Some(body) => req_with_body(api_name, body, Request::delete).await,
|
||||
None => req(api_name, Request::delete).await,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get<T>(api_name: &str) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
req(api_name, Request::get).await
|
||||
}
|
||||
|
||||
pub async fn get_with_params<T, U>(api_name: &str, params: U) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
req_with_params(api_name, params, Request::get).await
|
||||
}
|
||||
|
|
|
|||
156
frontend/src/ron_request.rs
Normal file
156
frontend/src/ron_request.rs
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/// This module provides a simple API for making HTTP requests to the server.
|
||||
/// For requests with a body (POST, PUT, PATCH, etc.), it uses the RON format.
|
||||
/// The RON data structures should come from the `web_api` module.
|
||||
/// For requests with parameters (GET), it uses the HTML form format.
|
||||
use common::web_api;
|
||||
use gloo::net::http::{Request, RequestBuilder};
|
||||
use serde::{Serialize, de::DeserializeOwned};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::toast::{self, Level};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Gloo error: {0}")]
|
||||
Gloo(#[from] gloo::net::Error),
|
||||
|
||||
#[error("RON Spanned error: {0}")]
|
||||
RonSpanned(#[from] ron::error::SpannedError),
|
||||
|
||||
#[error("RON Error: {0}")]
|
||||
Ron(#[from] ron::error::Error),
|
||||
|
||||
#[error("HTTP error: {0}")]
|
||||
Http(String),
|
||||
|
||||
#[error("Unknown error: {0}")]
|
||||
Other(String),
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
const CONTENT_TYPE: &str = "Content-Type"; // TODO: take it from the http crate.
|
||||
|
||||
async fn req_with_body<T, U>(url: &str, body: U, method_fn: fn(&str) -> RequestBuilder) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
let request_builder = method_fn(url).header(
|
||||
CONTENT_TYPE,
|
||||
common::consts::MIME_TYPE_RON.to_str().unwrap(),
|
||||
);
|
||||
send_req(request_builder.body(web_api::to_string(body)?)?).await
|
||||
}
|
||||
|
||||
async fn req_with_params<T, U>(
|
||||
url: &str,
|
||||
params: U,
|
||||
method_fn: fn(&str) -> RequestBuilder,
|
||||
) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
let mut url = url.to_string();
|
||||
url.push('?');
|
||||
serde_html_form::ser::push_to_string(&mut url, params).unwrap();
|
||||
let request_builder = method_fn(&url);
|
||||
send_req(request_builder.build()?).await
|
||||
}
|
||||
|
||||
async fn req<T>(url: &str, method_fn: fn(&str) -> RequestBuilder) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let request_builder = method_fn(url);
|
||||
send_req(request_builder.build()?).await
|
||||
}
|
||||
|
||||
async fn send_req<T>(request: Request) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
match request.send().await {
|
||||
Err(error) => {
|
||||
toast::show_message_level(Level::Error, &format!("Internal server error: {}", error));
|
||||
Err(Error::Gloo(error))
|
||||
}
|
||||
Ok(response) => {
|
||||
if !response.ok() {
|
||||
toast::show_message_level(
|
||||
Level::Error,
|
||||
&format!("HTTP error: {}", response.status_text()),
|
||||
);
|
||||
Err(Error::Http(response.status_text()))
|
||||
} else {
|
||||
let mut r = response.binary().await?;
|
||||
// An empty response is considered to be an unit value.
|
||||
if r.is_empty() {
|
||||
r = b"()".to_vec();
|
||||
}
|
||||
Ok(ron::de::from_bytes::<T>(&r)?)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sends a request to the server with the given API name and body.
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// use common::web_api;
|
||||
/// let body = web_api::SetLang { lang : "en".to_string() };
|
||||
/// request::put::<(), _>("lang", body).await;
|
||||
/// ```
|
||||
pub async fn put<T, U>(url: &str, body: U) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
req_with_body(url, body, Request::put).await
|
||||
}
|
||||
|
||||
pub async fn patch<T, U>(url: &str, body: U) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
req_with_body(url, body, Request::patch).await
|
||||
}
|
||||
|
||||
pub async fn post<T, U>(url: &str, body: Option<U>) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
match body {
|
||||
Some(body) => req_with_body(url, body, Request::post).await,
|
||||
None => req(url, Request::post).await,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete<T, U>(url: &str, body: Option<U>) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
match body {
|
||||
Some(body) => req_with_body(url, body, Request::delete).await,
|
||||
None => req(url, Request::delete).await,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get<T>(url: &str) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
req(url, Request::get).await
|
||||
}
|
||||
|
||||
pub async fn get_with_params<T, U>(url: &str, params: U) -> Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
U: Serialize,
|
||||
{
|
||||
req_with_params(url, params, Request::get).await
|
||||
}
|
||||
|
|
@ -1,16 +1,16 @@
|
|||
use chrono::{Datelike, Days, Months, NaiveDate};
|
||||
use common::ron_api;
|
||||
use common::web_api;
|
||||
use gloo::storage::{LocalStorage, Storage};
|
||||
use ron::ser::{PrettyConfig, to_string_pretty};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{calendar, request};
|
||||
use crate::{calendar, ron_request};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Request error: {0}")]
|
||||
Request(#[from] request::Error),
|
||||
Request(#[from] ron_request::Error),
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
|
@ -25,11 +25,11 @@ impl ShoppingList {
|
|||
Self { is_local }
|
||||
}
|
||||
|
||||
pub async fn get_items(&self) -> Result<Vec<ron_api::ShoppingListItem>> {
|
||||
pub async fn get_items(&self) -> Result<Vec<web_api::ShoppingListItem>> {
|
||||
if self.is_local {
|
||||
Ok(vec![]) // TODO
|
||||
} else {
|
||||
Ok(request::get("shopping_list").await?)
|
||||
Ok(ron_request::get("/ron-api/shopping_list").await?)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -37,9 +37,9 @@ impl ShoppingList {
|
|||
if self.is_local {
|
||||
todo!();
|
||||
} else {
|
||||
request::patch(
|
||||
"shopping_list/checked",
|
||||
ron_api::KeyValue {
|
||||
ron_request::patch(
|
||||
"/ron-api/shopping_list/checked",
|
||||
web_api::KeyValue {
|
||||
id: item_id,
|
||||
value: is_checked,
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue