Created
March 21, 2024 09:21
-
-
Save Lahirutech/8133c20ccf3b9b5ecabff282babae66f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from 'react' | |
import './App.css' | |
export default function App() { | |
const [url, setUrl] = useState("") | |
const [shurl, setShUrl] = useState("") | |
// https://api-ssl.bitly.com/v4/shorten | |
const postUrl = () => { | |
fetch("https://api-ssl.bitly.com/v4/shorten", { | |
method: "post", | |
headers: { | |
"Content-Type": "application/json", | |
"Authorization": 'Bearer ' + import.meta.env.VITE_BITLY | |
}, body: JSON.stringify({ long_url: url }) | |
}).then(res => res.json()).then(data => { | |
console.log(data) | |
setShUrl(data.link) | |
}) | |
} | |
const handleSubmit = (e: React.MouseEvent<HTMLButtonElement>) => { | |
e.preventDefault() | |
console.log("called") | |
postUrl() | |
} | |
return ( | |
<main> | |
SHort URL:{shurl} <br /> | |
Input Url: <input type="text" value={url} onChange={(e) => setUrl(e.target.value)} /> <br /> | |
<button onClick={(e) => handleSubmit(e)}>Generate Short URL</button> | |
</main> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment