summaryrefslogtreecommitdiff
path: root/src/name.rs
blob: 4b51def340037d75808cb2d0a6ef26d133c731e2 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use yew::prelude::*;
use vcard::properties;
use vcard::values::{self, text};

#[derive(Clone)]
pub struct Name {
    pub prefix: String,
    pub first_name: String,
    pub middle_name: String,
    pub last_name: String,
    pub suffix: String,
}

impl Name {
    pub fn new() -> Self { 
        Self {
            prefix: String::new(),
            first_name: String::new(),
            middle_name: String::new(),
            last_name: String::new(),
            suffix: String::new(),
        }
    }
    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
    }
    pub fn to_vcard_name(&self) -> properties::Name {
        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()),
            },
        );

        properties::Name::from_name_value(name_value)
    }
}

pub struct NameView {
    link: ComponentLink<Self>,
    value: Name,
    oninput: Callback<Name>,
    //errors: Vec<String>,
}

pub enum Msg {
    UpdatePrefix(String),
    UpdateFirstName(String),
    UpdateMiddleName(String),
    UpdateLastName(String),
    UpdateSuffix(String),
}

#[derive(Clone, PartialEq, Properties)]
pub struct Props {
    pub oninput: Callback<Name>,
    //pub errors: Vec<String>,
}

impl Component for NameView {
    type Message = Msg;
    type Properties = Props;
    fn create(props: <Self as yew::Component>::Properties, link: yew::html::Scope<Self>) -> Self {
        Self {
            link,
            value: Name::new(),
            oninput: props.oninput,
        }
    }
    fn update(&mut self, msg: <Self as yew::Component>::Message) -> bool {
        match msg {
            Msg::UpdatePrefix(p) => self.value.prefix = p,
            Msg::UpdateFirstName(f) => self.value.first_name = f,
            Msg::UpdateMiddleName(m) => self.value.middle_name = m,
            Msg::UpdateLastName(l) => self.value.last_name = l,
            Msg::UpdateSuffix(s) => self.value.suffix = s,
        };
        self.oninput.emit(self.value.clone());
        true
    }
    fn change(&mut self, props: <Self as yew::Component>::Properties) -> bool { 
        self.oninput = props.oninput;
        true
    }
    fn view(&self) -> yew::virtual_dom::VNode {
        html!{
            <div class="box">
                <h3 class="subtitle">{ "Name" }</h3>

                <div class="columns is-mobile is-multiline">
                
                    <div class="field column is-one-fifth-widescreen is-one-quarter-desktop is-one-third-tablet is-half-mobile">
                        <label class="label">{ "Prefix" }</label>
                        <div class="control">
                            <input  id="prefix" 
                                    type="text" 
                                    placeholder="Sir" 
                                    oninput=self.link.callback(|e: InputData| Msg::UpdatePrefix(e.value))
                            />
                        </div>
                    </div>

                    <div class="field column is-one-fifth-widescreen is-one-quarter-desktop is-one-third-tablet is-half-mobile">
                        <label class="label">{ "First name" }</label>
                        <div class="control">
                            <input  id="first_name" 
                                    type="text" 
                                    placeholder="Arthur" 
                                    oninput=self.link.callback(|e: InputData| Msg::UpdateFirstName(e.value))
                            />
                        </div>
                    </div>

                    <div class="field column is-one-fifth-widescreen is-one-quarter-desktop is-one-third-tablet is-half-mobile">
                        <label class="label">{ "Middle name" }</label>
                        <div class="control">
                            <input  id="middle_name" 
                                    type="text" 
                                    placeholder="Charles"
                                    oninput=self.link.callback(|e: InputData| Msg::UpdateMiddleName(e.value))
                            />
                        </div>
                    </div>
                    
                    <div class="field column is-one-fifth-widescreen is-one-quarter-desktop is-one-third-tablet is-half-mobile">
                        <label class="label">{ "Last name" }</label>
                        <div class="control">
                            <input  id="last_name"
                                    type="text" 
                                    placeholder="Clarke" 
                                    oninput=self.link.callback(|e: InputData| Msg::UpdateLastName(e.value))
                            />
                        </div>
                    </div>

                    <div class="field column is-one-fifth-widescreen is-one-quarter-desktop is-one-third-tablet is-half-mobile">
                        <label class="label">{ "Suffix" }</label>
                        <div class="control">
                            <input  id="suffix" 
                                    type="text" 
                                    placeholder="CBE FRAS" 
                                    oninput=self.link.callback(|e: InputData| Msg::UpdateSuffix(e.value))
                            />
                        </div>
                    </div>

                </div>
            </div>
        }
    }
}