'use client'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import SectionIcon from '@/components/SectionIcon'; import { profileSectionsService, ProfileSection, CreateSectionDto, UpdateSectionDto, getErrorMessage, } from '@/services'; type ModalType = 'create' | 'edit' | 'delete' | null; export default function ProfileSectionsPage() { const router = useRouter(); const [sections, setSections] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(''); const [showInactive, setShowInactive] = useState(true); // Modal state const [modalType, setModalType] = useState(null); const [selectedSection, setSelectedSection] = useState(null); const [isSubmitting, setIsSubmitting] = useState(false); const [modalError, setModalError] = useState(''); // Form state (slug is only editable in the edit modal) const [formData, setFormData] = useState({ name: '', slug: '', description: '', icon: '', isActive: true, isGlobal: false, isRepeatable: false, sortOrder: 0, }); useEffect(() => { fetchSections(); }, [showInactive]); const fetchSections = async () => { setIsLoading(true); setError(''); try { const data = await profileSectionsService.getAll(showInactive); setSections(data); } catch (err) { const errorMessage = getErrorMessage(err); setError(errorMessage); if (errorMessage.includes('Unauthorized')) { router.push('/login'); } } finally { setIsLoading(false); } }; const openCreateModal = () => { setFormData({ name: '', slug: '', description: '', icon: '', isActive: true, isGlobal: false, isRepeatable: false, sortOrder: 0, }); setModalError(''); setModalType('create'); }; const openEditModal = (section: ProfileSection) => { setSelectedSection(section); setFormData({ name: section.name, slug: section.slug, description: section.description || '', icon: section.icon || '', isActive: section.isActive, isGlobal: section.isGlobal, isRepeatable: section.isRepeatable || false, sortOrder: section.sortOrder, }); setModalError(''); setModalType('edit'); }; const openDeleteModal = (section: ProfileSection) => { setSelectedSection(section); setModalError(''); setModalType('delete'); }; const closeModal = () => { setModalType(null); setSelectedSection(null); setModalError(''); }; const handleCreate = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); setModalError(''); try { // Backend auto-generates slug on create; don't send the form slug field const { slug: _slug, ...createPayload } = formData; void _slug; await profileSectionsService.create(createPayload); closeModal(); fetchSections(); } catch (err) { setModalError(getErrorMessage(err)); } finally { setIsSubmitting(false); } }; const handleUpdate = async (e: React.FormEvent) => { e.preventDefault(); if (!selectedSection) return; setIsSubmitting(true); setModalError(''); try { await profileSectionsService.update(selectedSection.id, formData as UpdateSectionDto); closeModal(); fetchSections(); } catch (err) { setModalError(getErrorMessage(err)); } finally { setIsSubmitting(false); } }; const handleDelete = async () => { if (!selectedSection) return; setIsSubmitting(true); setModalError(''); try { await profileSectionsService.delete(selectedSection.id); closeModal(); fetchSections(); } catch (err) { setModalError(getErrorMessage(err)); } finally { setIsSubmitting(false); } }; const toggleStatus = async (section: ProfileSection) => { try { await profileSectionsService.update(section.id, { isActive: !section.isActive }); fetchSections(); } catch (err) { setError(getErrorMessage(err)); } }; return (
{/* Page Title */}

Profile Sections

Manage profile sections and their fields for agent types

{/* Sections Table */}
{error && (

{error}

)}
{isLoading ? (
) : sections.length === 0 ? (

No profile sections found

) : ( {sections.map((section) => ( ))}
Section Description Fields Agent Types Global Status Actions
{section.icon && ( )}
{section.name}
{section.slug}
{section.description || '-'}
{section._count?.fields || 0} fields
{section.isGlobal ? ( All (Global) ) : ( `${section._count?.agentTypeSections || 0} assigned` )}
{section.isSystem && ( System )} {section.isGlobal ? 'Global' : 'Specific'} {section.isRepeatable && ( Repeatable )}
Manage {section.isSystem ? ( Delete ) : ( )}
)}
{/* Create/Edit Modal */} {(modalType === 'create' || modalType === 'edit') && (

{modalType === 'create' ? 'Create Profile Section' : 'Edit Profile Section'}

{modalError && (

{modalError}

)}
setFormData({ ...formData, name: e.target.value })} className="w-full px-3 py-2 border border-[#e5e7eb] rounded-xl focus:outline-none focus:border-[#f5a623] text-[#00293d] bg-white placeholder:text-[#666666]" placeholder="e.g., Location, Experience, Education" required />
{modalType === 'edit' && (
setFormData({ ...formData, slug: e.target.value })} className="w-full px-3 py-2 border border-[#e5e7eb] rounded-xl focus:outline-none focus:border-[#f5a623] text-[#00293d] bg-white placeholder:text-[#666666] font-mono text-sm" placeholder="e.g., basic-information" pattern="^[a-z0-9]+(?:-[a-z0-9]+)*$" title="Lowercase letters, digits, and hyphens only (no leading/trailing/consecutive hyphens)" required />

Lowercase letters, digits, and hyphens only. Used in API keys and field references — change with care.

)}