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 } } pub struct TelephoneView { link: ComponentLink, value: Telephone, oninput: Callback, } pub enum Msg { UpdateNumber(String), UpdateExtension(String), ToggleWork, ToggleHome, ToggleText, ToggleVoice, ToggleFax, ToggleCell, ToggleVideo, TogglePager, ToggleTextPhone, } #[derive(Clone, PartialEq, Properties)] pub struct Props { pub oninput: Callback, //pub errors: Vec, } impl Component for TelephoneView { type Message = Msg; type Properties = Props; fn create(props: ::Properties, link: yew::html::Scope) -> Self { Self { link, value: Telephone::new(), oninput: props.oninput, } } fn update(&mut self, msg: ::Message) -> bool { match msg { Msg::UpdateNumber(n) => self.value.number = n, Msg::UpdateExtension(e) => self.value.extension = e, Msg::ToggleWork => self.value.work = !self.value.work, Msg::ToggleHome => self.value.home = !self.value.home, Msg::ToggleText => self.value.text = !self.value.text, Msg::ToggleVoice => self.value.voice = !self.value.voice, Msg::ToggleFax => self.value.fax = !self.value.fax, Msg::ToggleCell => self.value.cell = !self.value.cell, Msg::ToggleVideo => self.value.video = !self.value.video, Msg::TogglePager => self.value.pager = !self.value.pager, Msg::ToggleTextPhone => self.value.text_phone = !self.value.text_phone, }; self.oninput.emit(self.value.clone()); true } fn change(&mut self, props: ::Properties) -> bool { self.oninput = props.oninput; true } 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)) ) } { util::text_field_input( "Extension", "extension", None, self.link.callback(|e: InputData| Msg::UpdateExtension(e.value)) ) } { util::checkbox_field_input( "Work", "work", self.value.work, self.link.callback(|_| Msg::ToggleWork) ) }
} } }