From 71c2611d6389c991103a51e64086b06c2907081f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Tue, 29 Aug 2023 21:34:00 +0200 Subject: [PATCH] more htmx --- rust/src/components/inventory.rs | 26 +++++----- rust/src/components/trip/mod.rs | 2 - rust/src/main.rs | 81 ++++++++++++++++++++++++++------ 3 files changed, 78 insertions(+), 31 deletions(-) diff --git a/rust/src/components/inventory.rs b/rust/src/components/inventory.rs index 12c4e1a..e1c8ef2 100644 --- a/rust/src/components/inventory.rs +++ b/rust/src/components/inventory.rs @@ -87,20 +87,17 @@ impl InventoryCategoryList { } { a id="select-category" - href=( - format!( - "/inventory/category/{id}/", - id=category.id - ) - ) - // hx-post=( - // format!( - // "/inventory/category/{id}/items", - // id=category.id - // ) - // ) - // hx-swap="outerHTML" - // hx-target="#items" + href={ + "/inventory/category/" + (category.id) + } + hx-post={ + "/inventory/categories/" + (category.id) + "/select" + } + hx-swap="outerHTML" + hx-target="#pkglist-item-manager" ."inline-block" ."p-2" ."m-0" ."w-full" { (category.name.clone()) @@ -531,6 +528,7 @@ impl InventoryNewCategoryForm { name="new-category" id="new-category" action="/inventory/category/" + hx-boost="true" target="_self" method="post" ."p-5" ."border-2" ."border-gray-200" { diff --git a/rust/src/components/trip/mod.rs b/rust/src/components/trip/mod.rs index c451727..876c898 100644 --- a/rust/src/components/trip/mod.rs +++ b/rust/src/components/trip/mod.rs @@ -1036,7 +1036,6 @@ impl TripItemListRow { ."flex" ."bg-green-200"[item.picked] ."hover:bg-green-100"[!item.picked] - ."hover:bg-red-100"[item.picked] { @if item.picked { span @@ -1078,7 +1077,6 @@ impl TripItemListRow { ."flex" ."bg-green-200"[item.packed] ."hover:bg-green-100"[!item.packed] - ."hover:bg-red-100"[item.packed] { @if item.packed { span diff --git a/rust/src/main.rs b/rust/src/main.rs index 3516739..63a7e3c 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -104,12 +104,14 @@ impl HtmxEvents { enum HtmxResponseHeaders { Trigger, + PushUrl, } impl Into for HtmxResponseHeaders { fn into(self) -> HeaderName { match self { Self::Trigger => HeaderName::from_static("hx-trigger"), + Self::PushUrl => HeaderName::from_static("hx-push-url"), } } } @@ -199,6 +201,7 @@ async fn main() -> Result<(), sqlx::Error> { .route("/", get(inventory_inactive)) .route("/category/", post(inventory_category_create)) .route("/item/:id/", get(inventory_item)) + .route("/categories/:id/select", post(inventory_category_select)) .route("/item/", post(inventory_item_create)) .route("/item/name/validate", post(inventory_item_validate_name)) .route("/category/:id/", get(inventory_active)) @@ -479,20 +482,18 @@ async fn inventory_item_create( // it's impossible to NOT find the item here, as we literally just added // it. but good error handling never hurts - let active_category: Option<&Category> = state - .client_state - .active_category_id - .map(|id| { - inventory - .categories - .iter() - .find(|category| category.id == id) - .ok_or(( - StatusCode::NOT_FOUND, - ErrorPage::build(&format!("a category with id {id} was inserted but does not exist, this is a bug")), - )) - }) - .transpose()?; + let active_category: Option<&Category> = Some( + inventory + .categories + .iter() + .find(|category| category.id == new_item.category_id) + .ok_or(( + StatusCode::NOT_FOUND, + ErrorPage::build(&format!( + "a category with id {id} was inserted but does not exist, this is a bug" + )), + ))?, + ); Ok(( StatusCode::OK, @@ -1704,7 +1705,7 @@ async fn inventory_item( async fn trip_category_select( State(state): State, Path((trip_id, category_id)): Path<(Uuid, Uuid)>, -) -> Result<(StatusCode, Markup), (StatusCode, Markup)> { +) -> Result<(StatusCode, HeaderMap, Markup), (StatusCode, Markup)> { let mut trip = models::Trip::find(&state.database_pool, trip_id) .await .map_err(|e| { @@ -1736,8 +1737,58 @@ async fn trip_category_select( ErrorPage::build(&format!("category with id {category_id} not found")), ))?; + let mut headers = HeaderMap::new(); + headers.insert::( + HtmxResponseHeaders::PushUrl.into(), + format!("?={category_id}").parse().unwrap(), + ); + Ok(( StatusCode::OK, + headers, components::trip::TripItems::build(Some(&active_category), &trip), )) } + +async fn inventory_category_select( + State(state): State, + Path(category_id): Path, +) -> Result<(StatusCode, HeaderMap, Markup), (StatusCode, Markup)> { + let inventory = models::Inventory::load(&state.database_pool) + .await + .map_err(|error| { + ( + StatusCode::INTERNAL_SERVER_ERROR, + ErrorPage::build(&error.to_string()), + ) + })?; + + let active_category: Option<&Category> = Some( + inventory + .categories + .iter() + .find(|category| category.id == category_id) + .ok_or(( + StatusCode::NOT_FOUND, + ErrorPage::build(&format!("a category with id {category_id} not found")), + ))?, + ); + + let mut headers = HeaderMap::new(); + headers.insert::( + HtmxResponseHeaders::PushUrl.into(), + format!("/inventory/category/{category_id}") + .parse() + .unwrap(), + ); + + Ok(( + StatusCode::OK, + headers, + components::Inventory::build( + active_category, + &inventory.categories, + state.client_state.edit_item, + ), + )) +}