import { useCourse } from '@components/Contexts/CourseContext' import { useOrg } from '@components/Contexts/OrgContext' import { getAPIUrl } from '@services/config/config' import { updateCourseThumbnail } from '@services/courses/courses' import { getCourseThumbnailMediaDirectory } from '@services/media/media' import { ArrowBigUpDash, UploadCloud, Image as ImageIcon } from 'lucide-react' import { useLHSession } from '@components/Contexts/LHSessionContext' import React, { useState } from 'react' import { mutate } from 'swr' import UnsplashImagePicker from './UnsplashImagePicker' function ThumbnailUpdate() { const course = useCourse() as any const session = useLHSession() as any; const org = useOrg() as any const [localThumbnail, setLocalThumbnail] = React.useState(null) as any const [isLoading, setIsLoading] = React.useState(false) as any const [error, setError] = React.useState('') as any 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) } const handleUnsplashSelect = async (imageUrl: string) => { setIsLoading(true) const response = await fetch(imageUrl) const blob = await response.blob() const file = new File([blob], 'unsplash_image.jpg', { type: 'image/jpeg' }) setLocalThumbnail(file) await updateThumbnail(file) } const updateThumbnail = async (file: File) => { setIsLoading(true) const res = await updateCourseThumbnail( course.courseStructure.course_uuid, file, session.data?.tokens?.access_token ) mutate(`${getAPIUrl()}courses/${course.courseStructure.course_uuid}/meta?with_unpublished_activities=${withUnpublishedActivities}`) // wait for 1 second to show loading animation await new Promise((r) => setTimeout(r, 1500)) if (res.success === false) { setError(res.HTTPmessage) } else { setIsLoading(false) setError('') } } return (
Supported formats: PNG, JPG/JPEG