import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Button, Form, FormGroup, ModalBody, ModalFooter, ModalHeader, TextInput, Checkbox } from '@carbon/react'; import { showSnackbar } from '@openmrs/esm-framework'; import type { Schema } from '@types'; interface SectionModalProps { closeModal: () => void; schema: Schema; onSchemaChange: (schema: Schema) => void; pageIndex: number; sectionIndex?: number; modalType?: 'edit'; } const SectionModal: React.FC = ({ closeModal, schema, onSchemaChange, pageIndex, sectionIndex, modalType, }) => { const { t } = useTranslation(); const [sectionTitle, setSectionTitle] = useState( modalType === 'edit' ? schema.pages[pageIndex].sections[sectionIndex].label : '', ); const [isExpanded, setIsExpanded] = useState( modalType === 'edit' ? schema.pages[pageIndex].sections[sectionIndex].isExpanded : 'true', ); const handleUpdatePageSections = () => { updateSections(); closeModal(); }; const handleCheckboxChange = (event: React.ChangeEvent, { checked }: { checked: boolean }) => { setIsExpanded(checked === true ? 'true' : 'false'); }; const updateSections = () => { try { if (modalType === 'edit') { schema.pages[pageIndex].sections[sectionIndex].label = sectionTitle; schema.pages[pageIndex].sections[sectionIndex].isExpanded = isExpanded; } else { schema.pages[pageIndex]?.sections?.push({ label: sectionTitle, isExpanded: isExpanded, questions: [], }); } onSchemaChange({ ...schema }); setSectionTitle(''); showSnackbar({ title: t('success', 'Success!'), kind: 'success', isLowContrast: true, subtitle: modalType === 'edit' ? t('sectionEdited', 'Section edited') : t('sectionCreated', 'New section created'), }); } catch (error) { if (error instanceof Error) { showSnackbar({ title: modalType === 'edit' ? t('errorCreatingSection', 'Error creating section') : t('errorEditingSection', 'Error editing section'), kind: 'error', subtitle: error?.message, }); } } }; return ( <>
event.preventDefault()}> ) => setSectionTitle(event.target.value)} style={{ marginBottom: '16px' }} />
); }; export default SectionModal;