Created an Admin panel for developers

This commit is contained in:
Tiago Ribeiro
2023-09-16 15:00:48 +01:00
parent 05ca96e476
commit 91495d6a34
4 changed files with 164 additions and 5 deletions

View File

@@ -34,6 +34,7 @@ export default function Layout({user, children, className, navDisabled = false,
focusMode={focusMode}
onFocusLayerMouseEnter={onFocusLayerMouseEnter}
className="-lg:hidden"
showAdmin={user.type === "developer"}
/>
<div
className={clsx(

View File

@@ -1,5 +1,6 @@
import clsx from "clsx";
import {ReactNode} from "react";
import {BsArrowRepeat} from "react-icons/bs";
interface Props {
children: ReactNode;
@@ -7,11 +8,21 @@ interface Props {
variant?: "outline" | "solid";
className?: string;
disabled?: boolean;
isLoading?: boolean;
onClick?: () => void;
type?: "button" | "reset" | "submit";
}
export default function Button({color = "purple", variant = "solid", disabled = false, className, children, type, onClick}: Props) {
export default function Button({
color = "purple",
variant = "solid",
disabled = false,
isLoading = false,
className,
children,
type,
onClick,
}: Props) {
const colorClassNames: {[key in typeof color]: {[key in typeof variant]: string}} = {
purple: {
solid: "bg-mti-purple-light text-white border border-mti-purple-light hover:bg-mti-purple disabled:text-mti-purple disabled:bg-mti-purple-ultralight selection:bg-mti-purple-dark",
@@ -39,8 +50,13 @@ export default function Button({color = "purple", variant = "solid", disabled =
className,
colorClassNames[color][variant],
)}
disabled={disabled}>
{children}
disabled={disabled || isLoading}>
{!isLoading && children}
{isLoading && (
<div className="flex items-center justify-center">
<BsArrowRepeat className="text-white animate-spin" size={25} />
</div>
)}
</button>
);
}

View File

@@ -1,7 +1,7 @@
import clsx from "clsx";
import {IconType} from "react-icons";
import {MdSpaceDashboard} from "react-icons/md";
import {BsFileEarmarkText, BsClockHistory, BsPencil, BsGraphUp, BsChevronBarRight, BsChevronBarLeft} from "react-icons/bs";
import {BsFileEarmarkText, BsClockHistory, BsPencil, BsGraphUp, BsChevronBarRight, BsChevronBarLeft, BsShieldFill} from "react-icons/bs";
import {RiLogoutBoxFill} from "react-icons/ri";
import {SlPencil} from "react-icons/sl";
import {FaAward} from "react-icons/fa";
@@ -18,6 +18,7 @@ interface Props {
focusMode?: boolean;
onFocusLayerMouseEnter?: () => void;
className?: string;
showAdmin?: boolean;
}
interface NavProps {
@@ -43,7 +44,7 @@ const Nav = ({Icon, label, path, keyPath, disabled = false, isMinimized = false}
</Link>
);
export default function Sidebar({path, navDisabled = false, focusMode = false, onFocusLayerMouseEnter, className}: Props) {
export default function Sidebar({path, navDisabled = false, focusMode = false, showAdmin = false, onFocusLayerMouseEnter, className}: Props) {
const router = useRouter();
const [isMinimized, toggleMinimize] = usePreferencesStore((state) => [state.isSidebarMinimized, state.toggleSidebarMinimized]);
@@ -69,6 +70,9 @@ export default function Sidebar({path, navDisabled = false, focusMode = false, o
<Nav disabled={disableNavigation} Icon={BsPencil} label="Exercises" path={path} keyPath="/exercises" isMinimized={isMinimized} />
<Nav disabled={disableNavigation} Icon={BsGraphUp} label="Stats" path={path} keyPath="/stats" isMinimized={isMinimized} />
<Nav disabled={disableNavigation} Icon={BsClockHistory} label="Record" path={path} keyPath="/record" isMinimized={isMinimized} />
{showAdmin && (
<Nav disabled={disableNavigation} Icon={BsShieldFill} label="Admin" path={path} keyPath="/admin" isMinimized={isMinimized} />
)}
</div>
<div className="flex flex-col gap-0 absolute bottom-12">

138
src/pages/admin.tsx Normal file
View File

@@ -0,0 +1,138 @@
/* eslint-disable @next/next/no-img-element */
import Head from "next/head";
import {withIronSessionSsr} from "iron-session/next";
import {sessionOptions} from "@/lib/session";
import useUser from "@/hooks/useUser";
import {toast, ToastContainer} from "react-toastify";
import Layout from "@/components/High/Layout";
import {RadioGroup} from "@headlessui/react";
import {MODULE_ARRAY} from "@/utils/moduleUtils";
import clsx from "clsx";
import {capitalize} from "lodash";
import {FormEvent, useState} from "react";
import {Module} from "@/interfaces";
import Input from "@/components/Low/Input";
import Button from "@/components/Low/Button";
import {getExamById} from "@/utils/exams";
import useExamStore from "@/stores/examStore";
import {useRouter} from "next/router";
export const getServerSideProps = withIronSessionSsr(({req, res}) => {
const user = req.session.user;
if (!user || !user.isVerified) {
res.setHeader("location", "/login");
res.statusCode = 302;
res.end();
return {
props: {
user: null,
},
};
}
if (user.type !== "developer") {
res.setHeader("location", "/");
res.statusCode = 302;
res.end();
return {
props: {
user: null,
},
};
}
return {
props: {user: req.session.user},
};
}, sessionOptions);
const ExamLoader = () => {
const [selectedModule, setSelectedModule] = useState<Module>();
const [examId, setExamId] = useState<string>();
const [isLoading, setIsLoading] = useState(false);
const setExams = useExamStore((state) => state.setExams);
const setSelectedModules = useExamStore((state) => state.setSelectedModules);
const router = useRouter();
const loadExam = async (e?: FormEvent) => {
if (e) e.preventDefault();
setIsLoading(true);
if (selectedModule && examId) {
const exam = await getExamById(selectedModule, examId);
if (!exam) {
toast.error("Unknown Exam ID! Please make sure you selected the right module and entered the right exam ID", {
toastId: "invalid-exam-id",
});
setIsLoading(false);
return;
}
setExams([exam]);
setSelectedModules([selectedModule]);
router.push("/exercises");
}
setIsLoading(false);
};
return (
<div className="flex flex-col gap-4 border p-4 border-mti-gray-platinum rounded-xl">
<label className="font-normal text-base text-mti-gray-dim">Exam Loader</label>
<form className="flex flex-col gap-4 w-full" onSubmit={loadExam}>
<RadioGroup value={selectedModule} onChange={setSelectedModule} className="grid grid-cols-2 items-center gap-4 place-items-center">
{MODULE_ARRAY.map((module) => (
<RadioGroup.Option value={module} key={module}>
{({checked}) => (
<span
className={clsx(
"px-6 py-4 w-48 flex justify-center text-sm font-normal rounded-full border focus:outline-none cursor-pointer",
"hover:bg-mti-purple-light hover:border-mti-purple-dark hover:text-white",
"transition duration-300 ease-in-out",
!checked ? "bg-white border-mti-gray-platinum" : "bg-mti-purple-light border-mti-purple-dark text-white",
)}>
{capitalize(module)}
</span>
)}
</RadioGroup.Option>
))}
</RadioGroup>
<Input type="text" name="examId" onChange={setExamId} placeholder="Exam ID" />
<Button disabled={!selectedModule || !examId} isLoading={isLoading}>
Load Exam
</Button>
</form>
</div>
);
};
export default function Admin() {
const {user} = useUser({redirectTo: "/login"});
return (
<>
<Head>
<title>Admin Panel | EnCoach</title>
<meta
name="description"
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."
/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
</Head>
<ToastContainer />
{user && (
<Layout user={user}>
<section className="w-full flex gap-8">
<ExamLoader />
</section>
</Layout>
)}
</>
);
}