Skip to content

Commit

Permalink
Merge pull request #142 from crux-bphc/make-conditional-navbar
Browse files Browse the repository at this point in the history
Make Navbar components conditional
  • Loading branch information
soumitradev authored Jan 5, 2024
2 parents 163a375 + 1e7f1c4 commit d2ab89e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 26 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"lucide": "^0.302.0",
"lucide-react": "^0.302.0",
"react": "^18.2.0",
"react-cookie": "^6.1.3",
"react-dom": "^18.2.0",
"tailwind-merge": "^2.2.0",
"tailwindcss-animate": "^1.0.7",
Expand Down
47 changes: 47 additions & 0 deletions frontend/pnpm-lock.yaml

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

66 changes: 43 additions & 23 deletions frontend/src/components/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import {
useQuery,
useQueryClient,
} from "@tanstack/react-query";
import { Link } from "@tanstack/react-router";
import { Link, useRouter } from "@tanstack/react-router";
import axios, { AxiosError } from "axios";
import { BookUp, Info, LogOut, Pencil, Plus } from "lucide-react";
import { useCookies } from "react-cookie";
import { z } from "zod";
import { userWithTimetablesType } from "../../../lib/src/index";
import { router } from "../main";
Expand All @@ -42,9 +43,18 @@ const userQueryOptions = queryOptions({
});

export function NavBar() {
const stateRouter = useRouter();
const isCMSPage =
stateRouter.state.resolvedLocation.pathname.includes("/cms");
const isEditPage = stateRouter.state.resolvedLocation.pathname.includes(
"/edit/" || "/finalize/",
);

const [cookies, setCookie, removeCookie] = useCookies(["session"]);
const userQueryResult = useQuery(userQueryOptions);
const { toast } = useToast();
const queryClient = useQueryClient();

const createMutation = useMutation({
mutationFn: () => {
return axios.post<{ message: string; id: string }>(
Expand Down Expand Up @@ -149,17 +159,19 @@ export function NavBar() {
</h1>
</div>
</Link>
<Button
className="text-green-200 w-fit text-xl p-4 ml-4 bg-green-900 hover:bg-green-800"
onClick={
userQueryResultData ? () => createMutation.mutate() : undefined
}
>
<div className="hidden md:flex">Create a timetable</div>
<div className="flex md:hidden">
<Plus className="h-6 w-6" />
</div>
</Button>
{!isEditPage && (
<Button
className="text-green-200 w-fit text-xl p-4 ml-4 bg-green-900 hover:bg-green-800"
onClick={
userQueryResultData ? () => createMutation.mutate() : undefined
}
>
<div className="hidden md:flex">Create a timetable</div>
<div className="flex md:hidden">
<Plus className="h-6 w-6" />
</div>
</Button>
)}
<Link
// Comment out for now because the route doesn't exist
// to={userQueryResultData ? "/about" : undefined}
Expand All @@ -170,15 +182,17 @@ export function NavBar() {
<Info className="h-6 w-6" />
</div>
</Link>
<Link
to={userQueryResultData ? "/cmsExport" : undefined}
className="text-primary py-2 px-2 ml-2 text-lg rounded-full hover:bg-muted transition h-fit whitespace-nowrap duration-200 ease-in-out"
>
<div className="hidden md:flex">CMS Auto-Enroll</div>
<div className="flex md:hidden">
<BookUp className="h-6 w-6" />
</div>
</Link>
{!isCMSPage && (
<Link
to={userQueryResultData ? "/cmsExport" : undefined}
className="text-primary py-2 px-2 ml-2 text-lg rounded-full hover:bg-muted transition h-fit whitespace-nowrap duration-200 ease-in-out"
>
<div className="hidden md:flex">CMS Auto-Enroll</div>
<div className="flex md:hidden">
<BookUp className="h-6 w-6" />
</div>
</Link>
)}
</div>
<div className="flex flex-row">
<div className="pt-3">
Expand All @@ -203,11 +217,17 @@ export function NavBar() {
<DropdownMenuItem
asChild
className="focus:bg-destructive/90 focus:text-destructive-foreground cursor-pointer"
onClick={() => {
removeCookie("session", { path: "/" });
router.navigate({
to: "/login",
});
}}
>
<Link to={userQueryResultData ? "/login" : undefined}>
<div>
<LogOut className="mr-2 h-4 w-4" />
<span>Log out</span>
</Link>
</div>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
rootRouteWithContext,
} from "@tanstack/react-router";
import React from "react";
import { CookiesProvider } from "react-cookie";
import ReactDOM from "react-dom/client";
import authenticatedRoute from "./AuthenticatedRoute";
import cmsOptionRoute from "./CMSOption";
Expand Down Expand Up @@ -66,9 +67,11 @@ if (rootElement) {
ReactDOM.createRoot(rootElement).render(
<React.StrictMode>
<ThemeProvider defaultTheme="system" storageKey="vite-ui-theme">
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
</QueryClientProvider>
<CookiesProvider>
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
</QueryClientProvider>
</CookiesProvider>
</ThemeProvider>
</React.StrictMode>,
);
Expand Down

0 comments on commit d2ab89e

Please sign in to comment.