dioxus/examples/crm.rs

153 lines
4.4 KiB
Rust
Raw Normal View History

2022-12-16 10:55:20 +00:00
//! Tiny CRM: A port of the Yew CRM example to Dioxus.
2022-01-25 00:52:12 +00:00
use dioxus::prelude::*;
2022-12-16 10:55:20 +00:00
use dioxus_router::prelude::*;
2021-09-24 05:24:03 +00:00
fn main() {
launch_desktop(app);
2021-09-24 05:24:03 +00:00
}
/// A type alias that reprsents a shared context between components
///
/// Normally we'd wrap the Context in a newtype, but we only have one Signal<Vec<Client>> in this app
type Clients = Signal<Vec<Client>>;
2023-06-02 17:33:47 +00:00
fn app() -> Element {
use_context_provider::<Clients>(|| Signal::new(vec![]));
2021-09-24 05:24:03 +00:00
2024-01-16 19:18:46 +00:00
rsx! {
2022-12-16 10:55:20 +00:00
link {
rel: "stylesheet",
href: "https://unpkg.com/purecss@2.0.6/build/pure-min.css",
integrity: "sha384-Uu6IeWbM+gzNVXJcM9XV3SohHtmWE+3VGi496jvgX1jyvDTXfdK+rfZc8C1Aehk5",
2024-01-05 01:11:32 +00:00
crossorigin: "anonymous"
2022-12-16 10:55:20 +00:00
}
2024-01-05 01:11:32 +00:00
style {
"
2022-12-16 10:55:20 +00:00
.red {{
background-color: rgb(202, 60, 60) !important;
}}
2024-01-05 01:11:32 +00:00
"
}
2022-12-16 10:55:20 +00:00
h1 { "Dioxus CRM Example" }
2022-12-16 10:55:20 +00:00
2023-07-26 01:14:48 +00:00
Router::<Route> {}
2022-12-16 10:55:20 +00:00
}
}
#[derive(Routable, Clone)]
#[rustfmt::skip]
enum Route {
#[route("/")]
ClientList {},
#[route("/new")]
ClientAdd {},
#[route("/settings")]
Settings {},
}
#[derive(Clone, Debug, Default)]
pub struct Client {
pub first_name: String,
pub last_name: String,
pub description: String,
}
#[component]
fn ClientList() -> Element {
let mut clients = use_context::<Clients>();
2024-01-16 19:18:46 +00:00
rsx! {
2022-12-16 10:55:20 +00:00
h2 { "List of Clients" }
2024-01-05 01:11:32 +00:00
Link { to: Route::ClientAdd {}, class: "pure-button pure-button-primary", "Add Client" }
Link { to: Route::Settings {}, class: "pure-button", "Settings" }
for client in clients.read().iter() {
2024-01-15 21:06:05 +00:00
div { class: "client", style: "margin-bottom: 50px",
2022-12-16 10:55:20 +00:00
p { "Name: {client.first_name} {client.last_name}" }
p { "Description: {client.description}" }
}
}
2024-01-14 05:12:21 +00:00
}
2022-12-16 10:55:20 +00:00
}
#[component]
2024-01-15 21:06:05 +00:00
fn ClientAdd() -> Element {
let mut first_name = use_signal(String::new);
let mut last_name = use_signal(String::new);
let mut description = use_signal(String::new);
2022-12-16 10:55:20 +00:00
let submit_client = move |_: FormEvent| {
consume_context::<Clients>().write().push(Client {
first_name: first_name.to_string(),
last_name: last_name.to_string(),
description: description.to_string(),
});
dioxus_router::router().push(Route::ClientList {});
};
2024-01-16 19:18:46 +00:00
rsx! {
2022-12-16 10:55:20 +00:00
h2 { "Add new Client" }
form { class: "pure-form pure-form-aligned", onsubmit: submit_client,
2022-12-16 10:55:20 +00:00
fieldset {
2024-01-05 01:11:32 +00:00
div { class: "pure-control-group",
label { "for": "first_name", "First Name" }
2022-12-16 10:55:20 +00:00
input {
id: "first_name",
2024-01-15 22:51:34 +00:00
r#type: "text",
2022-12-16 10:55:20 +00:00
placeholder: "First Name…",
required: "",
value: "{first_name}",
2023-09-01 20:38:55 +00:00
oninput: move |e| first_name.set(e.value())
2021-09-24 05:24:03 +00:00
}
2022-03-05 19:36:25 +00:00
}
2022-12-16 10:55:20 +00:00
2024-01-05 01:11:32 +00:00
div { class: "pure-control-group",
label { "for": "last_name", "Last Name" }
2022-12-16 10:55:20 +00:00
input {
id: "last_name",
2024-01-15 22:51:34 +00:00
r#type: "text",
2022-12-16 10:55:20 +00:00
placeholder: "Last Name…",
required: "",
value: "{last_name}",
2023-09-01 20:38:55 +00:00
oninput: move |e| last_name.set(e.value())
2021-09-24 05:24:03 +00:00
}
2022-03-05 19:36:25 +00:00
}
2022-12-16 10:55:20 +00:00
2024-01-05 01:11:32 +00:00
div { class: "pure-control-group",
label { "for": "description", "Description" }
2022-12-16 10:55:20 +00:00
textarea {
id: "description",
placeholder: "Description…",
value: "{description}",
2023-09-01 20:38:55 +00:00
oninput: move |e| description.set(e.value())
2022-12-16 10:55:20 +00:00
}
}
2024-01-05 01:11:32 +00:00
div { class: "pure-controls",
2024-01-15 22:51:34 +00:00
button { r#type: "submit", class: "pure-button pure-button-primary", "Save" }
2024-01-05 01:11:32 +00:00
Link { to: Route::ClientList {}, class: "pure-button pure-button-primary red", "Cancel" }
2022-03-05 19:36:25 +00:00
}
}
2022-12-16 10:55:20 +00:00
}
2024-01-14 05:12:21 +00:00
}
2022-12-16 10:55:20 +00:00
}
#[component]
2024-01-15 21:06:05 +00:00
fn Settings() -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
2022-12-16 10:55:20 +00:00
h2 { "Settings" }
button {
class: "pure-button pure-button-primary red",
onclick: move |_| consume_context::<Clients>().write().clear(),
2022-12-16 10:55:20 +00:00
"Remove all Clients"
}
2024-01-05 01:11:32 +00:00
Link { to: Route::ClientList {}, class: "pure-button", "Go back" }
2024-01-14 05:12:21 +00:00
}
}