dioxus/examples/crm.rs

120 lines
4.9 KiB
Rust
Raw Normal View History

2021-09-24 05:24:03 +00:00
/*
Tiny CRM: A port of the Yew CRM example to Dioxus.
*/
2022-01-25 00:52:12 +00:00
use dioxus::prelude::*;
2021-09-24 05:24:03 +00:00
fn main() {
dioxus::desktop::launch(app);
2021-09-24 05:24:03 +00:00
}
enum Scene {
ClientsList,
NewClientForm,
Settings,
}
#[derive(Clone, Debug, Default)]
pub struct Client {
pub first_name: String,
pub last_name: String,
pub description: String,
}
fn app(cx: Scope) -> Element {
let clients = use_ref(&cx, || vec![] as Vec<Client>);
2022-01-26 02:41:40 +00:00
let (scene, set_scene) = use_state(&cx, || Scene::ClientsList);
let (firstname, set_firstname) = use_state(&cx, String::new);
let (lastname, set_lastname) = use_state(&cx, String::new);
let (description, set_description) = use_state(&cx, String::new);
2021-09-24 05:24:03 +00:00
cx.render(rsx!(
2022-01-03 06:20:05 +00:00
body {
margin_left: "35%",
link {
rel: "stylesheet",
href: "https://unpkg.com/purecss@2.0.6/build/pure-min.css",
integrity: "sha384-Uu6IeWbM+gzNVXJcM9XV3SohHtmWE+3VGi496jvgX1jyvDTXfdK+rfZc8C1Aehk5",
crossorigin: "anonymous",
}
h1 {"Dioxus CRM Example"}
2022-01-26 02:41:40 +00:00
match scene {
Scene::ClientsList => rsx!(
div { class: "crm",
h2 { margin_bottom: "10px", "List of clients" }
div { class: "clients", margin_left: "10px",
clients.read().iter().map(|client| rsx!(
div { class: "client", style: "margin-bottom: 50px",
p { "First Name: {client.first_name}" }
p { "Last Name: {client.last_name}" }
p {"Description: {client.description}"}
})
)
}
2022-01-26 02:41:40 +00:00
button { class: "pure-button pure-button-primary", onclick: move |_| set_scene(Scene::NewClientForm), "Add New" }
button { class: "pure-button", onclick: move |_| set_scene(Scene::Settings), "Settings" }
2021-09-24 05:24:03 +00:00
}
),
Scene::NewClientForm => rsx!(
div { class: "crm",
h2 { margin_bottom: "10px", "Add new client" }
form { class: "pure-form",
input {
class: "new-client firstname",
placeholder: "First name",
value: "{firstname}",
2022-01-26 02:41:40 +00:00
oninput: move |e| set_firstname(e.value.clone())
}
input {
class: "new-client lastname",
placeholder: "Last name",
value: "{lastname}",
2022-01-26 02:41:40 +00:00
oninput: move |e| set_lastname(e.value.clone())
}
textarea {
class: "new-client description",
placeholder: "Description",
value: "{description}",
2022-01-26 02:41:40 +00:00
oninput: move |e| set_description(e.value.clone())
}
}
button {
class: "pure-button pure-button-primary",
onclick: move |_| {
clients.write().push(Client {
description: (*description).clone(),
first_name: (*firstname).clone(),
last_name: (*lastname).clone(),
});
2022-01-26 02:41:40 +00:00
set_description(String::new());
set_firstname(String::new());
set_lastname(String::new());
},
"Add New"
}
2022-01-26 02:41:40 +00:00
button { class: "pure-button", onclick: move |_| set_scene(Scene::ClientsList),
"Go Back"
}
2021-09-24 05:24:03 +00:00
}
),
Scene::Settings => rsx!(
div {
h2 { margin_bottom: "10px", "Settings" }
button {
background: "rgb(202, 60, 60)",
class: "pure-button pure-button-primary",
onclick: move |_| clients.write().clear(),
"Remove all clients"
}
button {
class: "pure-button pure-button-primary",
2022-01-26 02:41:40 +00:00
onclick: move |_| set_scene(Scene::ClientsList),
"Go Back"
}
2021-09-24 05:24:03 +00:00
}
)
}
2021-09-24 05:24:03 +00:00
}
2021-11-07 03:11:17 +00:00
))
}