diff options
author | jelemux <jeremias.weber@protonmail.com> | 2020-11-24 21:58:51 +0100 |
---|---|---|
committer | jelemux <jeremias.weber@protonmail.com> | 2020-11-24 21:58:51 +0100 |
commit | 5a03734b6767fed04c0913384584d8f59dc597ea (patch) | |
tree | 62ef0a0afc0ab56dd36da1a662db768fd8c22eda /src/view/input_objects/name.rs | |
parent | 49588f22f7d20193f899226107c9e323a82c6951 (diff) | |
download | wasm-card-5a03734b6767fed04c0913384584d8f59dc597ea.tar.gz wasm-card-5a03734b6767fed04c0913384584d8f59dc597ea.tar.bz2 |
add traits for viewmodel and view
Diffstat (limited to 'src/view/input_objects/name.rs')
-rw-r--r-- | src/view/input_objects/name.rs | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/view/input_objects/name.rs b/src/view/input_objects/name.rs new file mode 100644 index 0000000..0fe9e64 --- /dev/null +++ b/src/view/input_objects/name.rs @@ -0,0 +1,118 @@ +use vcard::properties; +use vcard::values::{self, text}; +use super::*; +use super::super::name::*; + +#[derive(Clone)] +pub struct Name { + pub prefix: String, + pub first_name: String, + pub middle_name: String, + pub last_name: String, + pub suffix: String, +} + +impl VCardPropertyInputObject<properties::Name, NameView> for Name { + fn new() -> Self { + Self { + prefix: String::new(), + first_name: String::new(), + middle_name: String::new(), + last_name: String::new(), + suffix: String::new(), + } + } + fn get_input_fields(&self, link: &ComponentLink<NameView>) -> std::vec::Vec<VCardPropertyInputField> { + vec![ + VCardPropertyInputField::Text{ + label: "Prefix".to_string(), + id: Some("prefix".to_string()), + placeholder: Some("Sir".to_string()), + oninput: link.callback(|e: InputData| Msg::UpdatePrefix(e.value)), + value: self.prefix.clone(), + }, + VCardPropertyInputField::Text{ + label: "First Name".to_string(), + id: Some("first_name".to_string()), + placeholder: Some("Arthur".to_string()), + oninput: link.callback(|e: InputData| Msg::UpdateFirstName(e.value)), + value: self.first_name.clone(), + }, + VCardPropertyInputField::Text{ + label: "Middle Name".to_string(), + id: Some("middle_name".to_string()), + placeholder: Some("Charles".to_string()), + oninput: link.callback(|e: InputData| Msg::UpdateMiddleName(e.value)), + value: self.middle_name.clone(), + }, + VCardPropertyInputField::Text{ + label: "Last Name".to_string(), + id: Some("last_name".to_string()), + placeholder: Some("Clarke".to_string()), + oninput: link.callback(|e: InputData| Msg::UpdateLastName(e.value)), + value: self.last_name.clone(), + }, + VCardPropertyInputField::Text{ + label: "Suffix".to_string(), + id: Some("suffix".to_string()), + placeholder: Some("CBE FRAS".to_string()), + oninput: link.callback(|e: InputData| Msg::UpdateSuffix(e.value)), + value: self.suffix.clone(), + }, + ] + } + fn to_vcard_property(&self) -> std::result::Result<properties::Name, VCardPropertyInputError> { + let name_value = values::name_value::NameValue::from_components( + match self.last_name.is_empty() { + true => None, + false => Some(text::Component::from_str(&self.last_name).unwrap()), + }, + match self.first_name.is_empty() { + true => None, + false => Some(text::Component::from_str(&self.first_name).unwrap()), + }, + match self.middle_name.is_empty() { + true => None, + false => Some(text::Component::from_str(&self.middle_name).unwrap()), + }, + match self.prefix.is_empty() { + true => None, + false => Some(text::Component::from_str(&self.prefix).unwrap()), + }, + match self.suffix.is_empty() { + true => None, + false => Some(text::Component::from_str(&self.suffix).unwrap()), + }, + ); + + Ok(properties::Name::from_name_value(name_value)) + } +} + +impl Name { + pub fn formatted_name(&self) -> String { + let mut formatted_name = String::new(); + + if !self.prefix.is_empty() { + formatted_name.push_str(&self.prefix); + } + if !self.first_name.is_empty() { + formatted_name.push_str(" "); + formatted_name.push_str(&self.first_name); + } + if !self.middle_name.is_empty() { + formatted_name.push_str(" "); + formatted_name.push_str(&self.middle_name); + } + if !self.last_name.is_empty() { + formatted_name.push_str(" "); + formatted_name.push_str(&self.last_name); + } + if !self.suffix.is_empty() { + formatted_name.push_str(", "); + formatted_name.push_str(&self.suffix); + } + + formatted_name + } +}
\ No newline at end of file |