From 4f70ec40c94ffed25400d2a80e7654c594a669b3 Mon Sep 17 00:00:00 2001 From: Arkaprabha Chakraborty Date: Mon, 12 Aug 2024 15:23:53 +0530 Subject: [PATCH] Add QR code --- frontend/app/page.js | 131 ++++++++++++++++++++++-------------------- frontend/package.json | 3 +- 2 files changed, 70 insertions(+), 64 deletions(-) diff --git a/frontend/app/page.js b/frontend/app/page.js index 935a47c..c499561 100644 --- a/frontend/app/page.js +++ b/frontend/app/page.js @@ -1,11 +1,9 @@ "use client"; -import { useState, useEffect } from "react"; +import { useState } from "react"; import axios from "axios"; -import dotenv from "dotenv"; import { MdContentCopy } from "react-icons/md"; - -dotenv.config({ path: "./.env.local" }); +import QRCode from "react-qr-code"; export default function Home() { const [longUrl, setLongUrl] = useState(""); @@ -13,12 +11,6 @@ export default function Home() { const [copied, setCopied] = useState(false); const [prevLongUrl, setPrevLongUrl] = useState(""); - useEffect(() => { - if (prevLongUrl !== longUrl) { - setShortUrl(""); - } - }, [longUrl, prevLongUrl]); - const handleSubmit = async (e) => { e.preventDefault(); @@ -26,15 +18,13 @@ export default function Home() { return; } - // Use the frontend domain as the base URL const baseURL = window.location.origin; - try { const response = await axios.post( `${process.env.NEXT_PUBLIC_BACKEND_URL}/reduce/shorten`, { long_url: longUrl, - base_url: baseURL, // Include the frontend domain in the request body + base_url: baseURL, }, ); @@ -46,61 +36,76 @@ export default function Home() { }; const handleCopy = () => { - navigator.clipboard - .writeText(shortUrl) - .then(() => { - setCopied(true); - setTimeout(() => setCopied(false), 2000); // Reset copy state after 2 seconds - }) - .catch((error) => { - console.error("Failed to copy URL:", error); - }); + navigator.clipboard.writeText(shortUrl); + setCopied(true); + setTimeout(() => setCopied(false), 2000); }; return ( -
-
-

- Reduce -

-
- setLongUrl(e.target.value)} - className="border border-gray-300 rounded-lg p-3 text-gray-800 focus:outline-none focus:ring-2 focus:ring-blue-500" - /> - -
- {shortUrl && ( -
-
- - {shortUrl} - - + +
+ setLongUrl(e.target.value)} + className="border border-gray-300 bg-inherit rounded-lg p-3 text-white focus:outline-none focus:ring-2 focus:ring-blue-500" + /> + +
+ {shortUrl && ( +
+
+ + {shortUrl} + + +
+
+ +
-
- )} + )} +
-
+ ); } diff --git a/frontend/package.json b/frontend/package.json index fc14057..12b9c88 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -14,7 +14,8 @@ "next": "14.2.5", "react": "^18", "react-dom": "^18", - "react-icons": "^5.2.1" + "react-icons": "^5.2.1", + "react-qr-code": "^2.0.15" }, "devDependencies": { "postcss": "^8",