'use client' import FormLayout, { ButtonBlack, Flex, FormField, FormLabel, Input, Textarea, } from '@components/StyledElements/Form/Form' import * as Form from '@radix-ui/react-form' import { FormMessage } from '@radix-ui/react-form' import { createNewCourse } from '@services/courses/courses' import { getOrganizationContextInfoWithoutCredentials } from '@services/organizations/orgs' import React, { useState } 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' function CreateCourseModal({ closeModal, orgslug }: any) { const [isSubmitting, setIsSubmitting] = useState(false) const session = useLHSession() as any; const [name, setName] = React.useState('') const [description, setDescription] = React.useState('') const [learnings, setLearnings] = React.useState('') const [visibility, setVisibility] = React.useState(true) const [tags, setTags] = React.useState('') const [isLoading, setIsLoading] = React.useState(false) const [thumbnail, setThumbnail] = React.useState(null) as any const router = useRouter() const [orgId, setOrgId] = React.useState(null) as any const [org, setOrg] = React.useState(null) as any const getOrgMetadata = async () => { const org = await getOrganizationContextInfoWithoutCredentials(orgslug, { revalidate: 360, tags: ['organizations'], }) setOrgId(org.id) } const handleNameChange = (event: React.ChangeEvent) => { setName(event.target.value) } const handleDescriptionChange = (event: React.ChangeEvent) => { setDescription(event.target.value) } const handleLearningsChange = (event: React.ChangeEvent) => { setLearnings(event.target.value) } const handleVisibilityChange = (event: React.ChangeEvent) => { setVisibility(event.target.value) } const handleTagsChange = (event: React.ChangeEvent) => { setTags(event.target.value) } const handleThumbnailChange = (event: React.ChangeEvent) => { setThumbnail(event.target.files[0]) } const handleSubmit = async (e: any) => { e.preventDefault() setIsSubmitting(true) let res = await createNewCourse( orgId, { name, description, tags, visibility }, thumbnail, session.data?.tokens?.access_token ) const toast_loading = toast.loading('Creating course...') if (res.success) { await revalidateTags(['courses'], orgslug) setIsSubmitting(false) toast.dismiss(toast_loading) toast.success('Course created successfully') if (res.data.org_id == orgId) { closeModal() router.refresh() await revalidateTags(['courses'], orgslug) } } else { setIsSubmitting(false) toast.error(res.data.detail) } } React.useEffect(() => { if (orgslug) { getOrgMetadata() } }, [isLoading, orgslug]) return ( Course name Please provide a course name Course description Please provide a course description