View still active tickets

This commit is contained in:
2026-05-02 13:00:30 +02:00
parent 4bdf3dc04a
commit 37c00b0190
4 changed files with 123 additions and 2 deletions

View File

@@ -23,6 +23,8 @@ enum Route {
AllUsers, AllUsers,
#[at("/users/:id")] #[at("/users/:id")]
UserByID { id: i16 }, UserByID { id: i16 },
#[at("/diagnostics")]
Diagnostics,
#[at("/denied")] #[at("/denied")]
PermissionDenied, PermissionDenied,
#[not_found] #[not_found]
@@ -70,6 +72,11 @@ fn switch(route: Route) -> Html {
</ProtectedRoute> </ProtectedRoute>
}, },
Route::PermissionDenied => html! { <basic_pages::PermissionDenied/> }, Route::PermissionDenied => html! { <basic_pages::PermissionDenied/> },
Route::Diagnostics => html! {
<ProtectedRoute admin_page={true}>
<utilities::Diagnostics/>
</ProtectedRoute>
},
} }
} }

View File

@@ -2,3 +2,5 @@ pub mod basic_pages;
pub mod sidebar; pub mod sidebar;
pub mod ticket; pub mod ticket;
pub mod user; pub mod user;
pub mod utilities;

View File

@@ -41,8 +41,8 @@ pub struct TicketProps {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct ActiveUser { pub struct ActiveUser {
id: Option<i16>, pub id: Option<i16>,
is_admin: bool, pub is_admin: bool,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]

View File

@@ -0,0 +1,112 @@
use gloo_net::http::Request;
use wasm_bindgen_futures::spawn_local;
use yew::prelude::*;
use yew_router::prelude::*;
use crate::pages::ticket::{ActiveUser, Ticket};
#[component(Diagnostics)]
pub fn diagnostics_component() -> Html {
html! {
<div>
<TicketCount/>
</div>
}
}
#[component(TicketCount)]
pub fn ticket_count_component() -> Html {
let tickets = use_state(|| Vec::<Ticket>::new());
let error = use_state(|| None::<String>);
let loading = use_state(|| false);
let user = use_state(|| ActiveUser {
id: None,
is_admin: false,
});
{
let tickets = tickets.clone();
let error = error.clone();
let loading = loading.clone();
use_effect_with((), move |_| {
loading.set(true);
spawn_local(async move {
let url = format!("/api/tickets");
match Request::get(&url).send().await {
Ok(response) if response.status() == 200 => {
match response.json::<Vec<Ticket>>().await {
Ok(t) => tickets.set(t),
Err(e) => error.set(Some(format!("parse error: {}", e))),
}
}
Ok(response) => {
if let Ok(text) = response.text().await {
error.set(Some(text));
} else {
error.set(Some(format!("status {}", response.status())));
}
}
Err(err) => error.set(Some(format!("Network error: {}", err))),
}
loading.set(false);
});
|| ()
});
}
{
let user = user.clone();
use_effect_with((), move |_| {
let user = user.clone();
spawn_local(async move {
if let Ok(response) = Request::get("/api/users/current")
.credentials(web_sys::RequestCredentials::Include)
.send()
.await
{
if response.status() == 200 {
if let Ok(json) = response.json::<serde_json::Value>().await {
let id = json
.get("data")
.and_then(|d| d.get("id"))
.and_then(|v| v.as_i64())
.and_then(|n| i16::try_from(n).ok());
let is_admin = json
.get("data")
.and_then(|d| d.get("is_admin"))
.and_then(|v| v.as_bool())
.unwrap_or(false);
user.set(ActiveUser { id, is_admin });
}
}
}
});
|| ()
});
}
if *loading {
html! {<p>{ "Loading" }</p>}
} else if let Some(e) = &*error {
html! { <p>{ format!("Error: {}", e) }</p> }
} else {
let status_conditions = |t: &Ticket| t.status == "ToDo" || t.status == "InProgress";
let count = tickets
.iter()
.filter(|t| {
status_conditions(t)
&& (user.is_admin || user.id.map_or(false, |uid| t.user_id == uid))
})
.count();
html! {
<div>
<h2>{ "Offene Tickets" }</h2>
<h4>{ count }</h4>
</div>
}
}
}
// #[component(SubmitStats)]
// pub fn submit_stats_component() -> Html {}