Skip to content

Commit

Permalink
modify toast behaviour in Home
Browse files Browse the repository at this point in the history
- stopping auto-redirect to Login Component.
- Sending request to the server despite that user is not logged in, to check the API response from the backend. API should reject such req.
  • Loading branch information
DhananjayThomble committed Feb 11, 2023
1 parent 65a2150 commit 80a7841
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions react-frontend/src/components/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,25 @@ function Home() {
const [originalUrl, setOriginalUrl] = useState("");
const [shortUrl, setShortUrl] = useState("");
const [name, setName] = useState(localStorage.getItem("name") || "Guest");
const [isAuthenticated, setIsAuthenticated] = useState(false);
const URL = "https://app.dhananjaythomble.me/api/v2/url";
const navigate = useNavigate();

useEffect(() => {
const token = localStorage.getItem("token");
setName(localStorage.getItem("name") || "Guest");
if (!token) {
toast.warning("You are not logged in");
setTimeout(() => {
// redirect to login page
navigate("/login");
}, 3000);
setIsAuthenticated(false);
toast.warning("Please Login to use the App");
} else {
setIsAuthenticated(true);
}
}, [navigate]);
}, []);

const handleSubmit = async (e) => {
e.preventDefault();
if (!validateForm()) return false;
// show toast
toast("URL will be shortened soon!");
if (isAuthenticated) toast("URL will be shortened soon!");

await fetchUrl();
};
Expand Down Expand Up @@ -68,7 +67,7 @@ function Home() {
if (status === 400) {
toast.error(error);
} else if (status === 401) {
toast.error(error);
toast.error("Please Login First!");
} else if (status === 500) {
toast.error(error);
} else {
Expand Down

0 comments on commit 80a7841

Please sign in to comment.