Skip to content

Commit

Permalink
bugfixed quizzes routing and Link buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Junhaeng committed Nov 28, 2023
1 parent 4c64b39 commit e1fcaf4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 47 deletions.
8 changes: 3 additions & 5 deletions components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";
import Link from "next/link";
import { FC } from "react";
import { useCourseContext } from "../context/courseContext";
import { Course, CourseList, User } from "@/lib/types";

import { UserNav } from "./user-nav";
Expand All @@ -13,7 +12,8 @@ type NavbarProps = {
};

const Navbar: FC<NavbarProps> = ({ courseList }) => {
const { currentCourse, setCurrentCourse } = useCourseContext();
const router = useRouter();
const { courseName } = router.query;

const pathName = useRouter();

Expand All @@ -34,20 +34,18 @@ const Navbar: FC<NavbarProps> = ({ courseList }) => {
className={`p-4 block ${
isActive("dashboard") ? "bg-sidebarColor" : "hover:bg-sidebarColor"
}`}
onClick={() => setCurrentCourse("")}
>
Dashboard
</Link>
{courseList.map((course: Course) => (
<Link
key={course.id}
href={`/${course.name || currentCourse}`}
href={`/${course.name || courseName}`}
className={`p-4 block ${
isActive(`${course.name}`)
? "bg-sidebarColor"
: "hover:bg-sidebarColor"
}`}
onClick={() => setCurrentCourse(course.name)}
>
{course.name}
</Link>
Expand Down
17 changes: 3 additions & 14 deletions components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useCourseContext } from "../context/courseContext";
import { useRouter } from "next/router";

const Sidebar: FC = () => {
const { currentCourse, setCurrentCourse } = useCourseContext();
const [tabList, setTableList] = useState([
"Home",
"Assignments",
Expand All @@ -14,7 +13,7 @@ const Sidebar: FC = () => {
"Files",
]);
const router = useRouter();

const { courseName } = router.query;
const isActive = (it: string) => {
// If the item is 'Home', check against the base path ('/')
const paramList = router.asPath.split("/");
Expand All @@ -24,25 +23,15 @@ const Sidebar: FC = () => {
// Otherwise, compare against the lowercased item
return paramList[2] === it.toLocaleLowerCase();
};
// useEffect(() => {
// // If currentCourse is not set, redirect to the home page
// if (!currentCourse) {
// router.push("/");
// }
// }, [currentCourse, router]);

// if (!currentCourse) {
// return <div>Loading...</div>;
// }

return (
<nav className="flex flex-col bg-sidebarColor text-white h-screen pl-3 pr-2">
<div className="flex flex-col flex-grow">
{/* Links */}
{currentCourse &&
{courseName &&
tabList.map((it) => (
<Link
href={`/${currentCourse}${
href={`/${courseName}${
it !== "Home" ? `/${it.toLocaleLowerCase()}` : ""
}`}
className={`p-4 block group border-l-4 ${
Expand Down
File renamed without changes.
18 changes: 11 additions & 7 deletions pages/[courseName]/quizzes/[quizName]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ export default function Quiz() {
const { quizName } = router.query;

const pathName = router.asPath;
console.log(pathName);

const { isTA } = useUserTypeContext();

const handleButtonClick = () => {
if (isTA) {
router.push(`${pathName}/edit`);
} else {
router.push(`${pathName}/take`);
}
};

return (
<div className="hidden h-full flex-1 flex-col space-y-8 p-8 md:flex">
<div className="flex items-center justify-between space-y-2">
Expand All @@ -25,12 +33,8 @@ export default function Quiz() {
</div>
</div>
<div>
<Button>
{isTA ? (
<Link href={`${pathName}/edit`}>edit</Link>
) : (
<Link href={`${pathName}/take`}>take</Link>
)}
<Button onClick={handleButtonClick}>
{isTA ? <span>edit</span> : <span>take</span>}
</Button>
</div>
</div>
Expand Down
10 changes: 4 additions & 6 deletions pages/[courseName]/quizzes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { useRouter } from "next/router";
export default function Quizzes() {
const router = useRouter();
const { courseName } = router.query;
console.log("courseName" + courseName);
const currentPath = router.pathname;

const currentPath = router.asPath;
// List of the quizzes' file names.
const [quizList, setQuizList] = useState([
"Quiz_0",
Expand Down Expand Up @@ -65,10 +65,8 @@ export default function Quizzes() {
<TableRow key={"create"}>
<TableCell>
{isTA && (
<Button>
<Link href={`${currentPath}/create`} className="flex ">
<span>create</span>
</Link>
<Button onClick={() => router.push(`${currentPath}/create`)}>
<span>create</span>
</Button>
)}
</TableCell>
Expand Down
23 changes: 8 additions & 15 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export default function App({ Component, pageProps }: Props) {
// image: session.user.image || "",
// };

// nested layout
// const getLayout = Component.getLayout ?? ((page: React.ReactNode) => page);

// const [showSidebar, setShowSidebar] = useState(false);
const showSidebar = Component.showSidebar ?? true;

const router = useRouter();
Expand All @@ -57,23 +57,16 @@ export default function App({ Component, pageProps }: Props) {
];
const { isTA, setIsTA } = useUserTypeContext();

// useEffect(() => {
// setShowSidebar(!router.pathname.startsWith("/dashboard"));
// }, []);

// const showSidebar = !router.pathname.startsWith("/dashboard");
return (
<UserContextProvider>
<UserTypeContextProvider>
<CourseContextProvider>
<SessionProvider session={pageProps.session}>
<div className="flex">
<Navbar courseList={courseList} />
{showSidebar && <Sidebar />}
<Component {...pageProps} />
</div>
</SessionProvider>
</CourseContextProvider>
<SessionProvider session={pageProps.session}>
<div className="flex">
<Navbar courseList={courseList} />
{showSidebar && <Sidebar />}
<Component {...pageProps} />
</div>
</SessionProvider>
</UserTypeContextProvider>
</UserContextProvider>
);
Expand Down

0 comments on commit e1fcaf4

Please sign in to comment.