From eaf558a524c582d1f56684491e67cf7813690c07 Mon Sep 17 00:00:00 2001 From: Jon Seager Date: Sun, 8 Feb 2026 16:10:11 +0000 Subject: [PATCH] feat(routes): redirect to detail page after creating brews and cups Update create_brew, create_cup, and submit_checkin to navigate to the new detail pages instead of reloading or redirecting to home. --- src/application/routes/api/brews.rs | 10 ++++++---- src/application/routes/api/checkin.rs | 15 +++++++++++++-- src/application/routes/api/cups.rs | 21 ++++++++++++++------- templates/pages/checkin.html | 2 +- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/application/routes/api/brews.rs b/src/application/routes/api/brews.rs index 808a5c0..13fe1e9 100644 --- a/src/application/routes/api/brews.rs +++ b/src/application/routes/api/brews.rs @@ -254,9 +254,11 @@ pub(crate) async fn create_brew( info!(brew_id = %enriched.brew.id, "brew created"); state.stats_invalidator.invalidate(); + let detail_url = format!("/brews/{}", enriched.brew.id); + if is_datastar_request(&headers) { // If the request came from a page that has #brew-list, return the updated fragment. - // Otherwise (homepage, timeline, etc.), trigger a page reload instead. + // Otherwise (homepage, add page, etc.), redirect to the brew detail page. let from_brew_page = headers .get("referer") .and_then(|v| v.to_str().ok()) @@ -268,8 +270,8 @@ pub(crate) async fn create_brew( .map_err(ApiError::from) } else { use axum::http::header::HeaderValue; - let mut response = - axum::response::Html("").into_response(); + let script = format!(""); + let mut response = axum::response::Html(script).into_response(); response .headers_mut() .insert("datastar-selector", HeaderValue::from_static("body")); @@ -279,7 +281,7 @@ pub(crate) async fn create_brew( Ok(response) } } else if matches!(source, PayloadSource::Form) { - Ok(Redirect::to("/").into_response()) + Ok(Redirect::to(&detail_url).into_response()) } else { Ok((StatusCode::CREATED, Json(enriched)).into_response()) } diff --git a/src/application/routes/api/checkin.rs b/src/application/routes/api/checkin.rs index f3747cf..702513c 100644 --- a/src/application/routes/api/checkin.rs +++ b/src/application/routes/api/checkin.rs @@ -89,10 +89,21 @@ pub(crate) async fn submit_checkin( .await .map_err(AppError::from)?; + let detail_url = format!("/cups/{}", cup.id); + if is_datastar_request(&headers) { - crate::application::routes::support::render_signals_json(&[]).map_err(ApiError::from) + use axum::http::header::HeaderValue; + let script = format!(""); + let mut response = axum::response::Html(script).into_response(); + response + .headers_mut() + .insert("datastar-selector", HeaderValue::from_static("body")); + response + .headers_mut() + .insert("datastar-mode", HeaderValue::from_static("append")); + Ok(response) } else if matches!(source, PayloadSource::Form) { - Ok(Redirect::to("/").into_response()) + Ok(Redirect::to(&detail_url).into_response()) } else { Ok((StatusCode::CREATED, Json(cup)).into_response()) } diff --git a/src/application/routes/api/cups.rs b/src/application/routes/api/cups.rs index 222c030..8ba495b 100644 --- a/src/application/routes/api/cups.rs +++ b/src/application/routes/api/cups.rs @@ -52,7 +52,7 @@ pub(crate) async fn create_cup( Query(query): Query, payload: FlexiblePayload, ) -> Result { - let (request, search) = query.into_request_and_search::(); + let (_request, _search) = query.into_request_and_search::(); let (new_cup, source) = payload.into_parts(); let cup = state @@ -64,14 +64,21 @@ pub(crate) async fn create_cup( info!(cup_id = %cup.id, "cup created"); state.stats_invalidator.invalidate(); + let detail_url = format!("/cups/{}", cup.id); + if is_datastar_request(&headers) { - render_cup_list_fragment(state, request, search, true) - .await - .map_err(ApiError::from) + use axum::http::header::HeaderValue; + let script = format!(""); + let mut response = axum::response::Html(script).into_response(); + response + .headers_mut() + .insert("datastar-selector", HeaderValue::from_static("body")); + response + .headers_mut() + .insert("datastar-mode", HeaderValue::from_static("append")); + Ok(response) } else if matches!(source, PayloadSource::Form) { - let target = - ListNavigator::new(CUP_PAGE_PATH, CUP_FRAGMENT_PATH, request, search).page_href(1); - Ok(Redirect::to(&target).into_response()) + Ok(Redirect::to(&detail_url).into_response()) } else { Ok((StatusCode::CREATED, Json(cup)).into_response()) } diff --git a/templates/pages/checkin.html b/templates/pages/checkin.html index afc4615..ca62a9b 100644 --- a/templates/pages/checkin.html +++ b/templates/pages/checkin.html @@ -304,7 +304,7 @@ block title %}Brewlog ยท Check In{% endblock %} {% block head %}