dioxus/examples/crm.rs

158 lines
5.2 KiB
Rust
Raw Normal View History

2024-02-14 20:33:07 +00:00
//! Tiny CRM - A simple CRM app using the Router component and global signals
//!
//! This shows how to use the `Router` component to manage different views in your app. It also shows how to use global
//! signals to manage state across the entire app.
//!
//! We could simply pass the state as a prop to each component, but this is a good example of how to use global state
//! in a way that works across pages.
//!
//! We implement a number of important details here too, like focusing inputs, handling form submits, navigating the router,
//! platform-specific configuration, and importing 3rd party CSS libaries.
2022-01-25 00:52:12 +00:00
use dioxus::prelude::*;
2021-09-24 05:24:03 +00:00
fn main() {
2024-01-19 23:59:51 +00:00
LaunchBuilder::new()
2024-01-20 01:06:46 +00:00
.with_cfg(desktop!({
use dioxus::desktop::{LogicalSize, WindowBuilder};
dioxus::desktop::Config::default()
.with_window(WindowBuilder::new().with_inner_size(LogicalSize::new(800, 600)))
}))
2024-01-19 23:59:51 +00:00
.launch(|| {
rsx! {
link {
rel: "stylesheet",
href: "https://unpkg.com/purecss@2.0.6/build/pure-min.css",
integrity: "sha384-Uu6IeWbM+gzNVXJcM9XV3SohHtmWE+3VGi496jvgX1jyvDTXfdK+rfZc8C1Aehk5",
crossorigin: "anonymous"
}
2024-02-14 20:33:07 +00:00
style { {include_str!("./assets/crm.css")} }
2024-01-20 01:06:46 +00:00
h1 { "Dioxus CRM Example" }
Router::<Route> {}
2024-01-19 23:59:51 +00:00
}
2024-01-20 01:06:46 +00:00
});
2021-09-24 05:24:03 +00:00
}
2024-01-19 23:48:21 +00:00
/// We only have one list of clients for the whole app, so we can use a global signal.
2024-01-31 02:29:49 +00:00
static CLIENTS: GlobalSignal<Vec<Client>> = Signal::global(Vec::new);
2023-06-02 17:33:47 +00:00
2024-01-20 01:06:46 +00:00
struct Client {
first_name: String,
last_name: String,
description: String,
2022-12-16 10:55:20 +00:00
}
2024-02-14 20:33:07 +00:00
/// The pages of the app, each with a route
#[derive(Routable, Clone)]
enum Route {
#[route("/")]
2024-02-14 20:33:07 +00:00
List,
#[route("/new")]
2024-02-14 20:33:07 +00:00
New,
#[route("/settings")]
2024-01-21 20:46:19 +00:00
Settings,
}
#[component]
2024-02-14 20:33:07 +00:00
fn List() -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
2022-12-16 10:55:20 +00:00
h2 { "List of Clients" }
2024-02-14 20:33:07 +00:00
Link { to: Route::New, class: "pure-button pure-button-primary", "Add Client" }
2024-01-21 20:46:19 +00:00
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-02-14 20:33:07 +00:00
fn New() -> 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
2024-02-14 20:33:07 +00:00
let submit_client = move |_| {
2024-01-19 23:48:21 +00:00
// Write the client
CLIENTS.write().push(Client {
first_name: first_name(),
last_name: last_name(),
description: description(),
});
2024-01-19 23:48:21 +00:00
// And then navigate back to the client list
2024-02-14 20:33:07 +00:00
router().push(Route::List);
};
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",
2024-01-19 23:48:21 +00:00
label { r#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…",
2024-02-14 20:33:07 +00:00
required: true,
2022-12-16 10:55:20 +00:00
value: "{first_name}",
2024-01-19 23:59:51 +00:00
oninput: move |e| first_name.set(e.value()),
// when the form mounts, focus the first name input
2024-01-31 02:29:49 +00:00
onmounted: move |e| async move {
2024-01-31 22:07:00 +00:00
_ = e.set_focus(true).await;
2024-01-31 02:29:49 +00:00
},
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",
2024-02-14 20:33:07 +00:00
label { r#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…",
2024-02-14 20:33:07 +00:00
required: true,
2022-12-16 10:55:20 +00:00
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",
2024-02-14 20:33:07 +00:00
label { r#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-02-14 20:33:07 +00:00
Link { to: Route::List, 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",
2024-01-19 23:48:21 +00:00
onclick: move |_| {
CLIENTS.write().clear();
2024-02-14 20:33:07 +00:00
dioxus::router::router().push(Route::List);
2024-01-19 23:48:21 +00:00
},
2022-12-16 10:55:20 +00:00
"Remove all Clients"
}
2024-02-14 20:33:07 +00:00
Link { to: Route::List, class: "pure-button", "Go back" }
2024-01-14 05:12:21 +00:00
}
}