More housekeeping

This commit is contained in:
Tiago Ribeiro
2023-11-27 11:22:41 +00:00
parent f36c63f1b2
commit facac33a89
2 changed files with 13 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
import Button from "@/components/Low/Button";
import Checkbox from "@/components/Low/Checkbox";
import {PERMISSIONS} from "@/constants/userPermissions";
import useUsers from "@/hooks/useUsers";
import {Type, User} from "@/interfaces/user";
import {USER_TYPE_LABELS} from "@/resources/user";
import axios from "axios";
@@ -20,6 +21,8 @@ export default function BatchCodeGenerator({user}: {user: User}) {
const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true);
const [type, setType] = useState<Type>("student");
const {users} = useUsers();
const {openFilePicker, filesContent} = useFilePicker({
accept: ".txt",
multiple: false,
@@ -41,15 +44,17 @@ export default function BatchCodeGenerator({user}: {user: User}) {
const emails = file.content
.split("\n")
.map((x) => x.trim())
.filter((x) => new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/).test(x));
.filter((x) => new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/).test(x))
.filter((x) => !users.map((u) => u.email).includes(x));
if (emails.length === 0) {
toast.error("Please upload a .txt file containing e-mails, one per line!");
toast.error("Please upload a .txt file containing e-mails, one per line! All already registered e-mails have also been ignored!");
return;
}
setEmails([...new Set(emails)]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [filesContent]);
const generateCode = (type: Type) => {
@@ -122,7 +127,9 @@ export default function BatchCodeGenerator({user}: {user: User}) {
))}
</select>
)}
<Button onClick={() => generateCode(type)}>Generate & Send</Button>
<Button onClick={() => generateCode(type)} disabled={emails.length === 0 || (isExpiryDateEnabled ? !expiryDate : false)}>
Generate & Send
</Button>
</div>
);
}