import { createSignal } from 'solid-js'; import { Button } from '@/components/ui/Button'; import { Input } from '@/components/ui/Input'; import { ModalPortal } from '@/components/ui/ModalPortal'; import { IconX } from '@tabler/icons-solidjs'; interface LearningPathFormData { title: string; description: string; category: string; difficulty: 'beginner' | 'intermediate' | 'advanced'; duration: string; thumbnail?: string; is_featured?: boolean; } interface LearningPathModalProps { isOpen: boolean; onClose: () => void; onSubmit: (learningPath: LearningPathFormData) => Promise; learningPath?: LearningPathFormData | null; isEdit?: boolean; } export const LearningPathModal = (props: LearningPathModalProps) => { const [learningPathData, setLearningPathData] = createSignal({ title: '', description: '', category: '', difficulty: 'beginner', duration: '', thumbnail: '', is_featured: false }); const [isSubmitting, setIsSubmitting] = createSignal(false); // Reset form when modal opens/closes or learningPath changes const resetForm = () => { if (props.learningPath && props.isEdit) { setLearningPathData({ title: props.learningPath.title, description: props.learningPath.description, category: props.learningPath.category, difficulty: props.learningPath.difficulty, duration: props.learningPath.duration, thumbnail: props.learningPath.thumbnail || '', is_featured: props.learningPath.is_featured || false }); } else { setLearningPathData({ title: '', description: '', category: '', difficulty: 'beginner', duration: '', thumbnail: '', is_featured: false }); } }; // Reset form when modal opens/closes if (props.isOpen) { resetForm(); } const handleSubmit = async (e: Event) => { e.preventDefault(); if (!learningPathData().title.trim() || !learningPathData().description.trim()) { // Display inline error instead of alert return; } setIsSubmitting(true); try { await props.onSubmit(learningPathData()); props.onClose(); resetForm(); } catch (error) { console.error('Failed to save learning path:', error); // Let the parent handle the error display } finally { setIsSubmitting(false); } }; const handleInputChange = (field: keyof LearningPathFormData) => { return (e: Event) => { const target = e.currentTarget as HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement; if (target) { setLearningPathData(prev => ({ ...prev, [field]: target.type === 'checkbox' ? (target as HTMLInputElement).checked : target.value })); } }; }; if (!props.isOpen) return null; return (
{/* Header */}

{props.isEdit ? 'Edit Learning Path' : 'Create New Learning Path'}

{/* Form */}
{/* Title */}
{/* Description */}