View still active tickets
This commit is contained in:
@@ -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>
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -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)]
|
||||||
|
|||||||
112
frontend/src/pages/utilities.rs
Normal file
112
frontend/src/pages/utilities.rs
Normal 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 {}
|
||||||
Reference in New Issue
Block a user