From 6a5886cefe25c94065faa1a106e3a7a18dcde874 Mon Sep 17 00:00:00 2001 From: swve Date: Thu, 24 Apr 2025 21:59:32 +0200 Subject: [PATCH] feat: add file type validation and improve thumbnail upload UI in ThumbnailUpdate component --- .../EditCourseGeneral/ThumbnailUpdate.tsx | 100 +++++++++++------- 1 file changed, 63 insertions(+), 37 deletions(-) diff --git a/apps/web/components/Dashboard/Pages/Course/EditCourseGeneral/ThumbnailUpdate.tsx b/apps/web/components/Dashboard/Pages/Course/EditCourseGeneral/ThumbnailUpdate.tsx index 461508d5..0564a9a1 100644 --- a/apps/web/components/Dashboard/Pages/Course/EditCourseGeneral/ThumbnailUpdate.tsx +++ b/apps/web/components/Dashboard/Pages/Course/EditCourseGeneral/ThumbnailUpdate.tsx @@ -19,8 +19,24 @@ function ThumbnailUpdate() { const [showUnsplashPicker, setShowUnsplashPicker] = useState(false) const withUnpublishedActivities = course ? course.withUnpublishedActivities : false + const validateFileType = (file: File): boolean => { + const validTypes = ['image/jpeg', 'image/jpg', 'image/png']; + if (!validTypes.includes(file.type)) { + setError('Please upload only PNG or JPG/JPEG images'); + return false; + } + return true; + } + const handleFileChange = async (event: any) => { const file = event.target.files[0] + if (!file) return; + + if (!validateFileType(file)) { + event.target.value = ''; + return; + } + setLocalThumbnail(file) await updateThumbnail(file) } @@ -53,64 +69,74 @@ function ThumbnailUpdate() { } return ( -
-
-
-
- {error && ( -
-
{error}
-
- )} - {localThumbnail ? ( - - ) : ( - - )} +
+
+ {error && ( +
+
{error}
- {isLoading ? ( -
-
- - Uploading -
-
+ )} + +
+ {localThumbnail ? ( + Course thumbnail ) : ( -
+ Course thumbnail + )} + + {!isLoading && ( +
)}
+ + {isLoading && ( +
+
+ + Uploading... +
+
+ )} + +

Supported formats: PNG, JPG/JPEG

+ {showUnsplashPicker && (