From 5a03734b6767fed04c0913384584d8f59dc597ea Mon Sep 17 00:00:00 2001 From: jelemux Date: Tue, 24 Nov 2020 21:58:51 +0100 Subject: add traits for viewmodel and view --- src/view/telephone.rs | 137 +++++++++----------------------------------------- 1 file changed, 24 insertions(+), 113 deletions(-) (limited to 'src/view/telephone.rs') diff --git a/src/view/telephone.rs b/src/view/telephone.rs index 28a6f01..516552e 100644 --- a/src/view/telephone.rs +++ b/src/view/telephone.rs @@ -1,100 +1,14 @@ -use yew::services::ConsoleService; use yew::prelude::*; use vcard::properties; -use vcard::parameters; -use vcard::values::{self, text}; -use vcard::validators::ValidatedWrapper; -use std::collections::HashSet; -use crate::util; - -#[derive(Clone)] -pub struct Telephone { - pub number: String, - pub extension: String, - pub work: bool, - pub home: bool, - pub text: bool, - pub voice: bool, - pub fax: bool, - pub cell: bool, - pub video: bool, - pub pager: bool, - pub text_phone: bool, -} - -impl Telephone { - pub fn new() -> Self { - Self { - number: String::new(), - extension: String::new(), - work: false, - home: false, - text: false, - voice: false, - fax: false, - cell: false, - video: false, - pager: false, - text_phone: false, - } - } - pub fn to_vcard_value(&self) -> properties::Telephone { - let mut telephone = properties::Telephone::from_telephone_value( - values::telephone_value::TelephoneValue::from_telephone_number_str( - self.number.clone(), - match self.extension.is_empty() { - true => None::<&str>, - false => Some(&self.extension), - }, - ).unwrap() - ); - - let type_values = { - let mut type_values = HashSet::new(); - - if self.work { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Work); - } - if self.home { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Home); - } - if self.text { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Text); - } - if self.voice { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Voice); - } - if self.fax { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Fax); - } - if self.cell { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Cell); - } - if self.video { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Video); - } - if self.pager { - type_values.insert(values::type_value::TypeValueWithTelephoneType::Pager); - } - if self.text_phone { - type_values.insert(values::type_value::TypeValueWithTelephoneType::TextPhone); - } - - vcard::Set::from_hash_set(type_values).unwrap() - }; - - if let properties::Telephone::TelephoneValue { ref mut typ, .. } = telephone { - *typ = Some(parameters::typ::TypeWithTelType::from_type_values(type_values)); - } - - telephone - } -} +use super::input_objects::telephone::*; +use super::input_objects::VCardPropertyInputObject; +use super::VCardPropertyInputComponent; pub struct TelephoneView { link: ComponentLink, value: Telephone, oninput: Callback, + errors: Vec, } pub enum Msg { @@ -117,6 +31,18 @@ pub struct Props { //pub errors: Vec, } +impl VCardPropertyInputComponent for TelephoneView { + fn get_input_object(&self) -> Telephone { + self.value.clone() + } + fn get_title(&self) -> String { + "Telephone".to_string() + } + fn get_errors(&self) -> Vec { + self.errors.clone() + } +} + impl Component for TelephoneView { type Message = Msg; type Properties = Props; @@ -125,6 +51,7 @@ impl Component for TelephoneView { link, value: Telephone::new(), oninput: props.oninput, + errors: vec![], } } fn update(&mut self, msg: ::Message) -> bool { @@ -151,32 +78,16 @@ impl Component for TelephoneView { fn view(&self) -> yew::virtual_dom::VNode { html!{
-

{ "Telephone" }

- -
- - { util::text_field_input( - "Number", - "number", - None, - self.link.callback(|e: InputData| Msg::UpdateNumber(e.value)) - ) } + { + self.render_errors() + } - { util::text_field_input( - "Extension", - "extension", - None, - self.link.callback(|e: InputData| Msg::UpdateExtension(e.value)) - ) } +

{ self.get_title() }

- { util::checkbox_field_input( - "Work", - "work", - self.value.work, - self.link.callback(|_| Msg::ToggleWork) - ) } + { + self.get_input_object().render(&self.link) + } -
} } -- cgit v1.2.3