import React, {useState} from 'react'; import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; import {faChevronDown, faChevronUp, faPlus, faTrash} from '@fortawesome/free-solid-svg-icons'; import {Incident, PlotPoint} from '@/lib/models/Book'; import {ActChapter, ChapterListProps} from '@/lib/models/Chapter'; import {SelectBoxProps} from '@/shared/interface'; import ActChapterItem from './ActChapter'; import InputField from '@/components/form/InputField'; import SelectBox from '@/components/form/SelectBox'; import {useTranslations} from 'next-intl'; interface ActPlotPointsProps { plotPoints: PlotPoint[]; incidents: Incident[]; actId: number; mainChapters: ChapterListProps[]; newPlotPointTitle: string; setNewPlotPointTitle: (title: string) => void; selectedIncidentId: string; setSelectedIncidentId: (id: string) => void; onAddPlotPoint: (actId: number) => Promise; onDeletePlotPoint: (actId: number, plotPointId: string) => Promise; onLinkChapter: (actId: number, chapterId: string, plotPointId: string) => Promise; onUpdateChapterSummary: (chapterId: string, summary: string, plotPointId: string) => void; onUnlinkChapter: (chapterInfoId: string, chapterId: string, plotPointId: string) => Promise; sectionKey: string; isExpanded: boolean; onToggleSection: (sectionKey: string) => void; } export default function ActPlotPoints({ plotPoints, incidents, actId, mainChapters, newPlotPointTitle, setNewPlotPointTitle, selectedIncidentId, setSelectedIncidentId, onAddPlotPoint, onDeletePlotPoint, onLinkChapter, onUpdateChapterSummary, onUnlinkChapter, sectionKey, isExpanded, onToggleSection, }: ActPlotPointsProps) { const t = useTranslations('actComponent'); const [expandedItems, setExpandedItems] = useState<{ [key: string]: boolean }>({}); const [selectedChapterId, setSelectedChapterId] = useState(''); function toggleItem(itemKey: string): void { setExpandedItems(prev => ({ ...prev, [itemKey]: !prev[itemKey], })); } function getIncidentData(): SelectBoxProps[] { return incidents.map((incident: Incident): SelectBoxProps => ({ value: incident.incidentId, label: incident.title, })); } return (
{isExpanded && (
{plotPoints && plotPoints.length > 0 ? ( plotPoints.map((item: PlotPoint) => { const itemKey = `plotpoint_${item.plotPointId}`; const isItemExpanded: boolean = expandedItems[itemKey]; const linkedIncident: Incident | undefined = incidents.find( (inc: Incident): boolean => inc.incidentId === item.linkedIncidentId ); return (
{isItemExpanded && (
{item.chapters && item.chapters.length > 0 ? ( item.chapters.map((chapter: ActChapter) => ( onUpdateChapterSummary(chapterId, summary, item.plotPointId) } onUnlink={(chapterInfoId, chapterId) => onUnlinkChapter(chapterInfoId, chapterId, item.plotPointId) } /> )) ) : (

{t('noLinkedChapter')}

)}
)}
); }) ) : (

{t('noPlotPointAdded')}

)}
setNewPlotPointTitle(e.target.value)} placeholder={t('newPlotPointPlaceholder')} />
setSelectedIncidentId(e.target.value)} data={getIncidentData()} /> } addButtonCallBack={(): Promise => onAddPlotPoint(actId)} isAddButtonDisabled={newPlotPointTitle.trim() === ''} />
)} ); }