summaryrefslogtreecommitdiff
path: root/src/view/input_objects/address.rs
blob: 3975200a79909c6d8242ff1723443353a41152cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use vcard::properties;
use vcard::parameters;
use vcard::values::{self, text};
use std::collections::HashSet;
use super::*;
use super::super::address::*;

#[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,
    pub work: bool,
    pub home: bool,
}

impl VCardPropertyInputObject<properties::Address, AddressView> for Address {
    fn new() -> 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(),
            work: false,
            home: false,
        }
    }
    fn get_input_fields(&self, link: &ComponentLink<AddressView>) -> Vec<VCardPropertyInputField> {
        vec![
            VCardPropertyInputField::Text{
                label: "Post Office Box".to_string(),
                id: Some("post_office_box".to_string()),
                placeholder: None,
                oninput: link.callback(|e: InputData| Msg::UpdatePostOfficeBox(e.value)),
                value: self.post_office_box.clone(),
            },
            VCardPropertyInputField::Text{
                label: "Extension".to_string(),
                id: Some("extension".to_string()),
                placeholder: None,
                oninput: link.callback(|e: InputData| Msg::UpdateExtension(e.value)),
                value: self.extension.clone(),
            },
            VCardPropertyInputField::Text{
                label: "Street".to_string(),
                id: Some("street".to_string()),
                placeholder: None,
                oninput: link.callback(|e: InputData| Msg::UpdateStreet(e.value)),
                value: self.street.clone(),
            },
            VCardPropertyInputField::Text{
                label: "Locality".to_string(),
                id: Some("locality".to_string()),
                placeholder: None,
                oninput: link.callback(|e: InputData| Msg::UpdateLocality(e.value)),
                value: self.locality.clone(),
            },
            VCardPropertyInputField::Text{
                label: "Region".to_string(),
                id: Some("region".to_string()),
                placeholder: None,
                oninput: link.callback(|e: InputData| Msg::UpdateRegion(e.value)),
                value: self.region.clone(),
            },
            VCardPropertyInputField::Text{
                label: "Code".to_string(),
                id: Some("code".to_string()),
                placeholder: None,
                oninput: link.callback(|e: InputData| Msg::UpdateCode(e.value)),
                value: self.code.clone(),
            },
            VCardPropertyInputField::Text{
                label: "Country".to_string(),
                id: Some("country".to_string()),
                placeholder: None,
                oninput: link.callback(|e: InputData| Msg::UpdateCountry(e.value)),
                value: self.country.clone(),
            },
            VCardPropertyInputField::CheckBox{
                label: "Work".to_string(),
                id: Some("work".to_string()),
                onclick: link.callback(|_: MouseEvent| Msg::ToggleWork),
                value: self.work,
            },
            VCardPropertyInputField::CheckBox{
                label: "Home".to_string(),
                id: Some("home".to_string()),
                onclick: link.callback(|_: MouseEvent| Msg::ToggleHome),
                value: self.home,
            },
        ]
    }
    fn to_vcard_property(&self) -> Result<properties::Address, VCardPropertyInputError> {
        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();

            if self.work {
                type_values.insert(values::type_value::TypeValue::Work);
            }
            if self.home {
                type_values.insert(values::type_value::TypeValue::Home);
            }

            vcard::Set::from_hash_set(type_values).unwrap()
        };

        address.typ = Some(parameters::typ::Type::from_type_values(type_values));

        Ok(address)
    }
}