Skip to content

Commit

Permalink
feat: persist dashboard state via URL
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonVirgo committed Aug 1, 2024
1 parent 8dda089 commit 8042059
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/routes/pages/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ use crate::{
};
use actix_web::{get, web, HttpResponse, Responder};
use maud::html;
use serde::Deserialize;

use crate::models::thread::get_thread;

#[derive(Deserialize)]
struct QueryParams {
d: Option<String>,
}

#[get("/dashboard")]
async fn dashboard_no_context() -> impl Responder {
HttpResponse::Found()
Expand All @@ -15,7 +21,11 @@ async fn dashboard_no_context() -> impl Responder {
}

#[get("/dashboard/{thread_id}")]
async fn dashboard(raw_thread_id: web::Path<String>, state: web::Data<AppState>) -> impl Responder {
async fn dashboard(
raw_thread_id: web::Path<String>,
query: web::Query<QueryParams>,
state: web::Data<AppState>,
) -> impl Responder {
let raw_thread_id = raw_thread_id.into_inner();
let thread_id = match raw_thread_id.parse::<String>() {
Ok(thread_id) => thread_id.clone(),
Expand Down Expand Up @@ -44,18 +54,31 @@ async fn dashboard(raw_thread_id: web::Path<String>, state: web::Data<AppState>)
println!("{}", res);
res
};

let get_url_param = |param: &str| format!("/dashboard/{}?d={}", thread_id, param);
let get_htmx_trigger = |trigger: &str| match query.d {
Some(ref d) => {
if *d == trigger.to_string() {
"click, load"
} else {
"click"
}
}
None => "click",
};

let markup = html! {
(header)
body."bg-zinc-900 w-screen h-screen flex flex-row items-center justify-center text-white" {
div."bg-zinc-800 border-r border-zinc-600 shrink h-full" {
ul."w-64 flex flex-col gap-2 p-4"{
li."cursor-pointer" hx-get=(gen_url("setup")) hx-target="#dashboard-content" hx-trigger="click, load" {
li."cursor-pointer" hx-get=(gen_url("setup")) hx-target="#dashboard-content" hx-trigger=(get_htmx_trigger("1")) hx-push-url=(get_url_param("1")) {
"Setup"
}
li."cursor-pointer" hx-get=(gen_url("players")) hx-target="#dashboard-content" {
li."cursor-pointer" hx-get=(gen_url("players")) hx-target="#dashboard-content" hx-trigger=(get_htmx_trigger("2")) hx-push-url=(get_url_param("2")) {
"Players"
}
li."cursor-pointer" hx-get=(gen_url("votes")) hx-target="#dashboard-content" {
li."cursor-pointer" hx-get=(gen_url("votes")) hx-target="#dashboard-content" hx-trigger=(get_htmx_trigger("3")) hx-push-url=(get_url_param("3")) {
"Votes"
}
}
Expand Down

0 comments on commit 8042059

Please sign in to comment.