"use client"; import React, { useState } from "react"; import Navbar from "../../components/Navbar"; import Footer from "../../components/Footer"; import Button from "../../components/Button"; import Image from "next/image"; export default function QrCodeGenerator() { const [loading, setLoading] = useState(false); const [QRCodeURL, setQRCodeURL] = useState(null); const downloadImage = (dataUrl: string) => { const link = document.createElement("a"); link.href = dataUrl; link.download = "qrcode.png"; document.body.appendChild(link); link.click(); document.body.removeChild(link); }; const generateQRCode = async () => { setLoading(true); const textInput = (document.getElementById("data") as HTMLInputElement) .value; try { const response = await fetch( process.env.backend_url + "/api/generate-qrcode", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ qrcodeContent: textInput, }), }, ); if (!response.ok) { console.error("Error while converting"); } const data = await response.json(); if (data.qrCode) { setQRCodeURL(data.qrCode); downloadImage(data.qrCode); } } catch (error) { console.error("Error while converting:", error); alert("Error while converting"); } finally { setLoading(false); } }; function clearInputAndQRCode() { setQRCodeURL(""); const data = document.getElementById("data") as HTMLInputElement; data.value = ""; } return (

qr-code-generator

{QRCodeURL && ( QR Code )}
); }