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

Fix incomplete submit #11

Open
wants to merge 3 commits 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
34 changes: 24 additions & 10 deletions frontend/modules/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import RistekLogo from "../images/Logo";
import BottomBanner from "../images/Bottom";
import Input from "../components/Input";
import Button from "../components/Button";
import Toast from "../components/Toast";
import ResultBox from "../components/ResultBox";
import { useToast } from "@chakra-ui/react";
import useClipboard from "react-use-clipboard";
Expand All @@ -21,6 +22,8 @@ const HomePage = () => {
successDuration: 3000,
});
const toast = useToast();
const isAliasEmpty = alias === "";
const isUrlEmpty = url === "";

const handleUrlType = (text: string) => {
setUrl(text);
Expand All @@ -39,7 +42,6 @@ const HomePage = () => {
const handleAliasType = (text: string) => {
setAlias(text.split(" ").length > 1 ? text.split(" ").join("-") : text);
};

const handleSubmit = () => {
setIsLoading(true);
fetch("/api/shorten", {
Expand All @@ -62,21 +64,31 @@ const HomePage = () => {
setIsLoading(false);
setAlias("");
setIsGenerated(false);
toast({
title: "Error occured",
description: result.data,
status: "error",
duration: 5000,
isClosable: true,
});
Toast("Error occured", result.data, true);
}
});
};
const handleEmpty = () => {
if (!isUrlValid) {
Toast("Url invalid", "Please enter a valid url", true);
} else if (isUrlEmpty && isAliasEmpty) {
Toast(
"Long url and short url empty",
"Please enter long url and short url",
true
);
} else if (isUrlEmpty) {
Toast("Long url empty", "Please enter long url", true);
} else if (isAliasEmpty) {
Toast("Short url empty", "Please enter short url", true);
}
};

useEffect(() => {
if (!!alias && isUrlValid) {
if (!!alias && isUrlValid && !isUrlEmpty) {
return setIsAllowed(true);
}

setIsAllowed(false);
}, [url, alias]);

Expand Down Expand Up @@ -134,7 +146,9 @@ const HomePage = () => {
</div>
<Button
disabled={!isAllowed}
onClick={() => isAllowed && !isLoading && handleSubmit()}
onClick={() =>
isAllowed && !isLoading ? handleSubmit() : handleEmpty()
}
isLoading={isLoading}
>
<div className=" font-semibold text-lg">Shorten!</div>
Expand Down
13 changes: 13 additions & 0 deletions frontend/modules/components/Toast/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createStandaloneToast } from "@chakra-ui/react";

const Toast = (title: string, description: string, isError: boolean) => {
const toast = createStandaloneToast();
const status = isError ? "error" : "success";
toast({
title: title,
description: description,
status: status,
});
};

export default Toast;
30 changes: 15 additions & 15 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.