Add form to fill client's info
@@ -17,25 +17,37 @@ struct Client {
struct Model {
clients: Vec<Client>,
+ first_name_value: String,
+ last_name_value: String,
}
enum Msg {
AddNew,
+ UpdateFirstName(String),
+ UpdateLastName(String),
Store,
Restore,
Clear,
+ Nope,
}
fn update(context: &mut Context<Msg>, model: &mut Model, msg: Msg) {
match msg {
Msg::AddNew => {
- // TODO Add form to fill client's info
let client = Client {
- first_name: "Denis".into(),
- last_name: "Kolodin".into(),
+ first_name: model.first_name_value.clone(),
+ last_name: model.last_name_value.clone(),
};
model.clients.push(client);
}
+ Msg::UpdateFirstName(val) => {
+ println!("Input: {}", val);
+ model.first_name_value = val;
+ }
+ Msg::UpdateLastName(val) => {
+ println!("Input: {}", val);
+ model.last_name_value = val;
+ }
Msg::Store => {
context.store_value(Scope::Local, KEY, &model.clients);
}
@@ -50,6 +62,7 @@ fn update(context: &mut Context<Msg>, model: &mut Model, msg: Msg) {
model.clients.clear();
context.remove_value(Scope::Local, KEY);
}
+ Msg::Nope => {}
}
}
@@ -67,6 +80,10 @@ fn view(model: &Model) -> Html<Msg> {
<div class="clients",>
{ for model.clients.iter().map(view_client) }
</div>
+ <div class="names",>
+ { view_first_name_input(&model) }
+ { view_last_name_input(&model) }
+ </div>
<button onclick=|_| Msg::AddNew,>{ "AddNew" }</button>
<button onclick=|_| Msg::Store,>{ "Store" }</button>
<button onclick=|_| Msg::Restore,>{ "Restore" }</button>
@@ -75,10 +92,35 @@ fn view(model: &Model) -> Html<Msg> {
}
}
+fn view_first_name_input(model: &Model) -> Html<Msg> {
+ html! {
+ <input class="new-client-firstname",
+ placeholder="First name",
+ value=&model.first_name_value,
+ oninput=|e: InputData| Msg::UpdateFirstName(e.value),
+ onkeypress=|e: KeyData| {
+ if e.key == "Enter" { Msg::AddNew } else { Msg::Nope }
+ }, />
+ }
+}
+
+fn view_last_name_input(model: &Model) -> Html<Msg> {
+ html! {
+ <input class="new-client-lastname",
+ placeholder="Last name",
+ value=&model.last_name_value,
+ oninput=|e: InputData| Msg::UpdateLastName(e.value),
+ onkeypress=|e: KeyData| {
+ if e.key == "Enter" { Msg::AddNew } else { Msg::Nope }
+ }, />
+ }
+}
+
fn main() {
let model = Model {
clients: Vec::new(),
+ first_name_value: "".into(),
+ last_name_value: "".into(),
};
program(model, update, view);
}
-
