-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from picture-pro/42-show-qr-code-on-upload
feat: prototyped qr code page and integration
- Loading branch information
Showing
16 changed files
with
144 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod fetch; | ||
pub mod model_ext; | ||
pub mod qr_code; | ||
pub mod upload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use color_eyre::eyre::{Result, WrapErr}; | ||
use qrcode::QrCode; | ||
use tracing::instrument; | ||
|
||
/// Generate a QR code from the given data. Returns base64 encoded PNG data. | ||
#[instrument] | ||
pub fn generate_qr_code(data: &str) -> Result<String> { | ||
let code = | ||
QrCode::new(data.as_bytes()).wrap_err("Failed to create QR code")?; | ||
let image = code.render::<image::Luma<u8>>().build(); | ||
let mut png_bytes = Vec::new(); | ||
let encoder = image::codecs::jpeg::JpegEncoder::new(&mut png_bytes); | ||
image | ||
.write_with_encoder(encoder) | ||
.wrap_err("Failed to write QR code to png")?; | ||
|
||
use base64::prelude::*; | ||
let data = BASE64_STANDARD.encode(&png_bytes); | ||
|
||
Ok(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ pub mod auth; | |
pub mod dashboard; | ||
pub mod home_page; | ||
pub mod purchase; | ||
pub mod qr_code; | ||
|
||
use leptos::*; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use leptos::*; | ||
use leptos_router::use_params_map; | ||
|
||
use crate::pages::SmallPageWrapper; | ||
|
||
#[component] | ||
pub fn QrCodePage() -> impl IntoView { | ||
let params = use_params_map(); | ||
let id = params().get("id").cloned(); | ||
|
||
// routing makes sure that the ID param exists | ||
let id = id.unwrap(); | ||
let url = format!( | ||
"{}/photo/{id}", | ||
std::env::var("APP_BASE_URL").expect("APP_BASE_URL not set"), | ||
); | ||
|
||
view! { | ||
<SmallPageWrapper> | ||
<div class="d-card-body gap-4"> | ||
<p class="text-2xl font-semibold tracking-tight">"QR Code"</p> | ||
<QrCode data=url class="rounded-box border shadow" /> | ||
<div class="flex flex-row items-center gap-4"> | ||
<a href="/" class="d-btn d-btn-primary d-btn-sm">"Back to Dashboard"</a> | ||
<div class="flex-1" /> | ||
<a href={format!("/photo/{}", id)} class="d-btn d-btn-sm">"View Photo"</a> | ||
</div> | ||
</div> | ||
</SmallPageWrapper> | ||
} | ||
} | ||
|
||
#[component] | ||
pub fn QrCode( | ||
data: String, | ||
#[prop(default = "")] class: &'static str, | ||
) -> impl IntoView { | ||
let qr_code = create_resource(move || data.clone(), generate_qr_code); | ||
|
||
view! { | ||
<Suspense fallback=|| view!{}> | ||
{ qr_code.map(|r| { | ||
match r { | ||
Ok(qr_code) => view! { | ||
<img src={format!("data:image/png;base64,{}", qr_code)} class=class /> | ||
}.into_view(), | ||
Err(e) => view! { | ||
<div> | ||
<p>{e.to_string()}</p> | ||
<p>"Failed to generate QR code"</p> | ||
</div> | ||
}.into_view(), | ||
} | ||
})} | ||
</Suspense> | ||
} | ||
} | ||
|
||
#[server] | ||
pub async fn generate_qr_code(data: String) -> Result<String, ServerFnError> { | ||
bl::qr_code::generate_qr_code(&data).map_err(|e| { | ||
let error = e.to_string(); | ||
logging::error!("Failed to generate QR code: {}", error); | ||
ServerFnError::new(error) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters