From bfb697e3b934d70c5d550b89bfcdbe0f5809e189 Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Sun, 5 Nov 2023 11:32:59 -0800 Subject: [PATCH] handle hx-request for home page TODO: I want to handle fragments from rust, not the ructe compilation --- src/api.rs | 16 +++++++++++----- src/lib/hx_request.rs | 2 +- src/main.rs | 6 +++--- templates/home.rs.html | 12 +++++++----- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/api.rs b/src/api.rs index d22f0cc..a3cc449 100644 --- a/src/api.rs +++ b/src/api.rs @@ -8,6 +8,7 @@ use axum::{ Form, }; use cm_lib::{ + hx_request::HxRequest, models::{NewDrink, Shift}, schema::shifts, }; @@ -28,7 +29,7 @@ pub(crate) fn router() -> axum::Router { .route("/ada/updates", get(ada_subscribe)) } -async fn open_shift(State(state): State) -> impl IntoResponse { +async fn open_shift(State(state): State, HxRequest(hx): HxRequest) -> impl IntoResponse { let shift = { let mut conn = state.connection.get().await.unwrap(); conn.transaction(|conn| { @@ -53,10 +54,14 @@ async fn open_shift(State(state): State) -> impl IntoResponse { .unwrap() }; - render!(crate::templates::components::home_fragment_html, shift) + render!(crate::templates::home_html, shift, !hx) } -async fn close_shift(State(state): State, Path(id): Path) -> impl IntoResponse { +async fn close_shift( + State(state): State, + Path(id): Path, + HxRequest(hx): HxRequest, +) -> impl IntoResponse { let mut conn = state.connection.get().await.unwrap(); diesel::update(shifts::table.filter(shifts::id.eq(id))) .set(shifts::end.eq(Some(chrono::Utc::now().naive_local()))) @@ -64,7 +69,7 @@ async fn close_shift(State(state): State, Path(id): Path) -> impl .await .unwrap(); - render!(crate::templates::home_html, None) + render!(crate::templates::home_html, None, !hx) } #[derive(Deserialize, Debug)] @@ -86,6 +91,7 @@ impl From for NewDrink { async fn add_drink( State(state): State, + HxRequest(hx): HxRequest, Form(form): Form, ) -> impl IntoResponse { let mut conn = state.connection.get().await.unwrap(); @@ -118,7 +124,7 @@ async fn add_drink( headers.insert("HX-Push-Url", "/".parse().unwrap()); ( headers, - render!(crate::templates::home_html, Some(open_shift)), + render!(crate::templates::home_html, Some(open_shift), !hx), ) } diff --git a/src/lib/hx_request.rs b/src/lib/hx_request.rs index 47fb61c..925bc11 100644 --- a/src/lib/hx_request.rs +++ b/src/lib/hx_request.rs @@ -5,7 +5,7 @@ use axum::{async_trait, extract::FromRequestParts, http::request::Parts}; const HX_REQUEST: &str = "HX-Request"; #[derive(Debug, Copy, Clone)] -pub struct HxRequest(bool); +pub struct HxRequest(pub bool); #[async_trait] impl FromRequestParts for HxRequest diff --git a/src/main.rs b/src/main.rs index 1b00527..3c08785 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use axum_ructe::render; use cm_lib::{ models::{Drink, Shift}, report::GenerateDrinkReport, - schema::shifts, + schema::shifts, hx_request::HxRequest, }; use axum::{ @@ -113,7 +113,7 @@ async fn main() { .unwrap(); } -async fn root(State(state): State) -> impl IntoResponse { +async fn root(State(state): State, HxRequest(hx): HxRequest) -> impl IntoResponse { let mut conn = state.connection.get().await.unwrap(); let open_shift: Option = { @@ -130,7 +130,7 @@ async fn root(State(state): State) -> impl IntoResponse { tracing::debug!("{open_shift:?}"); - render!(templates::home_html, open_shift) + render!(templates::home_html, open_shift, !hx) } async fn drinks(Path(id): Path) -> impl IntoResponse { diff --git a/templates/home.rs.html b/templates/home.rs.html index 0a97491..bc576fa 100644 --- a/templates/home.rs.html +++ b/templates/home.rs.html @@ -2,10 +2,12 @@ @use super::components::home_fragment_html; @use cm_lib::models::Shift; -@(open_shift: Option) - -@:base_html({ +@(open_shift: Option, include_base: bool) +@if include_base { + @:base_html({ + @:home_fragment_html(open_shift) + }) +} else { @:home_fragment_html(open_shift) - -}) +}