Non admin only see their tickets

This commit is contained in:
2026-05-01 17:07:20 +02:00
parent fdd2e2a757
commit 4fc1c2eeb8

View File

@@ -37,6 +37,12 @@ pub struct TicketProps {
pub id: i32, pub id: i32,
} }
#[derive(Clone, Debug, PartialEq)]
pub struct ActiveUser {
id: Option<i16>,
is_admin: bool,
}
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct ApiError { struct ApiError {
message: String, message: String,
@@ -344,6 +350,10 @@ pub fn all_tickets_component() -> Html {
let tickets = use_state(|| Vec::<Ticket>::new()); let tickets = use_state(|| Vec::<Ticket>::new());
let error = use_state(|| None::<String>); let error = use_state(|| None::<String>);
let loading = use_state(|| false); let loading = use_state(|| false);
let user = use_state(|| ActiveUser {
id: None,
is_admin: false,
});
{ {
let tickets = tickets.clone(); let tickets = tickets.clone();
@@ -376,6 +386,37 @@ pub fn all_tickets_component() -> Html {
}); });
} }
{
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 { if *loading {
html! {<p>{ "Loading" }</p>} html! {<p>{ "Loading" }</p>}
} else if let Some(e) = &*error { } else if let Some(e) = &*error {
@@ -383,10 +424,17 @@ pub fn all_tickets_component() -> Html {
} else { } else {
html! { html! {
<ul> <ul>
{ for tickets.iter().map(|t| html! { { for tickets.iter().filter(|t| if user.is_admin { true } else if let Some(uid) = user.id { t.user_id == uid } else { false }).map(|t| html! {
<li key={t.id.to_string()}> <li key={t.id.to_string()}>
<h3>{ format!("{} - #{}", t.betreff, t.id) }</h3> <h3>{ format!("{} - #{}", t.betreff, t.id) }</h3>
<p>{ &t.description }</p> <p>{ &t.description }</p>
<p>{ match t.status.as_str() {
"ToDo" => "Zu tun",
"InProgress" => "In Bearbeitung",
"Completed" => "Erledigt",
"Archived" => "Archiviert",
_ => "Ungültiger Status"
}}</p>
</li> </li>
})} })}
</ul> </ul>