import React, { useState, useEffect } from 'react' import { Popconfirm, message } from 'antd' import { useAdminPanel } from '../AdminPanelContext' import { DefaultModal } from '../../Components/Modal/DefaultModal' import "../Styles/HeroSectionForm.scss" const HeroSectionForm = () => { const { sectionData, updateSectionData } = useAdminPanel() const heroData = sectionData.HeroSectionForm || [] const [isModalOpen, setIsModalOpen] = useState(false) const [title, setTitle] = useState('') const [description, setDescription] = useState('') const [subsections, setSubsections] = useState([{ id: 1, image: null, previewUrl: null, heading: '', subheading: '' }]) const [editingIndex, setEditingIndex] = useState(-1) const [titleError, setTitleError] = useState('') const [descriptionError, setDescriptionError] = useState('') // UUID generator fallback for browser compatibility const generateUUID = () => { if (typeof crypto !== 'undefined' && crypto.randomUUID) { return crypto.randomUUID() } return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { const r = Math.random() * 16 | 0 const v = c === 'x' ? r : (r & 0x3 | 0x8) return v.toString(16) }) } useEffect(() => { return () => { subsections.forEach(s => { if (s.previewUrl) URL.revokeObjectURL(s.previewUrl) }) } }, []) const openModal = () => { setIsModalOpen(true) } const closeModal = () => { setIsModalOpen(false) setTitle('') setDescription('') setSubsections([{ id: 1, image: null, previewUrl: null, heading: '', subheading: '' }]) setEditingIndex(-1) setTitleError('') setDescriptionError('') } const handleClear = () => { setTitle('') setDescription('') setSubsections([{ id: 1, image: null, previewUrl: null, heading: '', subheading: '' }]) setTitleError('') setDescriptionError('') } const addSubsection = () => { if (subsections.length >= 3) { message.warning('Only 3 subsections allowed') return } const newId = Math.max(...subsections.map(s => s.id)) + 1 setSubsections([...subsections, { id: newId, image: null, previewUrl: null, heading: '', subheading: '' }]) } const removeSubsection = (id) => { if (subsections.length === 1) { message.warning('At least one subsection is required') return } const toRemove = subsections.find(s => s.id === id) if (toRemove?.previewUrl) { URL.revokeObjectURL(toRemove.previewUrl) } setSubsections(subsections.filter(s => s.id !== id)) } const handleImageUpload = (id, file) => { setSubsections(subsections.map(s => { if (s.id !== id) return s if (s.previewUrl) { URL.revokeObjectURL(s.previewUrl) } const nextUrl = file ? URL.createObjectURL(file) : null return { ...s, image: file || null, previewUrl: nextUrl } })) } const handleSubsectionChange = (id, field, value) => { setSubsections(subsections.map(s => s.id === id ? { ...s, [field]: value } : s )) } const handleSave = () => { let hasError = false if (!title.trim()) { setTitleError('Title is required') hasError = true } else { setTitleError('') } if (!description.trim()) { setDescriptionError('Description is required') hasError = true } else { setDescriptionError('') } if (hasError) { message.error('Please fix the highlighted fields') return } const newHero = { id: editingIndex >= 0 ? heroData[editingIndex].id : generateUUID(), title: title.trim(), description: description.trim(), subsections: subsections.map(s => ({ id: s.id, heading: s.heading, subheading: s.subheading, displayUrl: s.previewUrl || (s.image ? URL.createObjectURL(s.image) : null), })), active: true, createdAt: editingIndex >= 0 ? heroData[editingIndex].createdAt : new Date().toLocaleDateString() } if (editingIndex >= 0) { const updatedHeros = heroData.map((hero, index) => index === editingIndex ? newHero : hero) updateSectionData('HeroSectionForm', updatedHeros) setEditingIndex(-1) message.success('Hero section updated!') } else { updateSectionData('HeroSectionForm', [...heroData, newHero]) message.success('Hero section added!') } closeModal() } const handleDelete = (index) => { const updatedHeros = heroData.filter((_, i) => i !== index) updateSectionData('HeroSectionForm', updatedHeros) message.success('Hero section deleted') } const handleEdit = (index) => { setTitle(heroData[index].title) setDescription(heroData[index].description) if (heroData[index].subsections && heroData[index].subsections.length > 0) { setSubsections(heroData[index].subsections.map(ss => ({ id: ss.id, image: null, previewUrl: ss.displayUrl || null, heading: ss.heading || '', subheading: ss.subheading || '' }))) } setEditingIndex(index) openModal() } const handleToggleActive = (index) => { const updated = [...heroData] updated[index].active = !updated[index].active updateSectionData('HeroSectionForm', updated) message.success('Status updated') } return (
Manage and organize your hero section content and settings.
Description: {hero.description || 'No description'}
Created: {hero.date || hero.createdAt}
Status: {hero.active ? 'Active' : 'Inactive'}
{hero.subsections && hero.subsections.length > 0 && (Get started by creating your first hero section
Add up to 3 subsections with images and content