Started trying out reading Excel files

This commit is contained in:
Tiago Ribeiro
2024-01-11 13:55:37 +00:00
parent fd1af3efee
commit f79857fabe
8 changed files with 213 additions and 29 deletions

View File

@@ -13,6 +13,7 @@ import ReactDatePicker from "react-datepicker";
import {toast} from "react-toastify";
import ShortUniqueId from "short-unique-id";
import {useFilePicker} from "use-file-picker";
import readXlsxFile from "read-excel-file";
export default function BatchCodeGenerator({user}: {user: User}) {
const [emails, setEmails] = useState<string[]>([]);
@@ -24,8 +25,9 @@ export default function BatchCodeGenerator({user}: {user: User}) {
const {users} = useUsers();
const {openFilePicker, filesContent} = useFilePicker({
accept: ".txt",
accept: ".xlsx",
multiple: false,
readAs: "ArrayBuffer",
});
useEffect(() => {
@@ -41,18 +43,21 @@ export default function BatchCodeGenerator({user}: {user: User}) {
useEffect(() => {
if (filesContent.length > 0) {
const file = filesContent[0];
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) => !users.map((u) => u.email).includes(x));
readXlsxFile(file.content).then((rows) => {
console.log(rows);
});
// 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) => !users.map((u) => u.email).includes(x));
if (emails.length === 0) {
toast.error("Please upload a .txt file containing e-mails, one per line! All already registered e-mails have also been ignored!");
return;
}
// if (emails.length === 0) {
// 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)]);
// setEmails([...new Set(emails)]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [filesContent]);
@@ -63,7 +68,7 @@ export default function BatchCodeGenerator({user}: {user: User}) {
setIsLoading(true);
axios
.post("/api/code", {type, codes, emails, expiryDate})
.post("/api/code", {type, codes, infos: emails.map((x) => ({email: x})), expiryDate})
.then(({data, status}) => {
if (data.ok) {
toast.success(`Successfully generated ${capitalize(type)} codes and they have been notified by e-mail!`, {toastId: "success"});