From 10eb0e8cff7f0bc6b61a77a179fec55ee1dfe929 Mon Sep 17 00:00:00 2001 From: jelemux Date: Wed, 21 Oct 2020 22:01:59 +0200 Subject: basic prototype with vcard and pdf generation --- src/view.rs | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 103 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/view.rs b/src/view.rs index 38cfa58..6953496 100644 --- a/src/view.rs +++ b/src/view.rs @@ -1,21 +1,49 @@ +extern crate console_error_panic_hook; use crate::pdfgen; use wasm_bindgen::prelude::*; use js_sys; use yew::prelude::*; +use vcard::{VCard, VCardError}; +use std::panic; -struct Form { +fn init() { + panic::set_hook(Box::new(console_error_panic_hook::hook)); +} + +pub struct Form { + link: ComponentLink, + error: Option, + formatted_name: String, + generated_vcard: Option, +} + +pub enum Msg { + UpdateFormattedName(String), + GenerateVCard, + Nope, } impl Component for Form { - type Message = (); + type Message = Msg; type Properties = (); - fn create(_props: Self::Properties, _link: ComponentLink) -> Self { - Self { } + fn create(_props: Self::Properties, link: ComponentLink) -> Self { + Self { link, error: None, formatted_name: String::new(), generated_vcard: None } } - fn update(&mut self, _msg: Self::Message) -> ShouldRender { - false + fn update(&mut self, msg: Self::Message) -> ShouldRender { + match msg { + Msg::UpdateFormattedName(value) => self.formatted_name = String::from(value), + Msg::GenerateVCard => { + match self.generate_vcard() { + Ok(vcard) => self.generated_vcard = Some(vcard.to_string()), + Err(VCardError::FormatError(err)) => self.error = Some(err.to_string()), + Err(VCardError::EmptyFormatName) => self.error = Some(String::from("A VCard should have at least one formatted name.")), + }; + } + Msg::Nope => (), + }; + true } fn change(&mut self, _props: Self::Properties) -> ShouldRender { @@ -23,19 +51,87 @@ impl Component for Form { } fn view(&self) -> Html { + + let raw = pdfgen::genpdf(); + let data = base64::encode(&raw); + let uri_component: String = js_sys::encode_uri_component(&data).into(); + let href = format!{"data:application/pdf;base64,{}", uri_component }; + + let formatted_name_input = self.link.callback(|e: ChangeData| + match e { + ChangeData::Value(v) => Msg::UpdateFormattedName(v), + _ => Msg::Nope, + } + ); + + html!{ +
+ { self.render_error() } + + + + + + + + + + { self.render_pdf() } { self.render_vcard() } +
+ } + } +} + +impl Form { + fn generate_vcard(&self) -> Result { + match VCard::from_formatted_name_str(&self.formatted_name) { + Ok(vcard) => Ok(vcard), + Err(err) => Err(err), + } + } + fn render_error(&self) -> Html { + if self.error.is_some() { + html!{ +
+

{ self.error.as_ref().unwrap() }

+
+ } + } else { + html!{} + } + } + fn render_pdf(&self) -> Html { let raw = pdfgen::genpdf(); let data = base64::encode(&raw); let uri_component: String = js_sys::encode_uri_component(&data).into(); let href = format!{"data:application/pdf;base64,{}", uri_component }; + html!{ - + { "Download PDF" } } } + fn render_vcard(&self) -> Html { + if self.generated_vcard.is_some() { + let data = base64::encode(self.generated_vcard.as_ref().unwrap()); + let uri_component: String = js_sys::encode_uri_component(&data).into(); + let href = format!("data:text/vcard;base64,{}", uri_component); + + html!{ + + { "Download vCard" } + + } + } else { + html!{} + } + } } + #[wasm_bindgen(start)] pub fn run_app() { + init(); App::
::new().mount_to_body(); } \ No newline at end of file -- cgit v1.2.3