Skip to content

Commit 7307175

Browse files
clinet validation
1 parent e944c9e commit 7307175

File tree

3 files changed

+34
-46
lines changed

3 files changed

+34
-46
lines changed

src/app/api/mail/route.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ type MailOptions = {
1515
}[];
1616
};
1717

18-
// Allowed MIME types for PDF and image files
1918
const ALLOWED_MIME_TYPES = [
2019
"application/pdf",
2120
"image/jpeg",
2221
"image/png",
2322
"image/gif",
2423
];
25-
const MAX_FILE_SIZE_MB = 5; // Limit file size to 5 MB
26-
const ALLOWED_EXTENSIONS = [".pdf", ".jpg", ".jpeg", ".png", ".gif"]; // Allowed file extensions
24+
const MAX_FILE_SIZE_MB = 5;
25+
const ALLOWED_EXTENSIONS = [".pdf", ".jpg", ".jpeg", ".png", ".gif"];
2726

2827
export async function POST(request: Request) {
2928
try {
@@ -69,13 +68,12 @@ export async function POST(request: Request) {
6968
const attachments: { filename: string; content: Buffer }[] = [];
7069
const files = formData.getAll("files");
7170

72-
// Validate files and prepare attachments
7371
for (const file of files) {
7472
if (file instanceof Blob) {
7573
const fileType = file.type;
7674
const fileName = (file as any).name;
7775
const fileExtension = path.extname(fileName).toLowerCase();
78-
const fileSizeMB = file.size / (1024 * 1024); // Convert size to MB
76+
const fileSizeMB = file.size / (1024 * 1024);
7977

8078
if (!ALLOWED_MIME_TYPES.includes(fileType) || !ALLOWED_EXTENSIONS.includes(fileExtension)) {
8179
return NextResponse.json(

src/app/upload/page.tsx

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import Footer from "@/components/Footer";
3030

3131
const Page = () => {
3232
const router = useRouter();
33-
// const [openCamera, setOpenCamera] = useState(false);
3433
const fileInputRef = useRef<HTMLInputElement>(null);
3534

3635
const [slot, setSlot] = useState("");
@@ -41,15 +40,29 @@ const Page = () => {
4140
const [inputValue, setInputValue] = useState('')
4241

4342
const [isSubjectCommandOpen, setIsSubjectCommandOpen] = useState(false);
44-
// const toggleOpenCamera = () => {
45-
// setOpenCamera((prev) => !prev);
46-
// };
43+
const [isUploading, setIsUploading] = useState(false);
4744

4845
const handlePrint = async () => {
4946
const maxFileSize = 5 * 1024 * 1024;
5047
const allowedFileTypes = ["application/pdf", "image/jpeg", "image/png", "image/gif"];
5148
const files = fileInputRef.current?.files as FileList | null;
5249

50+
if (!slot) {
51+
toast.error("Slot is required");
52+
return;
53+
}
54+
if (!subject) {
55+
toast.error("Subject is required");
56+
return;
57+
}
58+
if (!exam) {
59+
toast.error("Exam is required");
60+
return;
61+
}
62+
if (!year) {
63+
toast.error("Year is required");
64+
return;
65+
}
5366
if (!files || files.length === 0) {
5467
toast.error("No files selected");
5568
return;
@@ -72,13 +85,15 @@ const Page = () => {
7285

7386
const formData = new FormData();
7487
for (const file of files) {
75-
formData.append("files", file); // append each file
88+
formData.append("files", file);
7689
}
7790
formData.append("slot", slot);
7891
formData.append("subject", subject);
7992
formData.append("exam", exam);
8093
formData.append("year", year);
8194

95+
setIsUploading(true); // Set uploading to true
96+
8297
try {
8398
const result = await toast.promise(
8499
(async () => {
@@ -103,17 +118,15 @@ const Page = () => {
103118
error: (err: ApiError) => err.message,
104119
}
105120
);
106-
121+
107122
if (result?.message === "Email sent successfully!") {
108-
// setTimeout(() => {
109-
// router.push("/");
110-
// }, 1500);
111123
}
112-
} catch (e) {}
124+
} catch (e) {
125+
} finally {
126+
setIsUploading(false);
127+
}
113128
};
114129

115-
116-
117130
const handleSubjectSelect = (value: string) => {
118131
setSubject(value);
119132
setIsSubjectCommandOpen(false);
@@ -170,30 +183,6 @@ const Page = () => {
170183
{/* Subject Selection */}
171184
<div>
172185
<label>Subject:</label>
173-
{/* <div className="relative">
174-
<Button
175-
type="button"
176-
onClick={() => setIsSubjectCommandOpen((prev) => !prev)}
177-
className="m-2 rounded-md border p-2"
178-
>
179-
{subject || "Select subject"}
180-
</Button>
181-
{isSubjectCommandOpen && (
182-
<Command className="absolute z-10 mt-2 w-full rounded-lg border shadow-md">
183-
<CommandInput placeholder="Search subject..." />
184-
<CommandList>
185-
<CommandEmpty>No subjects found.</CommandEmpty>
186-
<CommandGroup heading="Subjects">
187-
{courses.map((course) => (
188-
<CommandItem key={course} onSelect={() => handleSubjectSelect(course)}>
189-
<span>{course}</span>
190-
</CommandItem>
191-
))}
192-
</CommandGroup>
193-
</CommandList>
194-
</Command>
195-
)}
196-
</div> */}
197186
<Command className="rounded-lg border shadow-md md:min-w-[450px]">
198187
<CommandInput
199188
value={inputValue}
@@ -250,7 +239,7 @@ const Page = () => {
250239
<Input
251240
required
252241
type="file"
253-
accept="image/*,.pdf"
242+
// accept="image/*,.pdf"
254243
multiple
255244
ref={fileInputRef}
256245
className="hidden"
@@ -262,7 +251,7 @@ const Page = () => {
262251
<div>
263252
<Button
264253
type="button"
265-
onClick={() => fileInputRef.current?.click()} // Trigger file input on button click
254+
onClick={() => fileInputRef.current?.click()}
266255
className="rounded-md px-4 py-2 transition"
267256
>
268257
Choose files
@@ -278,9 +267,10 @@ const Page = () => {
278267
</fieldset>
279268
<Button
280269
onClick={handlePrint}
281-
className="w-fit rounded-md px-4 py-3"
270+
disabled={isUploading}
271+
className={`w-fit rounded-md px-4 py-3 ${isUploading ? "bg-gray-300" : ""}`}
282272
>
283-
Upload Papers
273+
{isUploading ? "Uploading..." : "Upload Papers"}
284274
</Button>
285275
</div>
286276
<div className="">

src/components/Footer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default function Footer() {
5858
width={24}
5959
/>
6060
</Link>
61-
<Link href="https://x.com/codechefvit">
61+
<Link href="https://x.com/codechefvit" className="pb-1.5">
6262
<Image
6363
src={isDarkMode ? x_twitter_icon_dark : x_twitter_icon}
6464
alt="x_twitter_icon"

0 commit comments

Comments
 (0)