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 (
{/* Header Section */}
{/*
🌱
*/}

Offerings Management

Manage and organize your ecosystem content and settings.

{/* Tab Navigation */}
{/* Content Area */} {ecosystemData.length > 0 ? (
{ecosystemData.map((ecosystem, index) => (

{ecosystem.title}

handleDelete(index)} okText="Yes" cancelText="No" >
{ecosystem.posters && ecosystem.posters.length > 0 ? (
Posters ({ecosystem.posters.length}): {ecosystem.posters.map(poster => (
{poster.image && (
{poster.title}
)}

{poster.title}

{poster.description &&

{poster.description}

}
))}
) : ( // Legacy data structure support <> {ecosystem.posterImage && (
{ecosystem.posterTitle}
)}

Poster Title: {ecosystem.posterTitle}

Description: {ecosystem.posterDescription || 'No description'}

)}

Created: {ecosystem.date || ecosystem.createdAt}

Status: {ecosystem.active ? 'Active' : 'Inactive'}

))}
) : (
🌱

No Ecosystems Found

Get started by creating your first ecosystem

)} {/* Modal */} = 0 ? 'Edit Ecosystem' : 'Create New Ecosystem'} handleCancel={closeModal} handleSubmit={handleSave} buttonText={editingIndex >= 0 ? 'Update Ecosystem' : 'Add Ecosystem'} width={600} destroyOnClose={true} >
{ setTitle(e.target.value) if (titleError) setTitleError('') }} placeholder='Enter ecosystem title' className={titleError ? 'error' : ''} autoFocus /> {titleError && {titleError}}

Poster Sections

Add multiple poster sections for your ecosystem

{posters.map((poster, idx) => (
Poster {idx + 1} {posters.length > 1 && ( )}
handlePosterChange(poster.id, 'title', e.target.value)} placeholder='Enter poster title' className={posterErrors[poster.id] ? 'error' : ''} /> {posterErrors[poster.id] && {posterErrors[poster.id]}}