2021-09-24 05:24:03 +00:00
|
|
|
/*
|
|
|
|
Tiny CRM: A port of the Yew CRM example to Dioxus.
|
|
|
|
*/
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
dioxus::web::launch(App, |c| c);
|
|
|
|
}
|
|
|
|
enum Scene {
|
|
|
|
ClientsList,
|
|
|
|
NewClientForm,
|
|
|
|
Settings,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct Client {
|
|
|
|
pub first_name: String,
|
|
|
|
pub last_name: String,
|
|
|
|
pub description: String,
|
|
|
|
}
|
|
|
|
|
2021-12-10 02:19:31 +00:00
|
|
|
static App: Component<()> = |cx, _| {
|
2021-11-03 19:13:50 +00:00
|
|
|
let mut clients = use_ref(cx, || vec![] as Vec<Client>);
|
|
|
|
let mut scene = use_state(cx, || Scene::ClientsList);
|
2021-09-24 05:24:03 +00:00
|
|
|
|
2021-11-03 19:13:50 +00:00
|
|
|
let mut firstname = use_state(cx, || String::new());
|
|
|
|
let mut lastname = use_state(cx, || String::new());
|
|
|
|
let mut description = use_state(cx, || String::new());
|
2021-09-24 05:24:03 +00:00
|
|
|
|
2021-09-24 06:10:54 +00:00
|
|
|
let scene = match *scene {
|
2021-09-24 05:24:03 +00:00
|
|
|
Scene::ClientsList => {
|
|
|
|
rsx!(cx, div { class: "crm"
|
2021-09-24 06:10:54 +00:00
|
|
|
h2 { "List of clients" margin_bottom: "10px" }
|
|
|
|
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}"}
|
|
|
|
})
|
|
|
|
)}
|
2021-09-24 05:24:03 +00:00
|
|
|
}
|
2021-09-24 06:10:54 +00:00
|
|
|
button { class: "pure-button pure-button-primary" onclick: move |_| scene.set(Scene::NewClientForm), "Add New" }
|
|
|
|
button { class: "pure-button" onclick: move |_| scene.set(Scene::Settings), "Settings" }
|
2021-09-24 05:24:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
Scene::NewClientForm => {
|
2021-09-24 06:10:54 +00:00
|
|
|
let add_new = move |_| {
|
|
|
|
clients.write().push(Client {
|
|
|
|
description: (*description).clone(),
|
|
|
|
first_name: (*firstname).clone(),
|
|
|
|
last_name: (*lastname).clone(),
|
|
|
|
});
|
|
|
|
description.set(String::new());
|
|
|
|
firstname.set(String::new());
|
|
|
|
lastname.set(String::new());
|
|
|
|
};
|
2021-09-24 05:24:03 +00:00
|
|
|
rsx!(cx, div { class: "crm"
|
2021-09-24 06:10:54 +00:00
|
|
|
h2 {"Add new client" margin_bottom: "10px" }
|
|
|
|
form { class: "pure-form"
|
|
|
|
input { class: "new-client firstname" placeholder: "First name" value: "{firstname}"
|
2021-10-04 14:22:20 +00:00
|
|
|
oninput: move |e| firstname.set(e.value.clone())
|
2021-09-24 05:24:03 +00:00
|
|
|
}
|
2021-09-24 06:10:54 +00:00
|
|
|
input { class: "new-client lastname" placeholder: "Last name" value: "{lastname}"
|
2021-10-04 14:22:20 +00:00
|
|
|
oninput: move |e| lastname.set(e.value.clone())
|
2021-09-24 05:24:03 +00:00
|
|
|
}
|
2021-09-24 06:10:54 +00:00
|
|
|
textarea { class: "new-client description" placeholder: "Description" value: "{description}"
|
2021-10-04 14:22:20 +00:00
|
|
|
oninput: move |e| description.set(e.value.clone())
|
2021-09-24 05:24:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-24 06:10:54 +00:00
|
|
|
button { class: "pure-button pure-button-primary", onclick: {add_new}, "Add New" }
|
|
|
|
button { class: "pure-button", onclick: move |_| scene.set(Scene::ClientsList), "Go Back" }
|
2021-09-24 05:24:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
Scene::Settings => {
|
|
|
|
rsx!(cx, div {
|
2021-09-24 06:10:54 +00:00
|
|
|
h2 {"Settings" margin_bottom: "10px" }
|
|
|
|
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"
|
|
|
|
onclick: move |_| scene.set(Scene::ClientsList),
|
|
|
|
"Go Back"
|
|
|
|
}
|
2021-09-24 05:24:03 +00:00
|
|
|
})
|
|
|
|
}
|
2021-09-24 06:10:54 +00:00
|
|
|
};
|
|
|
|
|
2021-11-07 03:11:17 +00:00
|
|
|
cx.render(rsx!(
|
|
|
|
body {
|
|
|
|
link {
|
|
|
|
rel: "stylesheet"
|
|
|
|
href: "https://unpkg.com/purecss@2.0.6/build/pure-min.css"
|
|
|
|
integrity: "sha384-Uu6IeWbM+gzNVXJcM9XV3SohHtmWE+3VGi496jvgX1jyvDTXfdK+rfZc8C1Aehk5"
|
|
|
|
crossorigin: "anonymous"
|
|
|
|
}
|
|
|
|
margin_left: "35%"
|
|
|
|
h1 {"Dioxus CRM Example"}
|
|
|
|
{scene}
|
|
|
|
}
|
|
|
|
))
|
2021-09-24 05:24:03 +00:00
|
|
|
};
|