import React, { useState } from 'react' import { Popconfirm } from 'antd' import { useAdminPanel } from '../AdminPanelContext' import { DefaultModal } from '../../Components/Modal/DefaultModal' import "../Styles/OfferingsForm.scss" const OfferingsForm = () => { const { sectionData, updateSectionData } = useAdminPanel() const ecosystemData = sectionData.EcosystemForm || [] const [isModalOpen, setIsModalOpen] = useState(false) const [title, setTitle] = useState('') const [posters, setPosters] = useState([{ id: 1, title: '', description: '', image: '' }]) const [editingIndex, setEditingIndex] = useState(-1) const [titleError, setTitleError] = useState('') const [posterErrors, setPosterErrors] = useState({}) const openModal = () => { setIsModalOpen(true) } const closeModal = () => { setIsModalOpen(false) setTitle('') setPosters([{ id: 1, title: '', description: '', image: '' }]) setEditingIndex(-1) setTitleError('') setPosterErrors({}) } const handleClear = () => { setTitle('') setPosters([{ id: 1, title: '', description: '', image: '' }]) setTitleError('') setPosterErrors({}) } const addPoster = () => { const newId = Math.max(...posters.map(p => p.id)) + 1 setPosters([...posters, { id: newId, title: '', description: '', image: '' }]) } const removePoster = (id) => { if (posters.length > 1) { setPosters(posters.filter(p => p.id !== id)) const newErrors = { ...posterErrors } delete newErrors[id] setPosterErrors(newErrors) } } const handlePosterChange = (id, field, value) => { setPosters(posters.map(p => p.id === id ? { ...p, [field]: value } : p)) if (posterErrors[id]) { const newErrors = { ...posterErrors } delete newErrors[id] setPosterErrors(newErrors) } } const handleImageChange = (id, e) => { const file = e.target.files[0] if (file) { const reader = new FileReader() reader.onloadend = () => { handlePosterChange(id, 'image', reader.result) } reader.readAsDataURL(file) } } const handleSave = () => { let hasError = false const errors = {} if (!title.trim()) { setTitleError('Title is required') hasError = true } else { setTitleError('') } posters.forEach(poster => { if (!poster.title.trim()) { errors[poster.id] = 'Poster title is required' hasError = true } }) setPosterErrors(errors) if (hasError) return const newEcosystem = { id: editingIndex >= 0 ? ecosystemData[editingIndex].id : Date.now(), title: title.trim(), posters: posters.map(p => ({ id: p.id, title: p.title.trim(), description: p.description.trim(), image: p.image })), active: true, createdAt: editingIndex >= 0 ? ecosystemData[editingIndex].createdAt : new Date().toLocaleDateString() } if (editingIndex >= 0) { const updatedEcosystems = ecosystemData.map((ecosystem, index) => index === editingIndex ? newEcosystem : ecosystem) updateSectionData('EcosystemForm', updatedEcosystems) setEditingIndex(-1) } else { updateSectionData('EcosystemForm', [...ecosystemData, newEcosystem]) } closeModal() } const handleDelete = (index) => { const updatedEcosystems = ecosystemData.filter((_, i) => i !== index) updateSectionData('EcosystemForm', updatedEcosystems) } const handleEdit = (index) => { setTitle(ecosystemData[index].title) if (ecosystemData[index].posters && ecosystemData[index].posters.length > 0) { setPosters(ecosystemData[index].posters) } else { // Handle legacy data structure setPosters([{ id: 1, title: ecosystemData[index].posterTitle || '', description: ecosystemData[index].posterDescription || '', image: ecosystemData[index].posterImage || '' }]) } setEditingIndex(index) openModal() } const handleToggleActive = (index) => { const updated = [...ecosystemData] updated[index].active = !updated[index].active updateSectionData('EcosystemForm', updated) } return (
Manage and organize your ecosystem content and settings.
{poster.title}
{poster.description &&{poster.description}
}Poster Title: {ecosystem.posterTitle}
Description: {ecosystem.posterDescription || 'No description'}
> )}Created: {ecosystem.date || ecosystem.createdAt}
Status: {ecosystem.active ? 'Active' : 'Inactive'}
Get started by creating your first ecosystem
Add multiple poster sections for your ecosystem