Skip to content

Commit

Permalink
integrate url shortnening api
Browse files Browse the repository at this point in the history
  • Loading branch information
vinit717 committed Nov 7, 2023
1 parent 481a341 commit 0a00cb8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
37 changes: 34 additions & 3 deletions src/pages/dashboard/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,9 +10,41 @@ const Dashboard = () => {
const [url, getUrl] = useState<string>('');
const [shortUrl, setUrl] = useState<string>('');

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 (
<Layout title="Home | URL Shortener">
<div className="w-screen">
Expand Down
1 change: 0 additions & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export const alphanumicUnderscore = /^[a-zA-Z0-9_]+$/;
export const randomString = Math.random().toString(36).substring(2, 7);

0 comments on commit 0a00cb8

Please sign in to comment.