'use client' import { getAPIUrl } from '@services/config/config' import { revalidateTags } from '@services/utils/ts/requests' import React, { useEffect, useState } from 'react' import { DragDropContext, Droppable } from '@hello-pangea/dnd' import { mutate } from 'swr' import ChapterElement from './DraggableElements/ChapterElement' import PageLoading from '@components/Objects/Loaders/PageLoading' import { createChapter } from '@services/courses/chapters' import { useRouter } from 'next/navigation' import { useCourse, useCourseDispatch, } from '@components/Contexts/CourseContext' import { Hexagon } from 'lucide-react' import Modal from '@components/Objects/StyledElements/Modal/Modal' import NewChapterModal from '@components/Objects/Modals/Chapters/NewChapter' import { useLHSession } from '@components/Contexts/LHSessionContext' type EditCourseStructureProps = { orgslug: string course_uuid?: string } export type OrderPayload = | { chapter_order_by_ids: [ { chapter_id: string activities_order_by_ids: [ { activity_id: string }, ] }, ] } | undefined const EditCourseStructure = (props: EditCourseStructureProps) => { const router = useRouter() const session = useLHSession() as any; const access_token = session?.data?.tokens?.access_token; // Check window availability const [winReady, setwinReady] = useState(false) const dispatchCourse = useCourseDispatch() as any const [order, setOrder] = useState() const course = useCourse() as any const course_structure = course ? course.courseStructure : {} const course_uuid = course ? course.courseStructure.course_uuid : '' // New Chapter creation const [newChapterModal, setNewChapterModal] = useState(false) const closeNewChapterModal = async () => { setNewChapterModal(false) } // Submit new chapter const submitChapter = async (chapter: any) => { await createChapter(chapter,access_token) mutate(`${getAPIUrl()}courses/${course.courseStructure.course_uuid}/meta`) await revalidateTags(['courses'], props.orgslug) router.refresh() setNewChapterModal(false) } const updateStructure = (result: any) => { const { destination, source, draggableId, type } = result if (!destination) return if ( destination.droppableId === source.droppableId && destination.index === source.index ) return if (type === 'chapter') { const newChapterOrder = Array.from(course_structure.chapters) newChapterOrder.splice(source.index, 1) newChapterOrder.splice( destination.index, 0, course_structure.chapters[source.index] ) dispatchCourse({ type: 'setCourseStructure', payload: { ...course_structure, chapters: newChapterOrder }, }) dispatchCourse({ type: 'setIsNotSaved' }) } if (type === 'activity') { const newChapterOrder = Array.from(course_structure.chapters) const sourceChapter = newChapterOrder.find( (chapter: any) => chapter.chapter_uuid === source.droppableId ) as any const destinationChapter = newChapterOrder.find( (chapter: any) => chapter.chapter_uuid === destination.droppableId ) ? newChapterOrder.find( (chapter: any) => chapter.chapter_uuid === destination.droppableId ) : sourceChapter const activity = sourceChapter.activities.find( (activity: any) => activity.activity_uuid === draggableId ) sourceChapter.activities.splice(source.index, 1) destinationChapter.activities.splice(destination.index, 0, activity) dispatchCourse({ type: 'setCourseStructure', payload: { ...course_structure, chapters: newChapterOrder }, }) dispatchCourse({ type: 'setIsNotSaved' }) } } useEffect(() => { setwinReady(true) }, [props.course_uuid, course_structure, course]) if (!course) return return (
{winReady ? ( {(provided) => (
{course_structure.chapters && course_structure.chapters.map((chapter: any, index: any) => { return ( ) })} {provided.placeholder}
)}
{/* New Chapter Modal */} } dialogTitle="Create chapter" dialogDescription="Add a new chapter to the course" dialogTrigger={
Add Chapter
} />
) : ( <> )}
) } export default EditCourseStructure