Added the ability to generate a user code on the admin panel
This commit is contained in:
@@ -46,6 +46,7 @@
|
|||||||
"react-string-replace": "^1.1.0",
|
"react-string-replace": "^1.1.0",
|
||||||
"react-toastify": "^9.1.2",
|
"react-toastify": "^9.1.2",
|
||||||
"react-xarrows": "^2.0.2",
|
"react-xarrows": "^2.0.2",
|
||||||
|
"short-unique-id": "^5.0.2",
|
||||||
"swr": "^2.1.3",
|
"swr": "^2.1.3",
|
||||||
"typescript": "4.9.5",
|
"typescript": "4.9.5",
|
||||||
"uuid": "^9.0.0",
|
"uuid": "^9.0.0",
|
||||||
|
|||||||
11
src/constants/userPermissions.ts
Normal file
11
src/constants/userPermissions.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import {Type} from "@/interfaces/user";
|
||||||
|
|
||||||
|
export const PERMISSIONS = {
|
||||||
|
generateCode: {
|
||||||
|
student: ["teacher", "admin", "developer", "owner"],
|
||||||
|
teacher: ["admin", "developer", "owner"],
|
||||||
|
admin: ["owner", "developer"],
|
||||||
|
owner: ["developer", "owner"],
|
||||||
|
developer: ["developer"],
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -16,6 +16,9 @@ import Button from "@/components/Low/Button";
|
|||||||
import {getExamById} from "@/utils/exams";
|
import {getExamById} from "@/utils/exams";
|
||||||
import useExamStore from "@/stores/examStore";
|
import useExamStore from "@/stores/examStore";
|
||||||
import {useRouter} from "next/router";
|
import {useRouter} from "next/router";
|
||||||
|
import ShortUniqueId from "short-unique-id";
|
||||||
|
import {Type} from "@/interfaces/user";
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
export const getServerSideProps = withIronSessionSsr(({req, res}) => {
|
export const getServerSideProps = withIronSessionSsr(({req, res}) => {
|
||||||
const user = req.session.user;
|
const user = req.session.user;
|
||||||
@@ -111,6 +114,71 @@ const ExamLoader = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const CodeGenerator = () => {
|
||||||
|
const [generatedCode, setGeneratedCode] = useState<string>();
|
||||||
|
|
||||||
|
const generateCode = (type: Type) => {
|
||||||
|
const uid = new ShortUniqueId();
|
||||||
|
const code = uid.randomUUID(6);
|
||||||
|
|
||||||
|
axios
|
||||||
|
.post("/api/code", {type, code})
|
||||||
|
.then(({data, status}) => {
|
||||||
|
if (data.ok) {
|
||||||
|
toast.success(`Successfully generated a ${capitalize(type)} code!`, {toastId: "success"});
|
||||||
|
setGeneratedCode(code);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status === 403) {
|
||||||
|
toast.error(`You do not have permission to generate a ${capitalize(type)} code!`, {toastId: "forbidden"});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(({response: {status}}) => {
|
||||||
|
if (status === 403) {
|
||||||
|
toast.error(`You do not have permission to generate a ${capitalize(type)} code!`, {toastId: "forbidden"});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.error(`Something went wrong, please try again later!`, {toastId: "error"});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
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">User Code Generator</label>
|
||||||
|
<div className="grid grid-cols-2 gap-4">
|
||||||
|
<Button className="w-48" variant="outline" onClick={() => generateCode("student")}>
|
||||||
|
Student
|
||||||
|
</Button>
|
||||||
|
<Button className="w-48" variant="outline" onClick={() => generateCode("teacher")}>
|
||||||
|
Teacher
|
||||||
|
</Button>
|
||||||
|
<Button className="w-48" variant="outline" onClick={() => generateCode("admin")}>
|
||||||
|
Admin
|
||||||
|
</Button>
|
||||||
|
<Button className="w-48" variant="outline" onClick={() => generateCode("owner")}>
|
||||||
|
Owner
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<label className="font-normal text-base text-mti-gray-dim">Generated Code:</label>
|
||||||
|
<div
|
||||||
|
className={clsx(
|
||||||
|
"p-6 w-full min-h-[70px] flex justify-center text-sm font-normal rounded-full border focus:outline-none cursor-pointer",
|
||||||
|
"hover:border-mti-purple tooltip",
|
||||||
|
"transition duration-300 ease-in-out",
|
||||||
|
)}
|
||||||
|
data-tip="Click to copy"
|
||||||
|
onClick={() => {
|
||||||
|
if (generatedCode) navigator.clipboard.writeText(generatedCode);
|
||||||
|
}}>
|
||||||
|
{generatedCode}
|
||||||
|
</div>
|
||||||
|
{generatedCode && <span className="text-sm text-mti-gray-dim font-light">Give this code to the user to complete their registration</span>}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default function Admin() {
|
export default function Admin() {
|
||||||
const {user} = useUser({redirectTo: "/login"});
|
const {user} = useUser({redirectTo: "/login"});
|
||||||
|
|
||||||
@@ -130,6 +198,7 @@ export default function Admin() {
|
|||||||
<Layout user={user}>
|
<Layout user={user}>
|
||||||
<section className="w-full flex gap-8">
|
<section className="w-full flex gap-8">
|
||||||
<ExamLoader />
|
<ExamLoader />
|
||||||
|
<CodeGenerator />
|
||||||
</section>
|
</section>
|
||||||
</Layout>
|
</Layout>
|
||||||
)}
|
)}
|
||||||
|
|||||||
33
src/pages/api/code.ts
Normal file
33
src/pages/api/code.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
|
import type {NextApiRequest, NextApiResponse} from "next";
|
||||||
|
import {app} from "@/firebase";
|
||||||
|
import {getFirestore, setDoc, doc} from "firebase/firestore";
|
||||||
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
|
import {sessionOptions} from "@/lib/session";
|
||||||
|
import {Type} from "@/interfaces/user";
|
||||||
|
import {PERMISSIONS} from "@/constants/userPermissions";
|
||||||
|
import {uuidv4} from "@firebase/util";
|
||||||
|
|
||||||
|
const db = getFirestore(app);
|
||||||
|
|
||||||
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
if (!req.session.user) {
|
||||||
|
res.status(401).json({ok: false});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const {type, code} = req.body as {type: Type; code: string};
|
||||||
|
const permission = PERMISSIONS.generateCode[type];
|
||||||
|
|
||||||
|
if (!permission.includes(req.session.user.type)) {
|
||||||
|
res.status(403).json({ok: false});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const codeRef = doc(db, "codes", uuidv4());
|
||||||
|
await setDoc(codeRef, {type, code});
|
||||||
|
|
||||||
|
res.status(200).json({ok: true});
|
||||||
|
}
|
||||||
@@ -3284,6 +3284,11 @@ shebang-regex@^3.0.0:
|
|||||||
resolved "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz"
|
resolved "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz"
|
||||||
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==
|
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==
|
||||||
|
|
||||||
|
short-unique-id@^5.0.2:
|
||||||
|
version "5.0.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/short-unique-id/-/short-unique-id-5.0.2.tgz#b09821d8fc1ed89220acce3800013ebce21436dd"
|
||||||
|
integrity sha512-4wZq1VLV4hsEx8guP5bN7XnY8UDsVXtdUDWFMP1gvEieAXolq5fWGKpuua21PRXaLn3OybTKFQNm7JGcHSWu/Q==
|
||||||
|
|
||||||
side-channel@^1.0.4:
|
side-channel@^1.0.4:
|
||||||
version "1.0.4"
|
version "1.0.4"
|
||||||
resolved "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz"
|
resolved "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz"
|
||||||
|
|||||||
Reference in New Issue
Block a user