-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirect.js
33 lines (29 loc) · 956 Bytes
/
direct.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { useEffect, useContext } from "react";
import { Redirect, useParams } from "react-router-dom";
import axios from "axios";
import AuthContext from "./context/auth-context";
const Direct = () => {
const { shortenedURL } = useParams();
const ctx = useContext(AuthContext);
useEffect(() => {
const getURL = async () => {
const options = {
url: `https://urlshortdev.herokuapp.com/api/v2/shorturl/${shortenedURL}`,
method: "GET",
withCredentials: true,
};
const res = await axios(options);
console.log(res);
return res.data.data.url.fullUrl;
};
if (ctx.isLoggedIn) getURL().then((res) => window.open(res, "_blank"));
if (!ctx.isLoggedIn) getURL().then((res) => (window.location.href = res));
}, [ctx, shortenedURL]);
return (
<>
{ctx.isLoggedIn && <Redirect to="/home"></Redirect>}
{!ctx.isLoggedIn && <div></div>}
</>
);
};
export default Direct;