Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle not found links better, configurable URLs #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
15 changes: 12 additions & 3 deletions frontend/modules/Constants/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
const WORKERS_URL= "http://127.0.0.1:8787"
export const SHORTEN_URL = `${WORKERS_URL}/shorten`
export const RETRIEVE_URL = `${WORKERS_URL}/retrieve`
const env = process.env.NODE_ENV;
let WORKERS_URL;
export let BASENAME_URL;
if (env == "development") {
WORKERS_URL = "http://localhost:8787";
BASENAME_URL = "http://localhost:3000/api";
} else if (env == "production") {
WORKERS_URL = "https://ristek-link-workers.jonathanfilbert.workers.dev";
BASENAME_URL = "https://ristek.link";
}
export const SHORTEN_URL = `${WORKERS_URL}/shorten`;
export const RETRIEVE_URL = `${WORKERS_URL}/retrieve`;
22 changes: 20 additions & 2 deletions frontend/modules/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Button from "../components/Button";
import ResultBox from "../components/ResultBox";
import { useToast } from "@chakra-ui/react";
import useClipboard from "react-use-clipboard";
import { useRouter } from 'next/router'
import { BASENAME_URL } from '../Constants'

const HomePage = () => {
const [alias, setAlias] = useState("");
Expand All @@ -17,7 +19,7 @@ const HomePage = () => {
const [isLoading, setIsLoading] = useState(false);
const [isGenerated, setIsGenerated] = useState(false);
const [result, setResult] = useState("");
const [isCopied, setCopied] = useClipboard(`https://ristek.link/${result}`, {
const [isCopied, setCopied] = useClipboard(`${BASENAME_URL}/${result}`, {
successDuration: 3000,
});
const toast = useToast();
Expand Down Expand Up @@ -62,9 +64,11 @@ const HomePage = () => {
setIsLoading(false);
setAlias("");
setIsGenerated(false);
let message
if (result.error === "AlreadyExists") message = `The short url /${alias} already exists. Please choose another one.`
toast({
title: "Error occured",
description: result.data,
description: result.message,
status: "error",
duration: 5000,
isClosable: true,
Expand All @@ -73,6 +77,20 @@ const HomePage = () => {
});
};

const router = useRouter();
useEffect(() => {
const { notfound } = router.query
if (notfound) {
toast({
title: "Error occured",
description: `The short url /${notfound} does not exist.`,
status: "error",
duration: 5000,
isClosable: true,
});
}
},[router])

useEffect(() => {
if (!!alias && isUrlValid) {
return setIsAllowed(true);
Expand Down
3 changes: 2 additions & 1 deletion frontend/modules/components/ResultBox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import { BASENAME_URL } from "../../Constants";

type ResultBoxProps = {
onCopy?: () => any;
Expand All @@ -15,7 +16,7 @@ const ResultBox = (props: ResultBoxProps) => {
} else {
return (
<div className="absolute -bottom-32 w-full h-8 bg-primary py-9 flex flex-row items-center justify-between rounded-md px-4 mt-3 shadow-lg">
<div className="text-white font-bold truncate">{`https://ristek.link/${props.alias}`}</div>
<div className="text-white font-bold truncate">{`${BASENAME_URL}/${props.alias}`}</div>
<div
className="text-white cursor-pointer"
onClick={() => props.onCopy()}
Expand Down
13 changes: 11 additions & 2 deletions frontend/pages/api/[alias].tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { RETRIEVE_URL } from "../../modules/Constants";

export default async (req: NextApiRequest, res: NextApiResponse) => {
const { alias } = req.query;
const result = await fetch(
"https://ristek-link-workers.jonathanfilbert.workers.dev/retrieve",
RETRIEVE_URL,
{
method: "POST",
headers: {
Expand All @@ -13,5 +14,13 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
body: JSON.stringify({ short_url: alias }),
}
).then((res) => res.json());
return res.redirect(result.data);
// return res.redirect(result.data);
const { ok, data, error } = result
if (ok) {
return res.redirect(data);
} else {
if (error === "NotFound") {
return res.redirect("/" + "?notfound=" + alias)
}
}
};
3 changes: 2 additions & 1 deletion frontend/pages/api/shorten.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { SHORTEN_URL } from "../../modules/Constants";

export default async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === "POST") {
const { alias, url } = req.body;
const result = await fetch(
"https://ristek-link-workers.jonathanfilbert.workers.dev/shorten",
SHORTEN_URL,
{
method: "POST",
headers: {
Expand Down
8 changes: 4 additions & 4 deletions server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ const handleRequest = async event => {
if (!!existing_short_url) {
return new Response(
JSON.stringify({
data: `The short url /${short_url} already exists. Please choose another one.`,
ok: false,
error: 'AlreadyExists'
}),
{ status: 200, headers: corsHeaders },
{ status: 400, headers: corsHeaders },
)
}
const _ = await LINKS.put(short_url, long_url)
Expand All @@ -60,11 +60,11 @@ const handleRequest = async event => {
}
return new Response(
JSON.stringify({
data: `The short url /${short_url} does not exist.`,
ok: false,
error: 'NotFound'
}),
{
status: 200,
status: 404,
headers: corsHeaders,
},
)
Expand Down