/** * File: page.tsx * Created by: AI Assistant * Date: 2025-11-29 * Purpose: Create forum discussion page for kreatiVortex platform * Part of: kreatiVortex - Platform Pembelajaran Tari Online */ 'use client'; import React, { useState, useEffect } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { useFetch } from '@/hooks/useFetch'; import { uploadFile, isAllowedFileType, formatFileSize, UploadedFile } from '@/lib/upload'; interface ClassData { id: string; name: string; code: string; } export default function NewForumPage() { const router = useRouter(); const searchParams = useSearchParams(); const classId = searchParams.get('classId'); const [loading, setLoading] = useState(false); const [formData, setFormData] = useState({ title: '', content: '', classId: '', }); const [attachments, setAttachments] = useState([]); const { data: classes } = useFetch('/api/classes'); useEffect(() => { if (classId) { setFormData(prev => ({ ...prev, classId })); } }, [classId, classes ]); const handleFileUpload = async (e: React.ChangeEvent) => { const files = e.target.files; if (!files) return; const newAttachments: UploadedFile[] = []; for (let i = 0; i < files.length; i++) { const file = files[i]; if (!isAllowedFileType(file)) { alert(`File ${file.name} tidak diizinkan. Hanya PDF, Word, dan file gambar yang diperbolehkan.`); continue; } if (file.size > 10 * 1024 * 1024) { // 10MB limit alert(`File ${file.name} terlalu besar. Maksimal ukuran file adalah 10MB.`); continue; } try { const uploadedFile = await uploadFile(file, 'forum-attachments'); newAttachments.push(uploadedFile); } catch (error) { console.error('Error uploading file:', error); alert(`Gagal mengupload file ${file.name}`); } } setAttachments(prev => [...prev, ...newAttachments]); }; const removeAttachment = (index: number) => { setAttachments(prev => prev.filter((_, i) => i !== index)); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { const response = await fetch('/api/forums', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ title: formData.title, description: formData.content, type: formData.classId && formData.classId !== 'general' ? 'CLASS' : 'GENERAL', classId: formData.classId && formData.classId !== 'general' ? formData.classId : undefined, attachments: attachments, }), }); const result = await response.json(); if (result.success) { router.push(formData.classId && formData.classId !== 'general' ? `/dashboard/forum/${formData.classId}` : '/dashboard/forum/umum'); } else { console.error('Failed to create forum:', result.message); // You could show an error message to the user here } } catch (error) { console.error('Error creating forum:', error); // You could show an error message to the user here } finally { setLoading(false); } }; return (

Buat Diskusi Baru

Mulai percakapan dengan komunitas

setFormData({ ...formData, title: e.target.value })} required placeholder="Apa yang ingin Anda diskusikan?" className="bg-white/10 border-white/20 text-white placeholder:text-gray-400" />