Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import ViewProfile from "./components/Profile/ViewProfile";
import ResetPassword from "./components/Profile/ResetPassword";
import ResetPasswordForm from "./components/Profile/ResetPasswordForm";
import ResetPasswordSent from "./components/Profile/ResetPasswordSent";
import UrlRedirect from "./components/UrlRedirect";

function App() {
return (
Expand Down Expand Up @@ -57,6 +58,7 @@ function App() {
path="reset/sent"
element={<ResetPasswordSent />}
/>
<Route path="url/:shortCode" element={<UrlRedirect />} />

{/* private routes */}
<Route element={<RequireAuth />}>
Expand Down
63 changes: 63 additions & 0 deletions src/components/UrlRedirect.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { Box, Text, Spinner, Flex } from "@chakra-ui/react";
import axios from "../api/axios";

const UrlRedirect = () => {
const { shortCode } = useParams();
const [error, setError] = useState(false);

useEffect(() => {
const fetchOriginalUrl = async () => {
try {
const response = await axios.get(`/url/${shortCode}/`);
const { original_url: originalUrl } = response.data;
if (originalUrl) {
window.location.replace(originalUrl);
} else {
setError(true);
}
} catch (err) {
console.error("Failed to fetch the URL:", err);
setError(true);
}
};

fetchOriginalUrl();
}, [shortCode]);

if (error) {
return (
<Flex
direction="column"
justify="center"
align="center"
minH="60vh"
px={4}
>
<Text fontSize="3xl" color="red.500" mb={4}>
URL Not Found
</Text>
<Text fontSize="lg" textAlign="center">
The link you clicked on might be broken, expired, or typed incorrectly.
</Text>
</Flex>
);
}

return (
<Flex
direction="column"
justify="center"
align="center"
minH="60vh"
>
<Spinner size="xl" thickness="4px" speed="0.65s" color="blue.500" />
<Text fontSize="xl" mt={6}>
Redirecting...
</Text>
</Flex>
);
};

export default UrlRedirect;