diff --git a/frontend/src/lib.rs b/frontend/src/lib.rs index dbff061..54ba270 100644 --- a/frontend/src/lib.rs +++ b/frontend/src/lib.rs @@ -23,6 +23,8 @@ enum Route { AllUsers, #[at("/users/:id")] UserByID { id: i16 }, + #[at("/diagnostics")] + Diagnostics, #[at("/denied")] PermissionDenied, #[not_found] @@ -70,6 +72,11 @@ fn switch(route: Route) -> Html { }, Route::PermissionDenied => html! { }, + Route::Diagnostics => html! { + + + + }, } } diff --git a/frontend/src/pages/mod.rs b/frontend/src/pages/mod.rs index 69682f9..edab625 100644 --- a/frontend/src/pages/mod.rs +++ b/frontend/src/pages/mod.rs @@ -2,3 +2,5 @@ pub mod basic_pages; pub mod sidebar; pub mod ticket; pub mod user; +pub mod utilities; + diff --git a/frontend/src/pages/ticket.rs b/frontend/src/pages/ticket.rs index 1d9383a..2b58af7 100644 --- a/frontend/src/pages/ticket.rs +++ b/frontend/src/pages/ticket.rs @@ -41,8 +41,8 @@ pub struct TicketProps { #[derive(Clone, Debug, PartialEq)] pub struct ActiveUser { - id: Option, - is_admin: bool, + pub id: Option, + pub is_admin: bool, } #[derive(Deserialize, Debug)] diff --git a/frontend/src/pages/utilities.rs b/frontend/src/pages/utilities.rs new file mode 100644 index 0000000..37e5e76 --- /dev/null +++ b/frontend/src/pages/utilities.rs @@ -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! { +
+ +
+ } +} + +#[component(TicketCount)] +pub fn ticket_count_component() -> Html { + let tickets = use_state(|| Vec::::new()); + let error = use_state(|| None::); + 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::>().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::().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! {

{ "Loading" }

} + } else if let Some(e) = &*error { + html! {

{ format!("Error: {}", e) }

} + } 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! { +
+

{ "Offene Tickets" }

+

{ count }

+
+ } + } +} + +// #[component(SubmitStats)] +// pub fn submit_stats_component() -> Html {}