Skip to content

Commit

Permalink
Merge pull request #26 from HereEast/fix/router
Browse files Browse the repository at this point in the history
fix: Protected routes
  • Loading branch information
HereEast authored Oct 9, 2024
2 parents d40c8fe + 7ad6e4d commit e73aa9f
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 69 deletions.
6 changes: 6 additions & 0 deletions client/src/components/Entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function Entry({ entry }: EntryProps) {

const isValidEntry = entry.day <= today.day && entry.day >= 1;

// Handle click
function handleClick() {
if (isValidEntry) {
const id = selectedEntryId === entry._id ? null : entry._id;
Expand All @@ -39,6 +40,11 @@ export function Entry({ entry }: EntryProps) {
className={cn(
"flex size-6 shrink-0 items-center justify-center rounded-[4px] bg-stone-300/50 text-sm",
currentRating > 0 && statusColor(currentRating),
// (currentRating === 1 || currentRating === 2) && "bg-stone-400/50",
// (currentRating === 3 || currentRating === 4) && "bg-stone-400",
// (currentRating === 5 || currentRating === 6) && "bg-stone-500",
// (currentRating === 7 || currentRating === 8) && "bg-stone-600",
// (currentRating === 9 || currentRating === 10) && "bg-stone-800",
isValidEntry && "hover:border hover:border-brown-600",
selectedEntryId === entry._id
? "border border-brown-600"
Expand Down
24 changes: 0 additions & 24 deletions client/src/components/layouts/AuthLayout.tsx

This file was deleted.

12 changes: 8 additions & 4 deletions client/src/components/layouts/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import Link from "next/link";
// import { useRouter } from "next/router";
import { useRouter } from "next/router";

import { Button } from "../ui/Button";
import { useAuthContext } from "~/hooks";

export function Header() {
// const router = useRouter();
const router = useRouter();

const { user, isAuth, setIsAuth } = useAuthContext();

// Log out
function handleLogout() {
localStorage.removeItem("token");
setIsAuth(false);

router.replace("/");
}

return (
<div className="fixed flex h-16 w-full items-center justify-between bg-stone-300 px-10">
<h1 className="font-medium">Habit Tracker</h1>
<h1 className="font-medium hover:opacity-50">
<Link href="/">Habit Tracker</Link>
</h1>
<div className="flex items-center gap-2">
<div>
{isAuth ? (
<div className="flex items-center gap-4">
<span className="text-sm font-medium">👋 {user}</span>
{user && <span className="text-sm font-medium">👋 {user}</span>}
<Button onClick={handleLogout}>Log out</Button>
</div>
) : (
Expand Down
1 change: 0 additions & 1 deletion client/src/components/layouts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from "./Timeline";
export * from "./Layout";
export * from "./AuthLayout";
export * from "./Header";
export * from "./Footer";
export * from "./Login";
12 changes: 0 additions & 12 deletions client/src/pages/[slug].tsx

This file was deleted.

24 changes: 24 additions & 0 deletions client/src/pages/[username].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useRouter } from "next/router";
import { useEffect } from "react";

import { Timeline } from "~/components/layouts";
import { MonthContextProvider } from "~/context";
import { useAuthContext } from "~/hooks";

export default function TimelinePage() {
const router = useRouter();

const { isAuth, isAuthLoading } = useAuthContext();

useEffect(() => {
if (!isAuthLoading && !isAuth) {
router.replace("/login");
}
}, [isAuth, router, isAuthLoading]);

return (
<MonthContextProvider>
<Timeline />
</MonthContextProvider>
);
}
8 changes: 4 additions & 4 deletions client/src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { Login } from "~/components/layouts";
export default function LoginPage() {
const router = useRouter();

const { isAuth, isAuthLoading } = useAuthContext();
const { user, isAuthLoading } = useAuthContext();

useEffect(() => {
if (!isAuthLoading && isAuth) {
router.replace("/");
if (!isAuthLoading && user) {
router.replace(`/${user}`);
}
}, [isAuth, router, isAuthLoading]);
}, [user, router, isAuthLoading]);

return <Login />;
}
8 changes: 4 additions & 4 deletions client/src/pages/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { Register } from "~/components/layouts/Register";
export default function RegisterPage() {
const router = useRouter();

const { isAuth } = useAuthContext();
const { user, isAuthLoading } = useAuthContext();

useEffect(() => {
if (isAuth) {
router.replace("/");
if (!isAuthLoading && user) {
router.replace(`/${user}`);
}
}, [isAuth, router]);
}, [user, router, isAuthLoading]);

return <Register />;
}
34 changes: 14 additions & 20 deletions client/src/utils/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,20 @@ export function handleRequestError(err: Error) {

// Status color
export const statusColor = (status: Status): string => {
switch (status) {
case 0:
return "bg-stone-300/50";
case 1:
case 2:
return "bg-stone-400/50";
case 3:
case 4:
return "bg-stone-400";
case 5:
case 6:
return "bg-stone-500";
case 7:
case 8:
return "bg-stone-600";
case 9:
case 10:
return "bg-stone-800";
default:
return "bg-stone-300/50";
if (status === 0) {
return "bg-stone-300/50";
} else if (status === 1 || status === 2) {
return "bg-stone-400/50";
} else if (status === 3 || status === 4) {
return "bg-stone-400";
} else if (status === 5 || status === 6) {
return "bg-stone-500";
} else if (status === 7 || status === 8) {
return "bg-stone-600";
} else if (status === 9 || status === 10) {
return "bg-stone-800";
} else {
return "bg-stone-300/50";
}
};

Expand Down

0 comments on commit e73aa9f

Please sign in to comment.