more htmx
This commit is contained in:
@@ -87,20 +87,17 @@ impl InventoryCategoryList {
|
|||||||
} {
|
} {
|
||||||
a
|
a
|
||||||
id="select-category"
|
id="select-category"
|
||||||
href=(
|
href={
|
||||||
format!(
|
"/inventory/category/"
|
||||||
"/inventory/category/{id}/",
|
(category.id)
|
||||||
id=category.id
|
}
|
||||||
)
|
hx-post={
|
||||||
)
|
"/inventory/categories/"
|
||||||
// hx-post=(
|
(category.id)
|
||||||
// format!(
|
"/select"
|
||||||
// "/inventory/category/{id}/items",
|
}
|
||||||
// id=category.id
|
hx-swap="outerHTML"
|
||||||
// )
|
hx-target="#pkglist-item-manager"
|
||||||
// )
|
|
||||||
// hx-swap="outerHTML"
|
|
||||||
// hx-target="#items"
|
|
||||||
."inline-block" ."p-2" ."m-0" ."w-full"
|
."inline-block" ."p-2" ."m-0" ."w-full"
|
||||||
{
|
{
|
||||||
(category.name.clone())
|
(category.name.clone())
|
||||||
@@ -531,6 +528,7 @@ impl InventoryNewCategoryForm {
|
|||||||
name="new-category"
|
name="new-category"
|
||||||
id="new-category"
|
id="new-category"
|
||||||
action="/inventory/category/"
|
action="/inventory/category/"
|
||||||
|
hx-boost="true"
|
||||||
target="_self"
|
target="_self"
|
||||||
method="post"
|
method="post"
|
||||||
."p-5" ."border-2" ."border-gray-200" {
|
."p-5" ."border-2" ."border-gray-200" {
|
||||||
|
|||||||
@@ -1036,7 +1036,6 @@ impl TripItemListRow {
|
|||||||
."flex"
|
."flex"
|
||||||
."bg-green-200"[item.picked]
|
."bg-green-200"[item.picked]
|
||||||
."hover:bg-green-100"[!item.picked]
|
."hover:bg-green-100"[!item.picked]
|
||||||
."hover:bg-red-100"[item.picked]
|
|
||||||
{
|
{
|
||||||
@if item.picked {
|
@if item.picked {
|
||||||
span
|
span
|
||||||
@@ -1078,7 +1077,6 @@ impl TripItemListRow {
|
|||||||
."flex"
|
."flex"
|
||||||
."bg-green-200"[item.packed]
|
."bg-green-200"[item.packed]
|
||||||
."hover:bg-green-100"[!item.packed]
|
."hover:bg-green-100"[!item.packed]
|
||||||
."hover:bg-red-100"[item.packed]
|
|
||||||
{
|
{
|
||||||
@if item.packed {
|
@if item.packed {
|
||||||
span
|
span
|
||||||
|
|||||||
@@ -104,12 +104,14 @@ impl HtmxEvents {
|
|||||||
|
|
||||||
enum HtmxResponseHeaders {
|
enum HtmxResponseHeaders {
|
||||||
Trigger,
|
Trigger,
|
||||||
|
PushUrl,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<HeaderName> for HtmxResponseHeaders {
|
impl Into<HeaderName> for HtmxResponseHeaders {
|
||||||
fn into(self) -> HeaderName {
|
fn into(self) -> HeaderName {
|
||||||
match self {
|
match self {
|
||||||
Self::Trigger => HeaderName::from_static("hx-trigger"),
|
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("/", get(inventory_inactive))
|
||||||
.route("/category/", post(inventory_category_create))
|
.route("/category/", post(inventory_category_create))
|
||||||
.route("/item/:id/", get(inventory_item))
|
.route("/item/:id/", get(inventory_item))
|
||||||
|
.route("/categories/:id/select", post(inventory_category_select))
|
||||||
.route("/item/", post(inventory_item_create))
|
.route("/item/", post(inventory_item_create))
|
||||||
.route("/item/name/validate", post(inventory_item_validate_name))
|
.route("/item/name/validate", post(inventory_item_validate_name))
|
||||||
.route("/category/:id/", get(inventory_active))
|
.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's impossible to NOT find the item here, as we literally just added
|
||||||
// it. but good error handling never hurts
|
// it. but good error handling never hurts
|
||||||
let active_category: Option<&Category> = state
|
let active_category: Option<&Category> = Some(
|
||||||
.client_state
|
inventory
|
||||||
.active_category_id
|
.categories
|
||||||
.map(|id| {
|
.iter()
|
||||||
inventory
|
.find(|category| category.id == new_item.category_id)
|
||||||
.categories
|
.ok_or((
|
||||||
.iter()
|
StatusCode::NOT_FOUND,
|
||||||
.find(|category| category.id == id)
|
ErrorPage::build(&format!(
|
||||||
.ok_or((
|
"a category with id {id} was inserted but does not exist, this is a bug"
|
||||||
StatusCode::NOT_FOUND,
|
)),
|
||||||
ErrorPage::build(&format!("a category with id {id} was inserted but does not exist, this is a bug")),
|
))?,
|
||||||
))
|
);
|
||||||
})
|
|
||||||
.transpose()?;
|
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
@@ -1704,7 +1705,7 @@ async fn inventory_item(
|
|||||||
async fn trip_category_select(
|
async fn trip_category_select(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Path((trip_id, category_id)): Path<(Uuid, Uuid)>,
|
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)
|
let mut trip = models::Trip::find(&state.database_pool, trip_id)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
@@ -1736,8 +1737,58 @@ async fn trip_category_select(
|
|||||||
ErrorPage::build(&format!("category with id {category_id} not found")),
|
ErrorPage::build(&format!("category with id {category_id} not found")),
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert::<HeaderName>(
|
||||||
|
HtmxResponseHeaders::PushUrl.into(),
|
||||||
|
format!("?={category_id}").parse().unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
|
headers,
|
||||||
components::trip::TripItems::build(Some(&active_category), &trip),
|
components::trip::TripItems::build(Some(&active_category), &trip),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn inventory_category_select(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
Path(category_id): Path<Uuid>,
|
||||||
|
) -> 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::<HeaderName>(
|
||||||
|
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,
|
||||||
|
),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user