Add download qrcode to table
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use fluent::{FluentArgs, FluentBundle, FluentResource};
|
||||
use seed::log;
|
||||
use strum_macros::{AsRefStr, Display, EnumIter, EnumString};
|
||||
use unic_langid::LanguageIdentifier;
|
||||
|
||||
@@ -42,6 +43,7 @@ impl I18n {
|
||||
|
||||
/// Get a localized string. Optionally with parameters provided in `args`.
|
||||
pub fn translate(&self, key: impl AsRef<str>, args: Option<&FluentArgs>) -> String {
|
||||
log!(key.as_ref());
|
||||
let msg = self
|
||||
.ftl_bundle
|
||||
.get_message(key.as_ref())
|
||||
|
||||
+37
-9
@@ -4,6 +4,7 @@ pub mod pages;
|
||||
|
||||
use pages::list_links;
|
||||
use pages::list_users;
|
||||
use seed::window;
|
||||
use seed::{attrs, button, div, input, label, log, prelude::*, App, Url, C};
|
||||
use shared::apirequests::users::LoginUser;
|
||||
use shared::datatypes::{Loadable, User};
|
||||
@@ -18,14 +19,11 @@ fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
|
||||
orders.subscribe(Msg::UrlChanged);
|
||||
orders.send_msg(Msg::GetLoggedUser);
|
||||
|
||||
log!(&url);
|
||||
|
||||
let lang = I18n::new(Lang::EnUS);
|
||||
|
||||
Model {
|
||||
index: 0,
|
||||
base_url: Url::new().add_path_part("app"),
|
||||
current_url: url.clone(),
|
||||
location: Location::new(url.clone()),
|
||||
page: Page::init(url, orders, lang.clone()),
|
||||
i18n: lang,
|
||||
user: Loadable::Data(None),
|
||||
@@ -41,8 +39,7 @@ fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
|
||||
#[derive(Debug)]
|
||||
struct Model {
|
||||
index: usize,
|
||||
base_url: Url,
|
||||
current_url: Url,
|
||||
location: Location,
|
||||
page: Page,
|
||||
i18n: i18n::I18n,
|
||||
user: Loadable<User>,
|
||||
@@ -56,6 +53,32 @@ struct LoginForm {
|
||||
password: ElRef<web_sys::HtmlInputElement>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Location {
|
||||
host: String,
|
||||
base_url: Url,
|
||||
current_url: Url,
|
||||
}
|
||||
|
||||
impl Location {
|
||||
fn new(url: Url) -> Self {
|
||||
let host = get_host();
|
||||
Self {
|
||||
host,
|
||||
base_url: Url::new().add_path_part("app"),
|
||||
current_url: url,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn get_host() -> String {
|
||||
window()
|
||||
.location()
|
||||
.host()
|
||||
.expect("Failed to extract the host of the url")
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Page {
|
||||
Home(pages::list_links::Model),
|
||||
@@ -65,6 +88,7 @@ enum Page {
|
||||
|
||||
impl Page {
|
||||
fn init(mut url: Url, orders: &mut impl Orders<Msg>, i18n: I18n) -> Self {
|
||||
log!(&url);
|
||||
url.next_path_part();
|
||||
let result = match url.next_path_part() {
|
||||
None | Some("list_links") => Self::Home(pages::list_links::init(
|
||||
@@ -140,7 +164,11 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
|
||||
}
|
||||
Msg::UserReceived(user) => {
|
||||
model.user = Loadable::Data(Some(user));
|
||||
model.page = Page::init(model.current_url.clone(), orders, model.i18n.clone());
|
||||
model.page = Page::init(
|
||||
model.location.current_url.clone(),
|
||||
orders,
|
||||
model.i18n.clone(),
|
||||
);
|
||||
}
|
||||
Msg::NotAuthenticated => {
|
||||
if model.user.is_some() {
|
||||
@@ -252,8 +280,8 @@ fn view(model: &Model) -> Node<Msg> {
|
||||
C!["page"],
|
||||
match model.user {
|
||||
Loadable::Data(Some(ref user)) => div![
|
||||
navigation::navigation(&model.i18n, &model.base_url, user),
|
||||
view_content(&model.page, &model.base_url)
|
||||
navigation::navigation(&model.i18n, &model.location.base_url, user),
|
||||
view_content(&model.page, &model.location.base_url)
|
||||
],
|
||||
Loadable::Data(None) => view_login(&model.i18n, model),
|
||||
Loadable::Loading => div![C!("lds-ellipsis"), div!(), div!(), div!(), div!()],
|
||||
|
||||
@@ -17,7 +17,7 @@ use shared::{
|
||||
datatypes::FullLink,
|
||||
};
|
||||
|
||||
use crate::{i18n::I18n, unwrap_or_return, unwrap_or_send};
|
||||
use crate::{get_host, i18n::I18n, unwrap_or_return, unwrap_or_send};
|
||||
|
||||
/// Setup the page
|
||||
pub fn init(mut url: Url, orders: &mut impl Orders<Msg>, i18n: I18n) -> Model {
|
||||
@@ -543,6 +543,15 @@ fn view_link(l: &FullLink) -> Node<Msg> {
|
||||
&l.clicks.number
|
||||
]
|
||||
},
|
||||
{
|
||||
td![
|
||||
C!["table_qr"],
|
||||
a![
|
||||
attrs![At::Href => format!["http://localhost:8080/admin/download/png/{}", &l.link.code], At::Download => true.as_at_value()],
|
||||
raw!(&generate_qr_from_code(&l.link.code))
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
td![img![
|
||||
ev(Ev::Click, |_| Msg::Edit(EditMsg::MayDeleteSelected(link5))),
|
||||
@@ -602,7 +611,7 @@ fn edit_or_create_link<F: Fn(&str) -> String>(l: &RefCell<LinkDelta>, t: F) -> N
|
||||
],
|
||||
tr![
|
||||
th![t("qr-code")],
|
||||
td![raw!(&generate_qr_code(&format!("http://{}", &link.code)))]
|
||||
td![raw!(&generate_qr_from_code(&link.code))]
|
||||
]
|
||||
],
|
||||
a![
|
||||
@@ -616,8 +625,12 @@ fn edit_or_create_link<F: Fn(&str) -> String>(l: &RefCell<LinkDelta>, t: F) -> N
|
||||
]
|
||||
}
|
||||
|
||||
fn generate_qr_code(link: &str) -> String {
|
||||
if let Ok(qr) = QrCode::with_error_correction_level(&link, qrcode::EcLevel::L) {
|
||||
fn generate_qr_from_code(code: &str) -> String {
|
||||
generate_qr_from_link(&format!("https://{}/{}", get_host(), code))
|
||||
}
|
||||
|
||||
fn generate_qr_from_link(url: &str) -> String {
|
||||
if let Ok(qr) = QrCode::with_error_correction_level(&url, qrcode::EcLevel::L) {
|
||||
let svg = qr
|
||||
.render()
|
||||
.min_dimensions(100, 100)
|
||||
|
||||
Reference in New Issue
Block a user