Skip to content

Commit f4693a8

Browse files
client validation
1 parent 80b044f commit f4693a8

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/app/upload/page.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,33 @@ const Page = () => {
4444
// };
4545

4646
const handlePrint = async () => {
47-
// file validation
4847
const maxFileSize = 5 * 1024 * 1024;
48+
const allowedFileTypes = ["application/pdf", "image/jpeg", "image/png", "image/gif"];
4949
const files = fileInputRef.current?.files as FileList | null;
50-
if (!files || files.length == 0) {
50+
51+
if (!files || files.length === 0) {
5152
toast.error("No files selected");
5253
return;
5354
} else if (files.length > 5) {
5455
toast.error("More than 5 files selected");
5556
return;
5657
}
58+
5759
for (const file of files) {
5860
if (file.size > maxFileSize) {
5961
toast.error(`File ${file.name} is more than 5MB`);
6062
return;
6163
}
64+
65+
if (!allowedFileTypes.includes(file.type)) {
66+
toast.error(`File type of ${file.name} is not allowed. Only PDFs and images are accepted.`);
67+
return;
68+
}
6269
}
6370

6471
const formData = new FormData();
6572
for (const file of files) {
66-
formData.append("files", file); // append each file
73+
formData.append("files", file); // append each file
6774
}
6875
formData.append("slot", slot);
6976
formData.append("subject", subject);
@@ -94,6 +101,7 @@ const Page = () => {
94101
error: (err: ApiError) => err.message,
95102
}
96103
);
104+
97105
if (result?.message === "Email sent successfully!") {
98106
// setTimeout(() => {
99107
// router.push("/");
@@ -102,6 +110,7 @@ const Page = () => {
102110
} catch (e) {}
103111
};
104112

113+
105114

106115
const handleSubjectSelect = (value: string) => {
107116
setSubject(value);

0 commit comments

Comments
 (0)