Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions components/user/UserProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { FaShare } from "react-icons/fa6";
import { QRCodeCanvas } from "qrcode.react";
import { saveAs } from "file-saver";
import { useRouter } from "next/router";
import { toPng } from "html-to-image";
import LogoWide from "@public/logos/LogoWide";

import FallbackImage from "@components/FallbackImage";
import UserSocial from "./UserSocials";
Expand All @@ -23,13 +25,38 @@ function UserProfile({ BASE_URL, data }) {

//Declared Ref object for QR
const qrRef = useRef(null);
const elementRef = useRef(null);

//qrRef.current is pointing to the DOM node and firstChild to its canvas
const downloadQR = () =>
qrRef.current.firstChild.toBlob((blob) =>
saveAs(blob, `biodrop-${data.username}.png`),
);

const downloadWallpaper = async () => {
try {
// It removes the "hidden" class to make the element visible for rendering it.
elementRef.current.classList.remove("hidden");

const dataUrl = await toPng(elementRef.current, {
cacheBust: false,
backgroundColor: "#122640",
width: 1080,
height: 1920,
});

const link = document.createElement("a");
link.download = "Biodrop-QRCode-Wallpaper.png";
link.href = dataUrl;
link.click();

// It adds the "hidden" class back to hide the element again.
elementRef.current.classList.add("hidden");
} catch (e) {
console.log(e);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be replaced with Logger

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try from an existing example pages/account/manage/profile.js

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever I use Logger in the file. I get logged out. I have attached the screenshot below:

WIth console.log in the file
consolelogin

Whenever I replace console with logger in the file, it changes Account into Login. Then when I presss the FaShare icon it throws ADMIN_USERS NOT FOUND(may be the "Account-to-Login Change" has something to do with the error")
loggererror

}
};

return (
<>
<div className="flex justify-center items-center flex-col md:flex-row gap-x-6">
Expand Down Expand Up @@ -128,6 +155,13 @@ function UserProfile({ BASE_URL, data }) {
</Button>
)}
</div>
<div className="w-full px-2 mx-auto flex justify-center mb-4">
{qrShow && (
<Button primary={true} onClick={downloadWallpaper}>
Export as Wallpaper
</Button>
)}
</div>
</div>
{qrShow && (
<>
Expand Down Expand Up @@ -159,6 +193,22 @@ function UserProfile({ BASE_URL, data }) {
</>
)}
</div>
{/* Part that gets converted into the image */}
<div
ref={elementRef}
className="flex flex-col items-center justify-center mt-10 px-8 hidden"
>
{qrShow && (
<QRCodeCanvas
className="border border-white"
value={`${BASE_URL}/${data.username}`}
size={fallbackImageSize * 6}
/>
)}
<div className="flex m-20">
<LogoWide width={512} />
</div>
</div>
</Modal>
</>
);
Expand Down
8 changes: 7 additions & 1 deletion package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"autoprefixer": "^10.4.16",
"dotenv": "^16.3.1",
"file-saver": "^2.0.5",
"html-to-image": "^1.11.11",
"husky": "^8.0.3",
"leaflet": "^1.9.4",
"micro": "^10.0.1",
Expand Down