Files
ticketsystem/frontend/src/pages/basic_pages.rs
schn33fuchs 721e43c380 Refined docs and stuff
Docs link to each other and are generally better
2026-05-20 12:50:00 +02:00

146 lines
4.7 KiB
Rust

use gloo_net::http::Request;
use wasm_bindgen_futures::spawn_local;
use yew::prelude::*;
use yew_router::prelude::*;
#[macro_export]
/// Removes surrounding double quotes from a string.
///
/// This macro takes an expression that evaluates to a string and returns a new `String`
/// with any leading or trailing double quotes removed. It's useful for cleaning up
/// string data that might be inadvertently wrapped in quotes, such as JSON string values.
///
/// # Arguments
///
/// * `$str`: An expression that can be converted into a string slice (`&str`).
///
/// # Examples
///
/// ```rust
/// use your_crate::dequote; // Assuming `dequote` is re-exported or in scope
///
/// let quoted_string = "\"hello world\"";
/// let dequoted_string = dequote!(quoted_string);
/// assert_eq!(dequoted_string, "hello world");
///
/// let already_clean = "no quotes";
/// let dequoted_clean = dequote!(already_clean);
/// assert_eq!(dequoted_clean, "no quotes");
/// ```
macro_rules! dequote {
($str:expr) => {
$str.trim_matches('"').to_string()
};
}
/// The main home page component of the application.
///
/// This component displays different content based on whether the logged-in user
/// is an administrator. It fetches the user's admin status from the
/// `/api/users/current` endpoint upon initialization.
///
/// # Behavior
/// - **Loading**: Displays "Loading..." while fetching user data.
/// - **Admin User**: Renders the `TicketCount` utility component.
/// - **Non-Admin User**: Renders the `TicketCount` utility component.
///
/// # Example
/// ```rust
/// html! {
/// <Home />
/// }
/// ```
#[component(Home)]
pub fn home_component() -> Html {
let name = use_state(|| "".to_string());
{
let name = name.clone();
use_effect_with((), move |_| {
spawn_local(async move {
let response = Request::get("/api/users/current")
.credentials(web_sys::RequestCredentials::Include)
.send()
.await;
match response {
Ok(resp) if resp.status() == 200 => {
let user_data: serde_json::Value = resp.json().await.unwrap_or_default();
let name_value = format!(
"{} {}",
dequote!(user_data["data"]["first_name"].to_string()),
dequote!(user_data["data"]["last_name"].to_string())
);
name.set(name_value);
}
_ => name.set("Unknown".to_string()),
}
});
|| ()
});
}
html! {
<div class="form-container home">
<div class="page-header">
<h1>{ "Welcome" }</h1>
</div>
<crate::utilities::TicketCount/>
<div>
<p>{ "You are logged in as: " }</p>
<p class="text-muted">{ &*name }</p>
</div>
</div>
}
}
/// A basic component displayed when a requested route does not match any defined paths (404 error).
///
/// It provides a simple message indicating that the page was not found and includes
/// a link to navigate back to the home page.
///
/// # Example
/// ```rust
/// html! {
/// <NotFound />
/// }
/// ```
#[component(NotFound)]
pub fn not_found_component() -> Html {
let message = "404 Not found";
html! {
<div class="form-container">
<div class="empty-state">
<h1>{&message}</h1>
<p>{ "The page you are looking for does not exist." }</p>
<Link<crate::Route> to={crate::Route::Home}>{ "Back to Home" }</Link<crate::Route>>
</div>
</div>
}
}
/// A component displayed when a user attempts to access a page for which they do not have sufficient permissions.
///
/// It informs the user about the access restriction and provides instructions to contact
/// a specific person ("Herr Winter") if they believe this is an error.
/// It also includes a link to return to the home page.
///
/// # Example
/// ```rust
/// html! {
/// <PermissionDenied />
/// }
/// ```
#[component(PermissionDenied)]
pub fn denied_component() -> Html {
html! {
<div class="form-container">
<div class="empty-state">
<h1>{ "Access Denied" }</h1>
<p>{ "Sie haben nicht die benötigten Rechte um diese Seite aufzurufen" }</p>
<p class="text-muted">{ "Wenn sie denken, dass dies ein Fehler ist kontaktieren sie Herrn Winter" }</p>
<Link<crate::Route> to={crate::Route::Home}>{ "Back to Home" }</Link<crate::Route>>
</div>
</div>
}
}