Skip to content

Commit 4a06668

Browse files
fix
1 parent 67e8c8e commit 4a06668

1 file changed

Lines changed: 68 additions & 59 deletions

File tree

src/app/upload/page.tsx

Lines changed: 68 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -60,53 +60,52 @@ export default function Page() {
6060
}, [previews]);
6161

6262
const fileCheckAndSelect = useCallback((acceptedFiles: File[]) => {
63-
const maxFileSize = 5 * 1024 * 1024;
64-
const allowedFileTypes = [
65-
'application/pdf',
66-
'image/jpeg',
67-
'image/png',
68-
'image/gif',
69-
];
70-
71-
const toastId = toast.loading('uploading your files');
72-
if (!acceptedFiles || acceptedFiles.length === 0) {
73-
toast.error('No files selected', { id: toastId });
74-
return;
75-
}
63+
const maxFileSize = 5 * 1024 * 1024;
64+
const allowedFileTypes = ['application/pdf', 'image/jpeg', 'image/png', 'image/gif'];
7665

77-
if (acceptedFiles.length > 5) {
78-
toast.error('More than 5 files selected', { id: toastId });
79-
return;
80-
}
66+
const toastId = toast.loading('Uploading your files...');
67+
if (!acceptedFiles || acceptedFiles.length === 0) {
68+
toast.error('No files selected', { id: toastId });
69+
return;
70+
}
8171

82-
const invalidFiles = acceptedFiles.filter(
83-
(file) => file.size > maxFileSize || !allowedFileTypes.includes(file.type)
84-
);
85-
if (invalidFiles.length > 0) {
86-
toast.error(
87-
'Some files are invalid. Ensure each file is below 5MB and of an allowed type (PDF, JPEG, PNG, GIF).',
88-
{ id: toastId }
89-
);
90-
return;
91-
}
72+
const isNewPdf = acceptedFiles.some((file) => file.type === 'application/pdf');
73+
// const isExistingPdf = files.some((file) => file.type === 'application/pdf');
9274

93-
const isPdf = acceptedFiles.some((file) => file.type === 'application/pdf');
94-
if (isPdf && acceptedFiles.length > 1) {
95-
toast.error('PDFs must be uploaded separately', { id: toastId });
96-
return;
97-
}
75+
if ((isNewPdf && acceptedFiles.length > 1) || (isNewPdf && files.length > 0)) {
76+
toast.error('PDFs must be uploaded separately', { id: toastId });
77+
return;
78+
}
79+
80+
const allFiles = [...files, ...acceptedFiles];
81+
if (allFiles.length > 5) {
82+
toast.error('You can upload up to 5 files only', { id: toastId });
83+
return;
84+
}
9885

99-
const orderedFiles = acceptedFiles.sort((a, b) => a.lastModified - b.lastModified);
100-
setFiles(orderedFiles);
101-
setPreviews(
102-
orderedFiles.map((file, idx) => ({
103-
id: `${file.name}-${file.lastModified}-${idx}`,
104-
file,
105-
preview: URL.createObjectURL(file),
106-
}))
86+
const invalidFiles = acceptedFiles.filter(
87+
(file) => file.size > maxFileSize || !allowedFileTypes.includes(file.type)
88+
);
89+
90+
if (invalidFiles.length > 0) {
91+
toast.error(
92+
'Some files are invalid. Make sure each is under 5MB and of allowed types (PDF, JPEG, PNG, GIF).',
93+
{ id: toastId }
10794
);
108-
toast.success(`${orderedFiles.length} files selected!`, { id: toastId });
109-
}, []);
95+
return;
96+
}
97+
98+
const newPreviews = acceptedFiles.map((file, idx) => ({
99+
id: `${file.name}-${file.lastModified}-${Date.now()}-${idx}`,
100+
file,
101+
preview: URL.createObjectURL(file),
102+
}));
103+
104+
setFiles((prev) => [...prev, ...acceptedFiles]);
105+
setPreviews((prev) => [...prev, ...newPreviews]);
106+
107+
toast.success(`${acceptedFiles.length} file(s) added!`, { id: toastId });
108+
}, [files]);
110109

111110
const onDrop = useCallback((acceptedFiles: File[]) => {
112111
fileCheckAndSelect(acceptedFiles);
@@ -170,47 +169,56 @@ export default function Page() {
170169
setPreviews(newPreviews);
171170
setFiles(newPreviews.map((p) => p.file));
172171
};
172+
const handlePrint = async () => {
173+
// if (!campus) {
174+
// Vellore
175+
// }
173176

174-
const handlePrint = async () => {
175-
if (files.length === 0) return;
177+
const isPdf = files.length === 1 && files[0]?.type === "application/pdf";
176178

177-
const isPdf = files.length === 1 && files[0]?.type === 'application/pdf';
178179
const formData = new FormData();
179-
180180
files.forEach((file) => {
181-
formData.append('files', file);
181+
formData.append("files", file);
182182
});
183183

184-
formData.append('campus', campus);
185-
formData.append('isPdf', String(isPdf));
184+
formData.append("campus", campus);
185+
console.log("campus", campus);
186+
formData.append("isPdf", String(isPdf));
186187

187188
setIsUploading(true);
188189

189190
try {
190191
await toast.promise(
191192
async () => {
192193
try {
193-
await axios.post<APIResponse>('/api/ai-upload', formData);
194+
console.log("this is happening now");
195+
await axios.post<APIResponse>("/api/upload", formData);
196+
console.log("this is happening after now");
197+
return { message: "Papers uploaded successfully!" };
194198
} catch (error) {
195199
if (error instanceof AxiosError && error.response?.data) {
196200
const errorData = error.response.data as APIResponse;
197-
const errorMessage = errorData.message ?? 'Failed to upload papers';
201+
const errorMessage =
202+
errorData.message ?? "Failed to upload papers";
198203
throw new Error(errorMessage);
199204
}
200-
throw new Error('Failed to upload papers');
205+
throw new Error("Failed to upload papers");
201206
}
202207
},
203208
{
204-
loading: 'Uploading papers...',
205-
success: 'Papers uploaded successfully!',
206-
error: (error: Error) => error.message,
207-
}
209+
loading: "Uploading papers...",
210+
success: "Papers uploaded successfully!",
211+
error: (error: Error) => {
212+
return error.message;
213+
},
214+
},
208215
);
209216

210217
setFiles([]);
211-
setPreviews([]);
212-
} catch {
213-
// Handle error if needed
218+
// setResetSearch(true);
219+
// setTimeout(() => setResetSearch(false), 100);
220+
} catch (error) {
221+
// handleAPIError(error);
214222
} finally {
215223
setIsUploading(false);
216224
}
@@ -306,6 +314,7 @@ export default function Page() {
306314
</SortablePreview>
307315
))}
308316
<div className="relative w-20 h-20 cursor-pointer" {...getRootProps()}>
317+
<input {...getInputProps()} />
309318
<div className="absolute left-4 top-4 w-16 h-16 bg-violet-950 rounded-2xl" />
310319
<div className="absolute left-0 top-0 w-10 h-10 bg-violet-950 rounded-[20px]" />
311320
<div className="absolute left-1 top-1 w-8 h-8 bg-black/50 rounded-[20px]" />

0 commit comments

Comments
 (0)