summaryrefslogtreecommitdiff
path: root/src/view/property_group.rs
blob: 9c86e8097e3fe67a8473709549a4c655022f3bf7 (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
use crate::model::*;
use crate::view::weak_links::WeakComponentLink;
use yew::prelude::*;
use yewtil::NeqAssign;

#[derive(Clone, PartialEq, Properties)]
pub struct InputProps<
    O: 'static + VCardPropertyInputObject<M> + Clone,
    M: 'static + PartialEq + Clone,
> {
    pub generated: Callback<O>,
    pub weak_link: WeakComponentLink<PropertyGroupInputComponent<O, M>>,
}

#[derive(Clone, PartialEq)]
pub struct PropertyGroupInputComponent<
    O: 'static + VCardPropertyInputObject<M>,
    M: 'static + PartialEq + Clone,
> {
    pub props: InputProps<O, M>,
    pub value: O,
    pub error: Option<Error>,
}

impl<O: 'static + VCardPropertyInputObject<M>, M: 'static + PartialEq + Clone> Component
    for PropertyGroupInputComponent<O, M>
{
    type Message = M;
    type Properties = InputProps<O, M>;
    fn create(props: <Self as yew::Component>::Properties, link: yew::html::Scope<Self>) -> Self {
        props.weak_link.borrow_mut().replace(link);
        Self {
            props,
            value: O::new(),
            error: None,
        }
    }
    fn update(&mut self, msg: <Self as yew::Component>::Message) -> bool {
        self.value.update(self.props.clone(), msg)
    }
    fn change(&mut self, props: <Self as yew::Component>::Properties) -> bool {
        self.props.neq_assign(props)
    }
    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.value.get_title() }</h3>

                { self.value.render(&link) }

            </div>
        }
    }
}

impl<O: VCardPropertyInputObject<M>, M: 'static + PartialEq + Clone>
    PropertyGroupInputComponent<O, M>
{
    /// Returns the error as `Html`
    fn render_error(&self) -> Html {
        html! {
            <>
                {
                    if self.error.is_some() {
                        html!{
                            <div class="notification is-danger is-light">
                                { self.error.clone().unwrap().msg }
                            </div>
                        }
                    } else {
                        html!{}
                    }
                }
            </>
        }
    }
}