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,
};
use cm_lib::{
hx_request::HxRequest,
models::{NewDrink, Shift},
schema::shifts,
};
@ -28,7 +29,7 @@ pub(crate) fn router() -> axum::Router<AppState> {
.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 mut conn = state.connection.get().await.unwrap();
conn.transaction(|conn| {
@ -53,10 +54,14 @@ async fn open_shift(State(state): State<AppState>) -> 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<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();
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<AppState>, Path(id): Path<u32>) -> 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<DrinkForm> for NewDrink {
async fn add_drink(
State(state): State<AppState>,
HxRequest(hx): HxRequest,
Form(form): Form<DrinkForm>,
) -> 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),
)
}

View File

@ -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<S> FromRequestParts<S> for HxRequest

View File

@ -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<AppState>) -> impl IntoResponse {
async fn root(State(state): State<AppState>, HxRequest(hx): HxRequest) -> impl IntoResponse {
let mut conn = state.connection.get().await.unwrap();
let open_shift: Option<Shift> = {
@ -130,7 +130,7 @@ async fn root(State(state): State<AppState>) -> 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<u32>) -> impl IntoResponse {

View File

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