- Added the option to select an expiry date when an owner or dev creates a code
- Made it so the student's expiry date is the same as the admin when created by one
This commit is contained in:
@@ -1,21 +1,38 @@
|
|||||||
import Button from "@/components/Low/Button";
|
import Button from "@/components/Low/Button";
|
||||||
import {Type} from "@/interfaces/user";
|
import Checkbox from "@/components/Low/Checkbox";
|
||||||
|
import {PERMISSIONS} from "@/constants/userPermissions";
|
||||||
|
import {Type, User} from "@/interfaces/user";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import clsx from "clsx";
|
import clsx from "clsx";
|
||||||
import {capitalize} from "lodash";
|
import {capitalize} from "lodash";
|
||||||
|
import moment from "moment";
|
||||||
import {useEffect, useState} from "react";
|
import {useEffect, useState} from "react";
|
||||||
|
import ReactDatePicker from "react-datepicker";
|
||||||
import {toast} from "react-toastify";
|
import {toast} from "react-toastify";
|
||||||
import ShortUniqueId from "short-unique-id";
|
import ShortUniqueId from "short-unique-id";
|
||||||
import {useFilePicker} from "use-file-picker";
|
import {useFilePicker} from "use-file-picker";
|
||||||
|
|
||||||
export default function BatchCodeGenerator() {
|
export default function BatchCodeGenerator({user}: {user: User}) {
|
||||||
const [emails, setEmails] = useState<string[]>([]);
|
const [emails, setEmails] = useState<string[]>([]);
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [expiryDate, setExpiryDate] = useState<Date | null>(null);
|
||||||
|
const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true);
|
||||||
|
|
||||||
const {openFilePicker, filesContent} = useFilePicker({
|
const {openFilePicker, filesContent} = useFilePicker({
|
||||||
accept: ".txt",
|
accept: ".txt",
|
||||||
multiple: false,
|
multiple: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (user.type === "admin" || user.type === "teacher") {
|
||||||
|
setExpiryDate(user.subscriptionExpirationDate || null);
|
||||||
|
}
|
||||||
|
}, [user]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isExpiryDateEnabled) setExpiryDate(null);
|
||||||
|
}, [isExpiryDateEnabled]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (filesContent.length > 0) {
|
if (filesContent.length > 0) {
|
||||||
const file = filesContent[0];
|
const file = filesContent[0];
|
||||||
@@ -38,7 +55,7 @@ export default function BatchCodeGenerator() {
|
|||||||
|
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
axios
|
axios
|
||||||
.post("/api/code", {type, codes, emails})
|
.post("/api/code", {type, codes, emails, expiryDate})
|
||||||
.then(({data, status}) => {
|
.then(({data, status}) => {
|
||||||
if (data.ok) {
|
if (data.ok) {
|
||||||
toast.success(`Successfully generated ${capitalize(type)} codes and they have been notified by e-mail!`, {toastId: "success"});
|
toast.success(`Successfully generated ${capitalize(type)} codes and they have been notified by e-mail!`, {toastId: "success"});
|
||||||
@@ -66,18 +83,57 @@ export default function BatchCodeGenerator() {
|
|||||||
<Button onClick={openFilePicker} isLoading={isLoading} disabled={isLoading}>
|
<Button onClick={openFilePicker} isLoading={isLoading} disabled={isLoading}>
|
||||||
{filesContent.length > 0 ? filesContent[0].name : "Choose a file"}
|
{filesContent.length > 0 ? filesContent[0].name : "Choose a file"}
|
||||||
</Button>
|
</Button>
|
||||||
|
{(user.type === "developer" || user.type === "owner") && (
|
||||||
|
<>
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<label className="font-normal text-base text-mti-gray-dim">Expiry Date</label>
|
||||||
|
<Checkbox isChecked={isExpiryDateEnabled} onChange={setIsExpiryDateEnabled}>
|
||||||
|
Enabled
|
||||||
|
</Checkbox>
|
||||||
|
</div>
|
||||||
|
{isExpiryDateEnabled && (
|
||||||
|
<ReactDatePicker
|
||||||
|
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",
|
||||||
|
)}
|
||||||
|
filterDate={(date) => moment(date).isAfter(new Date())}
|
||||||
|
dateFormat="dd/MM/yyyy"
|
||||||
|
selected={expiryDate}
|
||||||
|
onChange={(date) => setExpiryDate(date)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
<label className="font-normal text-base text-mti-gray-dim">Select the type of user they should be</label>
|
<label className="font-normal text-base text-mti-gray-dim">Select the type of user they should be</label>
|
||||||
<div className="grid grid-cols-2 gap-4">
|
<div className="grid grid-cols-2 gap-4">
|
||||||
<Button className="w-48" variant="outline" onClick={() => generateCode("student")} disabled={emails.length === 0 || isLoading}>
|
<Button
|
||||||
|
className="w-48"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => generateCode("student")}
|
||||||
|
disabled={emails.length === 0 || isLoading || !PERMISSIONS.generateCode.student.includes(user.type)}>
|
||||||
Student
|
Student
|
||||||
</Button>
|
</Button>
|
||||||
<Button className="w-48" variant="outline" onClick={() => generateCode("teacher")} disabled={emails.length === 0 || isLoading}>
|
<Button
|
||||||
|
className="w-48"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => generateCode("teacher")}
|
||||||
|
disabled={emails.length === 0 || isLoading || !PERMISSIONS.generateCode.teacher.includes(user.type)}>
|
||||||
Teacher
|
Teacher
|
||||||
</Button>
|
</Button>
|
||||||
<Button className="w-48" variant="outline" onClick={() => generateCode("admin")} disabled={emails.length === 0 || isLoading}>
|
<Button
|
||||||
|
className="w-48"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => generateCode("admin")}
|
||||||
|
disabled={emails.length === 0 || isLoading || !PERMISSIONS.generateCode.admin.includes(user.type)}>
|
||||||
Admin
|
Admin
|
||||||
</Button>
|
</Button>
|
||||||
<Button className="w-48" variant="outline" onClick={() => generateCode("owner")} disabled={emails.length === 0 || isLoading}>
|
<Button
|
||||||
|
className="w-48"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => generateCode("owner")}
|
||||||
|
disabled={emails.length === 0 || isLoading || !PERMISSIONS.generateCode.owner.includes(user.type)}>
|
||||||
Owner
|
Owner
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,21 +1,37 @@
|
|||||||
import Button from "@/components/Low/Button";
|
import Button from "@/components/Low/Button";
|
||||||
import {Type} from "@/interfaces/user";
|
import Checkbox from "@/components/Low/Checkbox";
|
||||||
|
import {PERMISSIONS} from "@/constants/userPermissions";
|
||||||
|
import {Type, User} from "@/interfaces/user";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import clsx from "clsx";
|
import clsx from "clsx";
|
||||||
import {capitalize} from "lodash";
|
import {capitalize} from "lodash";
|
||||||
import {useState} from "react";
|
import moment from "moment";
|
||||||
|
import {useEffect, useState} from "react";
|
||||||
|
import ReactDatePicker from "react-datepicker";
|
||||||
import {toast} from "react-toastify";
|
import {toast} from "react-toastify";
|
||||||
import ShortUniqueId from "short-unique-id";
|
import ShortUniqueId from "short-unique-id";
|
||||||
|
|
||||||
export default function CodeGenerator() {
|
export default function CodeGenerator({user}: {user: User}) {
|
||||||
const [generatedCode, setGeneratedCode] = useState<string>();
|
const [generatedCode, setGeneratedCode] = useState<string>();
|
||||||
|
const [expiryDate, setExpiryDate] = useState<Date | null>(null);
|
||||||
|
const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (user.type === "admin" || user.type === "teacher") {
|
||||||
|
setExpiryDate(user.subscriptionExpirationDate || null);
|
||||||
|
}
|
||||||
|
}, [user]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isExpiryDateEnabled) setExpiryDate(null);
|
||||||
|
}, [isExpiryDateEnabled]);
|
||||||
|
|
||||||
const generateCode = (type: Type) => {
|
const generateCode = (type: Type) => {
|
||||||
const uid = new ShortUniqueId();
|
const uid = new ShortUniqueId();
|
||||||
const code = uid.randomUUID(6);
|
const code = uid.randomUUID(6);
|
||||||
|
|
||||||
axios
|
axios
|
||||||
.post("/api/code", {type, codes: [code]})
|
.post("/api/code", {type, codes: [code], expiryDate})
|
||||||
.then(({data, status}) => {
|
.then(({data, status}) => {
|
||||||
if (data.ok) {
|
if (data.ok) {
|
||||||
toast.success(`Successfully generated a ${capitalize(type)} code!`, {toastId: "success"});
|
toast.success(`Successfully generated a ${capitalize(type)} code!`, {toastId: "success"});
|
||||||
@@ -41,19 +57,58 @@ export default function CodeGenerator() {
|
|||||||
<div className="flex flex-col gap-4 border p-4 border-mti-gray-platinum rounded-xl">
|
<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>
|
<label className="font-normal text-base text-mti-gray-dim">User Code Generator</label>
|
||||||
<div className="grid grid-cols-2 gap-4">
|
<div className="grid grid-cols-2 gap-4">
|
||||||
<Button className="w-48" variant="outline" onClick={() => generateCode("student")}>
|
<Button
|
||||||
|
className="w-48"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => generateCode("student")}
|
||||||
|
disabled={!PERMISSIONS.generateCode.student.includes(user.type) || (isExpiryDateEnabled && expiryDate === null)}>
|
||||||
Student
|
Student
|
||||||
</Button>
|
</Button>
|
||||||
<Button className="w-48" variant="outline" onClick={() => generateCode("teacher")}>
|
<Button
|
||||||
|
className="w-48"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => generateCode("teacher")}
|
||||||
|
disabled={!PERMISSIONS.generateCode.teacher.includes(user.type) || (isExpiryDateEnabled && expiryDate === null)}>
|
||||||
Teacher
|
Teacher
|
||||||
</Button>
|
</Button>
|
||||||
<Button className="w-48" variant="outline" onClick={() => generateCode("admin")}>
|
<Button
|
||||||
|
className="w-48"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => generateCode("admin")}
|
||||||
|
disabled={!PERMISSIONS.generateCode.admin.includes(user.type) || (isExpiryDateEnabled && expiryDate === null)}>
|
||||||
Admin
|
Admin
|
||||||
</Button>
|
</Button>
|
||||||
<Button className="w-48" variant="outline" onClick={() => generateCode("owner")}>
|
<Button
|
||||||
|
className="w-48"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => generateCode("owner")}
|
||||||
|
disabled={!PERMISSIONS.generateCode.owner.includes(user.type) || (isExpiryDateEnabled && expiryDate === null)}>
|
||||||
Owner
|
Owner
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
{(user.type === "developer" || user.type === "owner") && (
|
||||||
|
<>
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<label className="font-normal text-base text-mti-gray-dim">Expiry Date</label>
|
||||||
|
<Checkbox isChecked={isExpiryDateEnabled} onChange={setIsExpiryDateEnabled}>
|
||||||
|
Enabled
|
||||||
|
</Checkbox>
|
||||||
|
</div>
|
||||||
|
{isExpiryDateEnabled && (
|
||||||
|
<ReactDatePicker
|
||||||
|
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",
|
||||||
|
)}
|
||||||
|
filterDate={(date) => moment(date).isAfter(new Date())}
|
||||||
|
dateFormat="dd/MM/yyyy"
|
||||||
|
selected={expiryDate}
|
||||||
|
onChange={(date) => setExpiryDate(date)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
<label className="font-normal text-base text-mti-gray-dim">Generated Code:</label>
|
<label className="font-normal text-base text-mti-gray-dim">Generated Code:</label>
|
||||||
<div
|
<div
|
||||||
className={clsx(
|
className={clsx(
|
||||||
|
|||||||
@@ -61,8 +61,8 @@ export default function Admin() {
|
|||||||
<Layout user={user} className="gap-6">
|
<Layout user={user} className="gap-6">
|
||||||
<section className="w-full flex gap-8 justify-between">
|
<section className="w-full flex gap-8 justify-between">
|
||||||
<ExamLoader />
|
<ExamLoader />
|
||||||
<CodeGenerator />
|
<CodeGenerator user={user} />
|
||||||
<BatchCodeGenerator />
|
<BatchCodeGenerator user={user} />
|
||||||
</section>
|
</section>
|
||||||
<section className="w-full">
|
<section className="w-full">
|
||||||
<Lists user={user} />
|
<Lists user={user} />
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const {type, codes, emails} = req.body as {type: Type; codes: string[]; emails?: string[]};
|
const {type, codes, emails, expiryDate} = req.body as {type: Type; codes: string[]; emails?: string[]; expiryDate: null | Date};
|
||||||
const permission = PERMISSIONS.generateCode[type];
|
const permission = PERMISSIONS.generateCode[type];
|
||||||
|
|
||||||
if (!permission.includes(req.session.user.type)) {
|
if (!permission.includes(req.session.user.type)) {
|
||||||
@@ -29,7 +29,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
const codePromises = codes.map(async (code, index) => {
|
const codePromises = codes.map(async (code, index) => {
|
||||||
const codeRef = doc(db, "codes", code);
|
const codeRef = doc(db, "codes", code);
|
||||||
await setDoc(codeRef, {type, code, creator: req.session.user!.id});
|
await setDoc(codeRef, {type, code, creator: req.session.user!.id, expiryDate});
|
||||||
|
|
||||||
if (emails && emails.length > index) {
|
if (emails && emails.length > index) {
|
||||||
const transport = prepareMailer();
|
const transport = prepareMailer();
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ async function login(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const codeData = codeDocs[0].data() as {code: string; type: Type; creator: string};
|
const codeData = codeDocs[0].data() as {code: string; type: Type; creator: string; expiryDate: Date | null};
|
||||||
|
|
||||||
createUserWithEmailAndPassword(auth, email, password)
|
createUserWithEmailAndPassword(auth, email, password)
|
||||||
.then(async (userCredentials) => {
|
.then(async (userCredentials) => {
|
||||||
@@ -52,6 +52,7 @@ async function login(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
isFirstLogin: codeData.type === "student",
|
isFirstLogin: codeData.type === "student",
|
||||||
focus: "academic",
|
focus: "academic",
|
||||||
type: codeData.type,
|
type: codeData.type,
|
||||||
|
subscriptionExpirationDate: codeData.expiryDate,
|
||||||
};
|
};
|
||||||
|
|
||||||
await setDoc(doc(db, "users", userId), user);
|
await setDoc(doc(db, "users", userId), user);
|
||||||
|
|||||||
Reference in New Issue
Block a user