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/address.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/address.rs')
-rw-r--r-- | src/view/address.rs | 181 |
1 files changed, 29 insertions, 152 deletions
diff --git a/src/view/address.rs b/src/view/address.rs index a30ba85..4d3eae7 100644 --- a/src/view/address.rs +++ b/src/view/address.rs @@ -1,108 +1,14 @@ use yew::prelude::*; use vcard::properties; -use vcard::parameters; -use vcard::values::{self, text}; -use std::collections::HashSet; - -use crate::util; - -#[derive(Clone)] -pub struct Address { - pub post_office_box: String, - pub extension: String, - pub street: String, - pub locality: String, - pub region: String, - pub code: String, - pub country: String, - address_type: AddressType, -} - -impl Address { - pub fn new_with_type(address_type: AddressType) -> Self { - Self { - post_office_box: String::new(), - extension: String::new(), - street: String::new(), - locality: String::new(), - region: String::new(), - code: String::new(), - country: String::new(), - address_type - } - } - pub fn to_vcard_value(&self) -> properties::Address { - let address_value = values::address_value::AddressValue::from_components( - match self.post_office_box.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.post_office_box).unwrap()), - }, - match self.extension.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.extension).unwrap()), - }, - match self.street.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.street).unwrap()), - }, - match self.locality.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.locality).unwrap()), - }, - match self.region.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.region).unwrap()), - }, - match self.code.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.code).unwrap()), - }, - match self.country.is_empty() { - true => None, - false => Some(text::Component::from_str(&self.country).unwrap()), - }, - ); - - let mut address = properties::Address::from_address_value(address_value); - - let type_values = { - let mut type_values = HashSet::new(); - - type_values.insert( - match self.address_type { - AddressType::Home => values::type_value::TypeValue::Home, - AddressType::Work => values::type_value::TypeValue::Work, - } - ); - - vcard::Set::from_hash_set(type_values).unwrap() - }; - - address.typ = Some(parameters::typ::Type::from_type_values(type_values)); - - address - } -} - -#[derive(Clone, Copy, PartialEq)] -pub enum AddressType { - Home, - Work, -} - -impl AddressType { - pub fn to_str(&self) -> &str { - match self { - AddressType::Home => "Home", - AddressType::Work => "Work", - } - } -} +use super::input_objects::address::*; +use super::input_objects::VCardPropertyInputObject; +use super::VCardPropertyInputComponent; pub struct AddressView { link: ComponentLink<Self>, value: Address, oninput: Callback<Address>, + errors: Vec<String>, } pub enum Msg { @@ -113,23 +19,37 @@ pub enum Msg { UpdateRegion(String), UpdateCode(String), UpdateCountry(String), + ToggleWork, + ToggleHome, } #[derive(Clone, PartialEq, Properties)] pub struct Props { pub oninput: Callback<Address>, - pub address_type: AddressType, //pub errors: Vec<String>, } +impl VCardPropertyInputComponent<properties::Address, Address> for AddressView { + fn get_input_object(&self) -> Address { + self.value.clone() + } + fn get_title(&self) -> String { + "Address".to_string() + } + fn get_errors(&self) -> Vec<String> { + self.errors.clone() + } +} + impl Component for AddressView { type Message = Msg; type Properties = Props; fn create(props: <Self as yew::Component>::Properties, link: yew::html::Scope<Self>) -> Self { Self { link, - value: Address::new_with_type(props.address_type), + value: Address::new(), oninput: props.oninput, + errors: vec![], } } fn update(&mut self, msg: <Self as yew::Component>::Message) -> bool { @@ -141,72 +61,29 @@ impl Component for AddressView { Msg::UpdateRegion(r) => self.value.region = r, Msg::UpdateCode(p) => self.value.code = p, Msg::UpdateCountry(c) => self.value.country = c, + Msg::ToggleWork => self.value.work = !self.value.work, + Msg::ToggleHome => self.value.home = !self.value.home, }; self.oninput.emit(self.value.clone()); true } fn change(&mut self, props: <Self as yew::Component>::Properties) -> bool { self.oninput = props.oninput; - self.value.address_type = props.address_type; true } fn view(&self) -> yew::virtual_dom::VNode { html!{ <div class="box"> - <h3 class="subtitle">{ format!("{} Address", self.value.address_type.to_str()) }</h3> - - <div class="columns is-mobile is-multiline"> - - { util::text_field_input( - "Post Office Box", - "post_office_box", - None, - self.link.callback(|e: InputData| Msg::UpdatePostOfficeBox(e.value)) - ) } - - { util::text_field_input( - "Extension", - "extension", - None, - self.link.callback(|e: InputData| Msg::UpdateExtension(e.value)) - ) } - - { util::text_field_input( - "Street", - "street", - None, - self.link.callback(|e: InputData| Msg::UpdateStreet(e.value)) - ) } - - { util::text_field_input( - "Locality", - "locality", - None, - self.link.callback(|e: InputData| Msg::UpdateLocality(e.value)) - ) } - - { util::text_field_input( - "Region", - "region", - None, - self.link.callback(|e: InputData| Msg::UpdateRegion(e.value)) - ) } + { + self.render_errors() + } - { util::text_field_input( - "Postal Code", - "code", - None, - self.link.callback(|e: InputData| Msg::UpdateCode(e.value)) - ) } + <h3 class="subtitle">{ self.get_title() }</h3> - { util::text_field_input( - "Country", - "country", - None, - self.link.callback(|e: InputData| Msg::UpdateCountry(e.value)) - ) } + { + self.get_input_object().render(&self.link) + } - </div> </div> } } |