From 2be4dd7966ab5bad0d3c70deffca9ad0932c5924 Mon Sep 17 00:00:00 2001 From: Prateek Date: Wed, 25 Sep 2024 19:23:50 +0530 Subject: [PATCH] Kukreja (#10) * Update home page UI and feat: Add an archive feature * Edit URL validation logic to handle duplicates * Add middleware for authentication * Refactor Navbar color logic based on pathname * Refactor Home component * Improve error handling in signIn component and refactor name fields to use camelCase * build error fix --------- Co-authored-by: Prateek Co-authored-by: Prateek Rohilla --- app/MobileNav.js | 6 +- app/archive/page.js | 133 ++++++++++++++++++++++++++ app/email-verification/page.js | 2 +- app/navbar.js | 58 +++++++----- app/page.js | 164 ++++++++++++++++++++------------- app/signin/page.js | 4 +- app/signup/page.js | 24 ++--- middleware.js | 23 +++++ 8 files changed, 308 insertions(+), 106 deletions(-) create mode 100644 app/archive/page.js create mode 100644 middleware.js diff --git a/app/MobileNav.js b/app/MobileNav.js index 1ba4e99..01ae616 100644 --- a/app/MobileNav.js +++ b/app/MobileNav.js @@ -25,12 +25,12 @@ export default function MobileNav() { - About + Archive - Contact + Profile @@ -41,7 +41,7 @@ export default function MobileNav() { diff --git a/app/archive/page.js b/app/archive/page.js new file mode 100644 index 0000000..ac0bdea --- /dev/null +++ b/app/archive/page.js @@ -0,0 +1,133 @@ +'use client' + +import { useEffect, useState } from 'react' +import Cookies from 'js-cookie' +import { MdDelete } from 'react-icons/md' +import TimeAgo from '../timeAgo' + +function Archive() { + const [archiveURLs, setArchiveURLs] = useState([]) + const [token, setToken] = useState(Cookies.get('jwt')) + const webURL = process.env.NEXT_PUBLIC_WEB_URL + + useEffect(() => { + fetchArchiveURLs() + }, []) + + const fetchArchiveURLs = async () => { + try { + const response = await fetch(webURL + 'shortener/archived', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, + }, + }) + + const archiveUrls = await response.json() + setArchiveURLs(archiveUrls) + } catch (error) { + console.error('Error fetching Archive URLs:', error) + } + } + + const handleDeleteUrl = async (id) => { + try { + await fetch(webURL + `shortener/${id}`, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, + }, + }) + + const updatedArchiveUrl = archiveURLs.filter((url) => url.id !== id) + setArchiveURLs(updatedArchiveUrl) + } catch (error) { + console.error('Error Deleting Archive URL:', error) + } + } + + return ( +
+ {archiveURLs.length > 0 ? ( +
+
+

Archive History

+
+ + + + + + + + + + + + + {archiveURLs + .sort( + (a, b) => + new Date(b.updatedAt).getTime() - + new Date(a.updatedAt).getTime() + ) + .map( + ({ + id, + originalUrl, + shortUrl, + clicks, + archived, + createdAt, + updatedAt, + }) => ( + + + + + + + + + ) + )} + +
Original URLShortened URLClicksCreation DateLast Updated
+ + {originalUrl.substring(0, 10)}... + + + + {shortUrl} + + {clicks} + + + + + handleDeleteUrl(id)} + /> +
+
+ ) : ( + 'fetching archive URLs...' + )} +
+ ) +} + +export default Archive diff --git a/app/email-verification/page.js b/app/email-verification/page.js index 4f3db6d..caa5a6b 100644 --- a/app/email-verification/page.js +++ b/app/email-verification/page.js @@ -60,7 +60,7 @@ export default function Verification() { {/* OTP Verification */}
+