handle hx-request for home page

TODO: I want to handle fragments from rust, not the ructe compilation
main
Zynh0722 2023-11-05 11:32:59 -08:00
parent a4d6d79c16
commit bfb697e3b9
4 changed files with 22 additions and 14 deletions

View File

@ -8,6 +8,7 @@ use axum::{
Form, Form,
}; };
use cm_lib::{ use cm_lib::{
hx_request::HxRequest,
models::{NewDrink, Shift}, models::{NewDrink, Shift},
schema::shifts, schema::shifts,
}; };
@ -28,7 +29,7 @@ pub(crate) fn router() -> axum::Router<AppState> {
.route("/ada/updates", get(ada_subscribe)) .route("/ada/updates", get(ada_subscribe))
} }
async fn open_shift(State(state): State<AppState>) -> impl IntoResponse { async fn open_shift(State(state): State<AppState>, HxRequest(hx): HxRequest) -> impl IntoResponse {
let shift = { let shift = {
let mut conn = state.connection.get().await.unwrap(); let mut conn = state.connection.get().await.unwrap();
conn.transaction(|conn| { conn.transaction(|conn| {
@ -53,10 +54,14 @@ async fn open_shift(State(state): State<AppState>) -> impl IntoResponse {
.unwrap() .unwrap()
}; };
render!(crate::templates::components::home_fragment_html, shift) render!(crate::templates::home_html, shift, !hx)
} }
async fn close_shift(State(state): State<AppState>, Path(id): Path<u32>) -> impl IntoResponse { async fn close_shift(
State(state): State<AppState>,
Path(id): Path<u32>,
HxRequest(hx): HxRequest,
) -> impl IntoResponse {
let mut conn = state.connection.get().await.unwrap(); let mut conn = state.connection.get().await.unwrap();
diesel::update(shifts::table.filter(shifts::id.eq(id))) diesel::update(shifts::table.filter(shifts::id.eq(id)))
.set(shifts::end.eq(Some(chrono::Utc::now().naive_local()))) .set(shifts::end.eq(Some(chrono::Utc::now().naive_local())))
@ -64,7 +69,7 @@ async fn close_shift(State(state): State<AppState>, Path(id): Path<u32>) -> impl
.await .await
.unwrap(); .unwrap();
render!(crate::templates::home_html, None) render!(crate::templates::home_html, None, !hx)
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
@ -86,6 +91,7 @@ impl From<DrinkForm> for NewDrink {
async fn add_drink( async fn add_drink(
State(state): State<AppState>, State(state): State<AppState>,
HxRequest(hx): HxRequest,
Form(form): Form<DrinkForm>, Form(form): Form<DrinkForm>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let mut conn = state.connection.get().await.unwrap(); let mut conn = state.connection.get().await.unwrap();
@ -118,7 +124,7 @@ async fn add_drink(
headers.insert("HX-Push-Url", "/".parse().unwrap()); headers.insert("HX-Push-Url", "/".parse().unwrap());
( (
headers, headers,
render!(crate::templates::home_html, Some(open_shift)), render!(crate::templates::home_html, Some(open_shift), !hx),
) )
} }

View File

@ -5,7 +5,7 @@ use axum::{async_trait, extract::FromRequestParts, http::request::Parts};
const HX_REQUEST: &str = "HX-Request"; const HX_REQUEST: &str = "HX-Request";
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct HxRequest(bool); pub struct HxRequest(pub bool);
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for HxRequest impl<S> FromRequestParts<S> for HxRequest

View File

@ -8,7 +8,7 @@ use axum_ructe::render;
use cm_lib::{ use cm_lib::{
models::{Drink, Shift}, models::{Drink, Shift},
report::GenerateDrinkReport, report::GenerateDrinkReport,
schema::shifts, schema::shifts, hx_request::HxRequest,
}; };
use axum::{ use axum::{
@ -113,7 +113,7 @@ async fn main() {
.unwrap(); .unwrap();
} }
async fn root(State(state): State<AppState>) -> impl IntoResponse { async fn root(State(state): State<AppState>, HxRequest(hx): HxRequest) -> impl IntoResponse {
let mut conn = state.connection.get().await.unwrap(); let mut conn = state.connection.get().await.unwrap();
let open_shift: Option<Shift> = { let open_shift: Option<Shift> = {
@ -130,7 +130,7 @@ async fn root(State(state): State<AppState>) -> impl IntoResponse {
tracing::debug!("{open_shift:?}"); tracing::debug!("{open_shift:?}");
render!(templates::home_html, open_shift) render!(templates::home_html, open_shift, !hx)
} }
async fn drinks(Path(id): Path<u32>) -> impl IntoResponse { async fn drinks(Path(id): Path<u32>) -> impl IntoResponse {

View File

@ -2,10 +2,12 @@
@use super::components::home_fragment_html; @use super::components::home_fragment_html;
@use cm_lib::models::Shift; @use cm_lib::models::Shift;
@(open_shift: Option<Shift>) @(open_shift: Option<Shift>, include_base: bool)
@:base_html({
@if include_base {
@:base_html({
@:home_fragment_html(open_shift) @:home_fragment_html(open_shift)
})
}) } else {
@:home_fragment_html(open_shift)
}