diff options
Diffstat (limited to 'src/view/name.rs')
-rw-r--r-- | src/view/name.rs | 57 |
1 files changed, 37 insertions, 20 deletions
diff --git a/src/view/name.rs b/src/view/name.rs index bee9256..5b64bae 100644 --- a/src/view/name.rs +++ b/src/view/name.rs @@ -1,14 +1,17 @@ +use yew::services::ConsoleService; +use yewtil::ptr::Irc; +use yewtil::ptr::Mrc; use crate::viewmodel::Error; use crate::view::WeakComponentLink; use yew::prelude::*; -use vobject::Property; +use yewtil::NeqAssign; use crate::viewmodel::name::*; use crate::viewmodel::VCardPropertyInputObject; use super::VCardPropertyInputComponent; pub struct NameView { props: Props, - value: Name, + value: Mrc<Name>, error: Option<Error>, } @@ -24,14 +27,19 @@ pub enum Msg { #[derive(Clone, PartialEq, Properties)] pub struct Props { - pub generated_name: Callback<Name>, - pub generated_fn: Callback<String>, + pub generated: Callback<Irc<Name>>, pub weak_link: WeakComponentLink<NameView>, } impl VCardPropertyInputComponent<Name> for NameView { fn get_input_object(&self) -> Name { - self.value.clone() + match self.value.clone().try_unwrap() { + Ok(name) => name, + Err(_) => { + ConsoleService::error("Couldn't unwrap name"); + Name::new() + }, + } } fn get_title(&self) -> String { "Name".to_string() @@ -48,31 +56,40 @@ impl Component for NameView { props.weak_link.borrow_mut().replace(link); Self { props, - value: Name::new(), + value: Mrc::new(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 => { - self.props.generated_fn.emit(self.value.generate_fn()); - self.props.generated_name.emit(self.value); + Msg::UpdatePrefix(p) => match self.value.get_mut() { + Some(value) => value.prefix = p, + None => ConsoleService::info("Couldn't get mutable reference to name"), + }, + Msg::UpdateFirstName(f) => match self.value.get_mut() { + Some(value) => value.first_name = f, + None => ConsoleService::info("Couldn't get mutable reference to name"), + }, + Msg::UpdateMiddleName(m) => match self.value.get_mut() { + Some(value) => value.middle_name = m, + None => ConsoleService::info("Couldn't get mutable reference to name"), + }, + Msg::UpdateLastName(l) => match self.value.get_mut() { + Some(value) => value.last_name = l, + None => ConsoleService::info("Couldn't get mutable reference to name"), + }, + Msg::UpdateSuffix(s) => match self.value.get_mut() { + Some(value) => value.suffix = s, + None => ConsoleService::info("Couldn't get mutable reference to name"), + }, + Msg::Generate => { + self.props.generated.emit(self.value.irc()); }, }; true } fn change(&mut self, props: <Self as yew::Component>::Properties) -> bool { - if self.props != props { - self.props = props; - true - } else { - false - } + self.props.neq_assign(props) } fn view(&self) -> yew::virtual_dom::VNode { let link = self.props.weak_link.borrow().clone().unwrap(); |