summaryrefslogtreecommitdiff
path: root/src/view/name.rs
blob: df3fb1566017aebbe431f23dff5f1b32c77b9b83 (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
use crate::viewmodel::Error;
use crate::view::WeakComponentLink;
use yew::prelude::*;
use vcard::properties;
use crate::viewmodel::name::*;
use crate::viewmodel::VCardPropertyInputObject;
use super::VCardPropertyInputComponent;

pub struct NameView {
    props: Props,
    value: Name,
    error: Option<Error>,
}

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

    Generate,
}

#[derive(Clone, PartialEq, Properties)]
pub struct Props {
    pub generated_name: Callback<Result<properties::Name,()>>,
    pub generated_fn: Callback<Result<properties::FormattedName,()>>,
    pub weak_link: WeakComponentLink<NameView>,
}

impl VCardPropertyInputComponent<properties::Name, Name> for NameView {
    fn get_input_object(&self) -> Name {
        self.value.clone()
    }
    fn get_title(&self) -> String {
        "Name".to_string()
    }
    fn get_error(&self) -> Option<Error> {
        self.error.clone()
    }
}

impl Component for NameView {
    type Message = Msg;
    type Properties = Props;
    fn create(props: <Self as yew::Component>::Properties, link: yew::html::Scope<Self>) -> Self {
        props.weak_link.borrow_mut().replace(link);
        Self {
            props,
            value: Name::new(),
            error: None,
        }
    }
    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,
            Generate => {
                match self.value.formatted_name() {
                    Ok(formatted_name) => self.props.generated_fn.emit(Ok(formatted_name)),
                    Err(error) => {
                        self.props.generated_fn.emit(Err(()));
                        self.error = Some(error);
                    },
                };
                match self.value.to_vcard_property() {
                    Ok(name) => {
                        self.props.generated_name.emit(Ok(name));
                        return false;
                    },
                    Err(error) => {
                        self.props.generated_name.emit(Err(()));
                        self.error = Some(error);
                    },
                };
            },
        };
        true
    }
    fn change(&mut self, props: <Self as yew::Component>::Properties) -> bool { 
        if self.props != props {
            self.props = props;
            true
        } else {
            false
        }
    }
    fn view(&self) -> yew::virtual_dom::VNode {
        let link = self.props.weak_link.borrow().clone().unwrap();

        html!{
            <div class="box">
                { self.render_error() }

                <h3 class="subtitle">{ self.get_title() }</h3>

                { self.get_input_object().render(&link) }

            </div>
        }
    }
}