summaryrefslogtreecommitdiff
path: root/src/model/property_groups/other_identification.rs
blob: ca48375da46ab9397faf06b0ae16ceeb4c617d7a (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
use crate::model::input_fields::VCardPropertyInputField;
use crate::model::*;

/// Type that represents the vcard `anniversary` and `birthday` properties.
#[derive(Clone, Debug, PartialEq)]
pub struct OtherIdentification {
    pub nickname: String,
    pub photo: Option<File>,
    pub birthday: String,
    pub anniversary: String,
    pub gender: String,
}

#[derive(Clone, PartialEq)]
pub enum OtherIdentificationMsg {
    UpdateNickname(String),
    UpdatePhoto(Option<File>),
    UpdateBirthday(String),
    UpdateAnniversary(String),
    UpdateGender(String),

    Generate,

    Nope,
}

impl VCardPropertyInputGroupObject<OtherIdentificationMsg> for OtherIdentification {
    fn new() -> Self {
        Self {
            nickname: String::new(),
            photo: None,
            birthday: String::new(),
            anniversary: String::new(),
            gender: String::new(),
        }
    }
    fn get_title(&self) -> std::string::String {
        "Other Identification Properties".to_string()
    }
    fn get_input_fields(
        &self,
        link: &yew::html::Scope<PropertyGroupInputComponent<Self, OtherIdentificationMsg>>,
    ) -> std::vec::Vec<VCardPropertyInputField> {
        vec![
            VCardPropertyInputField::Text {
                label: String::from("Nickname"),
                id: Some(String::from("nickname")),
                placeholder: None,
                oninput: link
                    .callback(|e: InputData| OtherIdentificationMsg::UpdateNickname(e.value)),
                value: self.nickname.clone(),
                typ: String::from("text"),
            },
            VCardPropertyInputField::File {
                label: String::from("Photo"),
                name: String::from("photo"),
                callback: link
                    .callback(|file: Option<File>| OtherIdentificationMsg::UpdatePhoto(file)),
                value: self.photo.clone(),
            },
            VCardPropertyInputField::Text {
                label: "Birthday".to_string(),
                id: Some("birthday".to_string()),
                placeholder: None,
                oninput: link
                    .callback(|e: InputData| OtherIdentificationMsg::UpdateBirthday(e.value)),
                value: self.birthday.clone(),
                typ: String::from("date"),
            },
            VCardPropertyInputField::Text {
                label: "Anniversary".to_string(),
                id: Some("anniversary".to_string()),
                placeholder: None,
                oninput: link
                    .callback(|e: InputData| OtherIdentificationMsg::UpdateAnniversary(e.value)),
                value: self.anniversary.clone(),
                typ: String::from("date"),
            },
            VCardPropertyInputField::Select {
                label: String::from("Gender"),
                id: Some(String::from("gender")),
                options: vec![
                    (String::from(""), String::from("Select one")),
                    (String::from("M"), String::from("Male")),
                    (String::from("F"), String::from("Female")),
                    (String::from("O"), String::from("Other")),
                    (String::from("N"), String::from("Not applicable")),
                    (String::from("U"), String::from("Unknown")),
                ],
                onchange: link.callback(|c: ChangeData| match c {
                    ChangeData::Select(v) => OtherIdentificationMsg::UpdateGender(v.value()),
                    _ => OtherIdentificationMsg::Nope,
                }),
                value: self.gender.clone(),
            },
        ]
    }
    fn update(
        &mut self,
        props: InputProps<Self, OtherIdentificationMsg>,
        msg: <PropertyGroupInputComponent<Self, OtherIdentificationMsg> as yew::Component>::Message,
    ) -> bool {
        match msg {
            OtherIdentificationMsg::UpdateNickname(n) => self.nickname = n,
            OtherIdentificationMsg::UpdatePhoto(p) => self.photo = p,
            OtherIdentificationMsg::UpdateBirthday(b) => self.birthday = b,
            OtherIdentificationMsg::UpdateAnniversary(a) => self.anniversary = a,
            OtherIdentificationMsg::UpdateGender(g) => self.gender = g,
            OtherIdentificationMsg::Generate => props.generated.emit(self.clone()),
            OtherIdentificationMsg::Nope => (),
        };
        true
    }
    fn is_empty(&self) -> bool {
        self.anniversary.is_empty() && self.birthday.is_empty()
    }
}