diff --git a/src/pages/dashboard/index.tsx b/src/pages/dashboard/index.tsx index 69df0f4..1ba325c 100644 --- a/src/pages/dashboard/index.tsx +++ b/src/pages/dashboard/index.tsx @@ -5,46 +5,18 @@ import { useState } from 'react'; import AddIcon from '../../../public/assets/icons/add'; import CopyIcon from '../../../public/assets/icons/copy'; import ReloadIcon from '../../../public/assets/icons/reload'; +import { shortenUrl } from '../../utils/api'; const Dashboard = () => { const [url, getUrl] = useState(''); - const [shortUrl, setUrl] = useState(''); + const [shortUrl, setShortUrl] = useState(''); - 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 handleUrl = async () => { const shortenedUrl = await shortenUrl(url); if (shortenedUrl) { - setUrl(shortenedUrl); + setShortUrl(shortenedUrl); } }; - return (
@@ -65,7 +37,7 @@ const Dashboard = () => { /> @@ -84,7 +56,7 @@ const Dashboard = () => { @@ -92,7 +64,7 @@ const Dashboard = () => { type="button" className="w-full md:w-auto bg-gray-200 px-4 md:px-8 py-3 hover:bg-gray-300 mt-2 md:mt-0 md:rounded-none" onClick={() => { - setUrl(''); + setShortUrl(''); }} > diff --git a/src/utils/api.ts b/src/utils/api.ts new file mode 100644 index 0000000..4b30057 --- /dev/null +++ b/src/utils/api.ts @@ -0,0 +1,29 @@ +import { TINY_API_URL } from '@/constants/url'; + +export async function shortenUrl(originalUrl: string) { + try { + const response = await fetch(`${TINY_API_URL}/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; + } +}