Skip to content

Commit

Permalink
feat(fe): Added QR codes
Browse files Browse the repository at this point in the history
  • Loading branch information
MendyBerger committed Mar 19, 2024
1 parent 4ad28af commit 99e1ce3
Show file tree
Hide file tree
Showing 19 changed files with 394 additions and 37 deletions.
127 changes: 127 additions & 0 deletions frontend/apps/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const_format = "0.2.5"
num-traits = "0.2"
num-derive = "0.3"
csv = "1.3"
qrcode-generator = "4.1.9"
web-sys = { version = "0.3.55", features = [
'Url',
'Request',
Expand Down
3 changes: 3 additions & 0 deletions frontend/apps/crates/components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ rgb = { workspace = true }
regex = { workspace = true }
num-traits = { workspace = true }
num-derive = { workspace = true }
qrcode-generator = { workspace = true }

[features]
quiet = ["utils/quiet"]
Expand Down Expand Up @@ -88,6 +89,7 @@ hebrew_buttons = []
page_header = []
page_footer = []
pdf = []
qr_dialog = []
share_jig = []
player_popup = []
box_outline = []
Expand Down Expand Up @@ -135,6 +137,7 @@ all = [
"page_header",
"page_footer",
"pdf",
"qr_dialog",
"share_jig",
"player_popup",
"box_outline",
Expand Down
2 changes: 2 additions & 0 deletions frontend/apps/crates/components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub mod page_header;
pub mod pdf;
#[cfg(feature = "player_popup")]
pub mod player_popup;
#[cfg(feature = "qr_dialog")]
pub mod qr_dialog;
#[cfg(feature = "share_jig")]
pub mod share_asset;
#[cfg(feature = "stickers")]
Expand Down
11 changes: 11 additions & 0 deletions frontend/apps/crates/components/src/qr_dialog/actions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::rc::Rc;

use utils::js_wrappers::download_url;

use super::QrDialog;

impl QrDialog {
pub fn download(self: &Rc<Self>) {
download_url(&self.file_label, &self.url)
}
}
11 changes: 11 additions & 0 deletions frontend/apps/crates/components/src/qr_dialog/callbacks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub struct QrDialogCallbacks {
pub on_close: Box<dyn Fn()>,
}

impl QrDialogCallbacks {
pub fn new(on_close: impl Fn() + 'static) -> Self {
Self {
on_close: Box::new(on_close),
}
}
}
50 changes: 50 additions & 0 deletions frontend/apps/crates/components/src/qr_dialog/dom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::rc::Rc;

use dominator::{clone, DomBuilder, EventOptions};
use utils::{component::Component, dialog, events};
use web_sys::ShadowRoot;

use super::QrDialog;

impl Component<QrDialog> for Rc<QrDialog> {
fn styles() -> &'static str {
include_str!("./styles.css")
}

fn dom(&self, dom: DomBuilder<ShadowRoot>) -> DomBuilder<ShadowRoot> {
let state = self;
dom.child(dialog! {
.class("qr-dialog")
.event_with_options(&EventOptions::bubbles(), clone!(state => move |e: events::Click| {
e.stop_propagation();
(state.callbacks.on_close)();
}))
.child(html!("div", {
.class("body")
.event_with_options(&EventOptions::bubbles(), |e: events::Click| {
e.stop_propagation();
})
.child(html!("fa-button", {
.class("close")
.prop("icon", "fa-regular fa-xmark")
.event(clone!(state => move |_: events::Click| {
(state.callbacks.on_close)();
}))
}))
.child(html!("img", {
.style("max-height", "5cm")
.style("max-width", "5cm")
.prop("src", &state.url)
}))
.child(html!("fa-button", {
.class("download")
.prop("icon", "fa-solid fa-square-down")
.prop("title", "Download")
.event(clone!(state => move |_: events::Click| {
state.download();
}))
}))
}))
})
}
}
8 changes: 8 additions & 0 deletions frontend/apps/crates/components/src/qr_dialog/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod actions;
mod callbacks;
mod dom;
mod state;

pub use callbacks::*;
pub use dom::*;
pub use state::*;
45 changes: 45 additions & 0 deletions frontend/apps/crates/components/src/qr_dialog/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::rc::Rc;

use qrcode_generator::QrCodeEcc;
use utils::{routes::Route, unwrap::UnwrapJiExt};
use wasm_bindgen::JsValue;

use super::QrDialogCallbacks;

pub struct QrDialog {
pub url: String,
pub file_label: String,
pub callbacks: QrDialogCallbacks,
}

impl QrDialog {
pub fn new(route: Route, file_label: String, callbacks: QrDialogCallbacks) -> Rc<Self> {
let url = qr_core_file_from_route(route);
Rc::new(Self {
url,
file_label,
callbacks,
})
}
}

fn file_to_object_url(filetype: &str, data: &str) -> String {
let data = JsValue::from_str(data);
let blob = web_sys::Blob::new_with_str_sequence_and_options(
&js_sys::Array::from_iter(vec![data]),
web_sys::BlobPropertyBag::new().type_(filetype),
)
.unwrap_ji();
let url = web_sys::Url::create_object_url_with_blob(&blob).unwrap_ji();
url
}

pub fn qr_core_file_from_route(route: Route) -> String {
let result: String =
qrcode_generator::to_svg_to_string(route.to_string(), QrCodeEcc::High, 200, None::<&str>)
.unwrap();

let url = file_to_object_url("image/svg+xml", &result);

url
}
Loading

0 comments on commit 99e1ce3

Please sign in to comment.