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 2 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
45 changes: 42 additions & 3 deletions frontend/modules/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const HomePage = () => {
successDuration: 3000,
});
const toast = useToast();
const isAliasEmpty = alias === "";
const isUrlEmpty = url === "";

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

const handleSubmit = () => {
setIsLoading(true);
fetch("/api/shorten", {
Expand Down Expand Up @@ -72,11 +73,47 @@ const HomePage = () => {
}
});
};
const handleEmpty = () => {
if (!isUrlValid)
toast({
title: "Error occured",
description: "Please enter a valid url",
status: "error",
duration: 5000,
isClosable: true,
});
else if (isUrlEmpty && isAliasEmpty) {
toast({
title: "Long url and short url empty",
description: "Please enter long url and short url",
status: "error",
duration: 5000,
isClosable: true,
});
} else if (isUrlEmpty) {
toast({
title: "Long url empty",
description: "Please enter long url",
status: "error",
duration: 5000,
isClosable: true,
});
} else if (isAliasEmpty) {
toast({
title: "Short url empty",
description: "Please enter short url",
status: "error",
duration: 5000,
isClosable: true,
});
}
};

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

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

Expand Down Expand Up @@ -134,7 +171,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
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.