diff --git a/src/pages/dashboard/index.tsx b/src/pages/dashboard/index.tsx index 45d3cca..69df0f4 100644 --- a/src/pages/dashboard/index.tsx +++ b/src/pages/dashboard/index.tsx @@ -1,7 +1,6 @@ import Button from '@/components/Button'; import InputBox from '@/components/InputBox'; import Layout from '@/components/Layout'; -import { randomString } from '@/utils/constants'; import { useState } from 'react'; import AddIcon from '../../../public/assets/icons/add'; import CopyIcon from '../../../public/assets/icons/copy'; @@ -11,9 +10,41 @@ const Dashboard = () => { const [url, getUrl] = useState(''); const [shortUrl, setUrl] = useState(''); - const handleUniqueUrl = () => { - setUrl(`https://rds.li/${randomString}`); + async function shortenUrl(originalUrl: unknown) { + try { + const response = await fetch('http://localhost:8000/v1/tinyurl', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + OriginalUrl: originalUrl, + Comment: 'your', + CreatedBy: 'vinit', + UserId: 1, + }), + }); + + if (response.status === 200) { + const data = await response.json(); + return data.shortUrl; + } else { + console.error('Error shortening URL:', response.statusText); + return null; + } + } catch (error) { + console.error('Error shortening URL:', error); + return null; + } + } + + const handleUniqueUrl = async () => { + const shortenedUrl = await shortenUrl(url); + if (shortenedUrl) { + setUrl(shortenedUrl); + } }; + return (
diff --git a/src/utils/constants.ts b/src/utils/constants.ts index a80ce32..d2d3595 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -1,2 +1 @@ export const alphanumicUnderscore = /^[a-zA-Z0-9_]+$/; -export const randomString = Math.random().toString(36).substring(2, 7);