jana_sessions_webpage/app/src/main.rs

121 lines
2.7 KiB
Rust
Raw Normal View History

2025-02-20 10:56:28 +01:00
use dioxus::prelude::*;
#[derive(Debug, Clone, Routable, PartialEq)]
enum Route {
2025-02-21 10:49:50 +01:00
#[layout(Navbar)]
#[route("/")]
Home {},
#[route("/blog/:id")]
Blog { id: i32 },
2025-02-20 10:56:28 +01:00
}
const FAVICON: Asset = asset!("/assets/favicon.ico");
const MAIN_CSS: Asset = asset!("/assets/main.css");
const HEADER_SVG: Asset = asset!("/assets/header.svg");
fn main() {
2025-02-21 10:49:50 +01:00
launch(App);
2025-02-20 10:56:28 +01:00
}
#[component]
fn App() -> Element {
rsx! {
2025-02-21 10:49:50 +01:00
document::Link { rel: "icon", href: FAVICON }
document::Link { rel: "stylesheet", href: MAIN_CSS }
Router::<Route> {}
2025-02-20 10:56:28 +01:00
}
}
#[component]
pub fn Hero() -> Element {
rsx! {
2025-02-21 10:49:50 +01:00
div { id: "hero",
img { src: HEADER_SVG, id: "header" }
div { id: "links",
a { href: "https://dioxuslabs.com/learn/0.6/", "📚 Learn Dioxus" }
a { href: "https://dioxuslabs.com/awesome", "🚀 Awesome Dioxus" }
a { href: "https://github.com/dioxus-community/", "📡 Community Libraries" }
a { href: "https://github.com/DioxusLabs/sdk", "⚙️ Dioxus Development Kit" }
a { href: "https://marketplace.visualstudio.com/items?itemName=DioxusLabs.dioxus",
"💫 VSCode Extension"
}
a { href: "https://discord.gg/XgGxMSkvUM", "👋 Community Discord" }
2025-02-20 10:56:28 +01:00
}
2025-02-21 10:49:50 +01:00
}
2025-02-20 10:56:28 +01:00
}
}
/// Home page
#[component]
fn Home() -> Element {
rsx! {
2025-02-21 10:49:50 +01:00
Hero {}
Echo {}
2025-02-20 10:56:28 +01:00
}
}
/// Blog page
#[component]
pub fn Blog(id: i32) -> Element {
rsx! {
2025-02-21 10:49:50 +01:00
div { id: "blog",
2025-02-20 10:56:28 +01:00
2025-02-21 10:49:50 +01:00
// Content
h1 { "This is blog #{id}!" }
p {
"In blog #{id}, we show how the Dioxus router works and how URL parameters can be passed as props to our route components."
2025-02-20 10:56:28 +01:00
}
2025-02-21 10:49:50 +01:00
// Navigation links
Link { to: Route::Blog { id: id - 1 }, "Previous" }
span { " <---> " }
Link { to: Route::Blog { id: id + 1 }, "Next" }
}
2025-02-20 10:56:28 +01:00
}
}
/// Shared navbar component.
#[component]
fn Navbar() -> Element {
rsx! {
2025-02-21 10:49:50 +01:00
div { id: "navbar",
Link { to: Route::Home {}, "Home" }
Link { to: Route::Blog { id: 1 }, "Blog" }
}
2025-02-20 10:56:28 +01:00
2025-02-21 10:49:50 +01:00
Outlet::<Route> {}
2025-02-20 10:56:28 +01:00
}
}
/// Echo component that demonstrates fullstack server functions.
#[component]
fn Echo() -> Element {
let mut response = use_signal(|| String::new());
rsx! {
2025-02-21 10:49:50 +01:00
div { id: "echo",
h4 { "ServerFn Echo" }
input {
placeholder: "Type here to echo...",
oninput: move |event| async move {
let data = echo_server(event.value()).await.unwrap();
response.set(data);
},
}
2025-02-20 10:56:28 +01:00
2025-02-21 10:49:50 +01:00
if !response().is_empty() {
p {
"Server echoed: "
i { "{response}" }
}
2025-02-20 10:56:28 +01:00
}
2025-02-21 10:49:50 +01:00
}
2025-02-20 10:56:28 +01:00
}
}
/// Echo the user input on the server.
#[server(EchoServer)]
async fn echo_server(input: String) -> Result<String, ServerFnError> {
Ok(input)
}