"use client"; import { useEffect, useState } from "react"; import { cmsApi, Lesson, Block } from "@/lib/api"; import Link from "next/link"; import DescriptionBlock from "@/components/blocks/DescriptionBlock"; import MediaBlock from "@/components/blocks/MediaBlock"; import QuizBlock from "@/components/blocks/QuizBlock"; import FillInTheBlanksBlock from "@/components/blocks/FillInTheBlanksBlock"; import MatchingBlock from "@/components/blocks/MatchingBlock"; import OrderingBlock from "@/components/blocks/OrderingBlock"; import ShortAnswerBlock from "@/components/blocks/ShortAnswerBlock"; export default function LessonEditor({ params }: { params: { id: string; lessonId: string } }) { const [lesson, setLesson] = useState(null); const [loading, setLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); const [editMode, setEditMode] = useState(false); // Activity State (Blocks) const [blocks, setBlocks] = useState([]); useEffect(() => { const loadData = async () => { try { const lessonData: Lesson = await fetch(`http://localhost:3001/lessons/${params.lessonId}`).then(res => res.json()); setLesson(lessonData); if (lessonData.metadata?.blocks) { setBlocks(lessonData.metadata.blocks); } else { setBlocks([ { id: 'initial-desc', type: 'description', content: `Welcome to ${lessonData.title}. Please follow the instructions below.` } ]); } } catch { console.error("Failed to load lesson"); } finally { setLoading(false); } }; loadData(); }, [params.id, params.lessonId]); const handleSave = async () => { if (!lesson) return; setIsSaving(true); try { const updated = await cmsApi.updateLesson(lesson.id, { metadata: { ...lesson.metadata, blocks } }); setLesson(updated); setEditMode(false); } catch { alert("Failed to save activity."); } finally { setIsSaving(false); } }; const addBlock = (type: 'description' | 'media' | 'quiz' | 'fill-in-the-blanks' | 'matching' | 'ordering' | 'short-answer') => { const newBlock: Block = { id: Math.random().toString(36).substr(2, 9), type, ...(type === 'description' && { content: "" }), ...(type === 'media' && { url: "", media_type: 'video' as const, config: { maxPlays: 0 } }), ...(type === 'quiz' && { quiz_data: { questions: [] } }), ...(type === 'fill-in-the-blanks' && { content: "Type your text here with [[blanks]]." }), ...(type === 'matching' && { pairs: [{ left: "Item 1", right: "Match 1" }] }), ...(type === 'ordering' && { items: ["Item A", "Item B"] }), ...(type === 'short-answer' && { prompt: "Question?", correctAnswers: ["Answer"] }), }; setBlocks([...blocks, newBlock]); }; const removeBlock = (id: string) => { setBlocks(blocks.filter(b => b.id !== id)); }; const updateBlock = (id: string, updates: Partial) => { setBlocks(blocks.map(b => b.id === id ? { ...b, ...updates } : b)); }; const moveBlock = (index: number, direction: 'up' | 'down') => { const newBlocks = [...blocks]; const targetIndex = direction === 'up' ? index - 1 : index + 1; if (targetIndex < 0 || targetIndex >= newBlocks.length) return; [newBlocks[index], newBlocks[targetIndex]] = [newBlocks[targetIndex], newBlocks[index]]; setBlocks(newBlocks); }; if (loading) return
Initializing Activity Builder...
; if (!lesson) return
Activity not found.
; return (
Outline / Activity

{lesson.title}

{editMode ? ( <> ) : ( )}
{blocks.map((block, index) => (
{editMode && (
)}
{block.type === 'description' && ( updateBlock(block.id, updates)} /> )} {block.type === 'media' && ( updateBlock(block.id, updates)} /> )} {block.type === 'quiz' && ( updateBlock(block.id, updates)} /> )} {block.type === 'fill-in-the-blanks' && ( updateBlock(block.id, updates)} /> )} {block.type === 'matching' && ( updateBlock(block.id, updates)} /> )} {block.type === 'ordering' && ( updateBlock(block.id, updates)} /> )} {block.type === 'short-answer' && ( updateBlock(block.id, updates)} /> )}
))} {editMode && (
Add Content Block
)}
); }