From 8c0236172349564f638ce757191b724445bc771d Mon Sep 17 00:00:00 2001 From: Arkaprabha Chakraborty Date: Tue, 6 Aug 2024 14:34:06 +0530 Subject: [PATCH] Fix: new link isn't created for existing long url --- frontend/app/layout.js | 2 +- frontend/app/page.js | 25 +++++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/frontend/app/layout.js b/frontend/app/layout.js index 10ba240..99de99f 100644 --- a/frontend/app/layout.js +++ b/frontend/app/layout.js @@ -7,7 +7,7 @@ dotenv.config(); const inter = Inter({ subsets: ["latin"] }); export const metadata = { - title: "URL Shortener", + title: "Reduce", description: "A simple URL shortener application", }; diff --git a/frontend/app/page.js b/frontend/app/page.js index 46def5e..935a47c 100644 --- a/frontend/app/page.js +++ b/frontend/app/page.js @@ -1,6 +1,6 @@ "use client"; -import { useState } from "react"; +import { useState, useEffect } from "react"; import axios from "axios"; import dotenv from "dotenv"; import { MdContentCopy } from "react-icons/md"; @@ -11,10 +11,21 @@ export default function Home() { const [longUrl, setLongUrl] = useState(""); const [shortUrl, setShortUrl] = useState(""); const [copied, setCopied] = useState(false); + const [prevLongUrl, setPrevLongUrl] = useState(""); + + useEffect(() => { + if (prevLongUrl !== longUrl) { + setShortUrl(""); + } + }, [longUrl, prevLongUrl]); const handleSubmit = async (e) => { e.preventDefault(); + if (longUrl === prevLongUrl && shortUrl) { + return; + } + // Use the frontend domain as the base URL const baseURL = window.location.origin; @@ -28,6 +39,7 @@ export default function Home() { ); setShortUrl(response.data.short_url); + setPrevLongUrl(longUrl); } catch (error) { console.error("Error shortening URL:", error); } @@ -69,15 +81,20 @@ export default function Home() { {shortUrl && (
- + {shortUrl} - +