'use client' import { Input } from "@components/ui/input" import { Textarea } from "@components/ui/textarea" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@components/ui/select" import FormLayout, { FormField, FormLabelAndMessage, } from '@components/Objects/StyledElements/Form/Form' import * as Form from '@radix-ui/react-form' import { createNewCourse } from '@services/courses/courses' import { getOrganizationContextInfoWithoutCredentials } from '@services/organizations/orgs' import React, { useEffect } from 'react' import { BarLoader } from 'react-spinners' import { revalidateTags } from '@services/utils/ts/requests' import { useRouter } from 'next/navigation' import { useLHSession } from '@components/Contexts/LHSessionContext' import toast from 'react-hot-toast' import { useFormik } from 'formik' import * as Yup from 'yup' import { UploadCloud, Image as ImageIcon } from 'lucide-react' import UnsplashImagePicker from "@components/Dashboard/Pages/Course/EditCourseGeneral/UnsplashImagePicker" const validationSchema = Yup.object().shape({ name: Yup.string() .required('Course name is required') .max(100, 'Must be 100 characters or less'), description: Yup.string() .max(1000, 'Must be 1000 characters or less'), learnings: Yup.string(), tags: Yup.string(), visibility: Yup.boolean(), thumbnail: Yup.mixed().nullable() }) function CreateCourseModal({ closeModal, orgslug }: any) { const router = useRouter() const session = useLHSession() as any const [orgId, setOrgId] = React.useState(null) as any const [showUnsplashPicker, setShowUnsplashPicker] = React.useState(false) const [isUploading, setIsUploading] = React.useState(false) const formik = useFormik({ initialValues: { name: '', description: '', learnings: '', visibility: true, tags: '', thumbnail: null }, validationSchema, onSubmit: async (values, { setSubmitting }) => { const toast_loading = toast.loading('Creating course...') try { const res = await createNewCourse( orgId, { name: values.name, description: values.description, tags: values.tags, visibility: values.visibility }, values.thumbnail, session.data?.tokens?.access_token ) if (res.success) { await revalidateTags(['courses'], orgslug) toast.dismiss(toast_loading) toast.success('Course created successfully') if (res.data.org_id === orgId) { closeModal() router.refresh() await revalidateTags(['courses'], orgslug) } } else { toast.error(res.data.detail) } } catch (error) { toast.error('Failed to create course') } finally { setSubmitting(false) } } }) const getOrgMetadata = async () => { const org = await getOrganizationContextInfoWithoutCredentials(orgslug, { revalidate: 360, tags: ['organizations'], }) setOrgId(org.id) } useEffect(() => { if (orgslug) { getOrgMetadata() } }, [orgslug]) const handleFileChange = async (event: React.ChangeEvent) => { const file = event.target.files?.[0] if (file) { formik.setFieldValue('thumbnail', file) } } const handleUnsplashSelect = async (imageUrl: string) => { setIsUploading(true) try { const response = await fetch(imageUrl) const blob = await response.blob() const file = new File([blob], 'unsplash_image.jpg', { type: 'image/jpeg' }) formik.setFieldValue('thumbnail', file) } catch (error) { toast.error('Failed to load image from Unsplash') } setIsUploading(false) } return (