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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
extern crate console_error_panic_hook;
use qrcodegen::QrCode;
use qrcodegen::QrCodeEcc;
use crate::pdfgen;
use wasm_bindgen::prelude::*;
use js_sys;
use yew::prelude::*;
use vcard::{VCard, VCardError};
use std::panic;
fn init() {
panic::set_hook(Box::new(console_error_panic_hook::hook));
}
pub struct Form {
link: ComponentLink<Self>,
error: Vec<String>,
formatted_name: String,
vcard: Option<String>,
qr_code: Option<String>,
}
pub enum Msg {
UpdateFormattedName(String),
GenerateVCard,
GeneratePdf,
GenerateQrCode,
Nope,
}
impl Component for Form {
type Message = Msg;
type Properties = ();
fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self {
Self { link, error: vec![], formatted_name: String::new(), vcard: None, qr_code: None }
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
self.error.clear();
match msg {
Msg::UpdateFormattedName(value) => self.formatted_name = String::from(value),
Msg::GenerateVCard => {
match self.generate_vcard() {
Ok(vcard) => self.vcard = Some(vcard.to_string()),
Err(VCardError::FormatError(err)) => self.error.push(err.to_string()),
Err(VCardError::EmptyFormatName) => self.error.push(String::from("A VCard should have at least one formatted name.")),
};
}
Msg::GeneratePdf => {
}
Msg::GenerateQrCode => {
match self.generate_vcard() {
Ok(vcard) => self.vcard = Some(vcard.to_string()),
Err(VCardError::FormatError(err)) => self.error.push(err.to_string()),
Err(VCardError::EmptyFormatName) => self.error.push(String::from("A VCard should have at least one formatted name.")),
};
if self.vcard.is_some() {
match QrCode::encode_text(self.vcard.as_ref().unwrap(), QrCodeEcc::Low) {
Ok(qr) => self.qr_code = Some(qr.to_svg_string(4)),
Err(_) => self.error.push(String::from("Sorry, VCard is too long!")),
};
}
}
Msg::Nope => return false,
};
if self.error.len() > 0 {
self.vcard = None;
self.qr_code = None;
}
true
}
fn change(&mut self, _props: Self::Properties) -> ShouldRender {
false
}
fn view(&self) -> Html {
let formatted_name_input = self.link.batch_callback(|e: InputData|
vec![Msg::UpdateFormattedName(e.value), Msg::GenerateVCard]
);
let download_options = self.link.callback(|e: ChangeData|
match e {
ChangeData::Select(v) => match v.value().as_str() {
"vcard" => Msg::GenerateVCard,
"pdf" => Msg::GeneratePdf,
"qrcode" => Msg::GenerateQrCode,
_ => Msg::Nope,
},
_ => Msg::Nope,
}
);
html!{
<div class="container">
<div class="banner row">
<div class="span twelve">
<h4>{ "A Generator for vCards" }</h4>
<h5>{ "Supports generating vCards (.vcf), print-ready PDF business cards and QR Codes" }</h5>
</div>
</div>
{ self.render_error() }
<h4 class="explainer">{ "Name" }</h4>
<form class="row">
<label for="formatted_name">{ "Formatted name: " }</label>
<input id="formatted_name" type="text" oninput=formatted_name_input/>
<br/>
<label for="prefix">{ "Prefix: " }</label>
<input id="prefix" type="text"/>
<br/>
<label for="first_name">{ "First name: " }</label>
<input id="first_name" type="text"/>
<label for="middle_name">{ "Middle name: " }</label>
<input id="middle_name" type="text"/>
<label for="last_name">{ "Last name: " }</label>
<input id="last_name" type="text"/>
<br/>
<label for="suffix">{ "Suffix: " }</label>
<input id="suffix" type="text"/>
</form>
<select id="download_options" onchange=download_options>
<option value="">{ "" }</option>
<option value="vcard">{ "VCard (.vcf)" }</option>
<option value="pdf">{ "Print-ready PDF" }</option>
<option value="qrcode">{ "QR Code" }</option>
</select>
{ self.render_pdf() } { self.render_vcard() }
<div class="row">
{ self.render_qrcode() }
</div>
</div>
}
}
}
impl Form {
fn generate_vcard(&self) -> Result<VCard, VCardError> {
match VCard::from_formatted_name_str(&self.formatted_name) {
Ok(vcard) => Ok(vcard),
Err(err) => Err(err),
}
}
fn render_error(&self) -> Html {
html!{
<>
{
for self.error.iter().map(|err|
html!{
<div class="alert danger">
<p>{ err }</p>
</div>
}
)
}
</>
}
}
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!{
<a href=href download="demo.pdf" class="button success small" >
{ "Download PDF" }
</a>
}
}
fn render_vcard(&self) -> Html {
if self.vcard.is_some() {
let data = base64::encode(self.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!{
<a href=href download=format!("{}.vcs",self.formatted_name) class="button success small">
{ "Download vCard" }
</a>
}
} else {
html!{}
}
}
fn render_qrcode(&self) -> Html {
if self.qr_code.is_some() {
let data = base64::encode(self.qr_code.as_ref().unwrap());
let uri_component: String = js_sys::encode_uri_component(&data).into();
let src = format!("data:image/svg+xml;base64,{}", uri_component);
html!{
<img src=src alt="QR Code" width="200" height="200"/>
}
} else {
html!{}
}
}
}
#[wasm_bindgen(start)]
pub fn run_app() {
init();
App::<Form>::new().mount_to_body();
}
|