import React, { useCallback, useState } from 'react'; import classNames from 'classnames'; import { SortableContext, useSortable } from '@dnd-kit/sortable'; import { useTranslation } from 'react-i18next'; import { IconButton } from '@carbon/react'; import { Draggable, Edit, TrashCan, Copy } from '@carbon/react/icons'; import { showModal, ChevronDownIcon, ChevronUpIcon } from '@openmrs/esm-framework'; import MarkdownWrapper from '../markdown-wrapper/markdown-wrapper'; import type { FormField } from '@openmrs/esm-form-engine-lib'; import type { Schema } from '@types'; import styles from './draggable-question.scss'; interface DraggableQuestionProps { handleDuplicateQuestion: (question: FormField, pageId: number, sectionId: number, questionId?: number) => void; onSchemaChange: (schema: Schema) => void; pageIndex: number; question: FormField; questionCount: number; questionIndex: number; schema: Schema; sectionIndex: number; subQuestionIndex?: number; children?: React.ReactNode; } const DraggableQuestion: React.FC = ({ handleDuplicateQuestion, onSchemaChange, pageIndex, question, questionCount, questionIndex, schema, sectionIndex, subQuestionIndex, children, }) => { const { t } = useTranslation(); const defaultEnterDelayInMs = 300; const [isCollapsed, setIsCollapsed] = useState(true); const toggleCollapse = () => { if (question.questions) { setIsCollapsed(!isCollapsed); } }; const launchEditQuestionModal = useCallback(() => { const dispose = showModal('question-modal', { formField: question, closeModal: () => dispose(), onSchemaChange, schema, questionIndex, pageIndex, sectionIndex, subQuestionIndex, }); }, [onSchemaChange, pageIndex, question, questionIndex, schema, sectionIndex, subQuestionIndex]); const launchDeleteQuestionModal = useCallback(() => { const dispose = showModal('delete-question-modal', { closeModal: () => dispose(), pageIndex, sectionIndex, question, questionIndex, subQuestionIndex, onSchemaChange, schema, }); }, [onSchemaChange, pageIndex, question, questionIndex, schema, sectionIndex, subQuestionIndex]); const { attributes, listeners, setNodeRef, active, isDragging, over, isOver } = useSortable({ id: question.id, data: { type: subQuestionIndex === null || subQuestionIndex === undefined ? 'question' : 'obsQuestion', question: { handleDuplicateQuestion: handleDuplicateQuestion, onSchemaChange: onSchemaChange, pageIndex: pageIndex, sectionIndex: sectionIndex, question: question, questionCount: questionCount, questionIndex: questionIndex, subQuestionIndex: subQuestionIndex !== null ? subQuestionIndex : null, schema: schema, }, }, disabled: questionCount <= 1 && (subQuestionIndex === undefined || subQuestionIndex === null), }); const handleDuplicate = useCallback(() => { if (!isDragging) { if (subQuestionIndex !== undefined && subQuestionIndex !== null) { handleDuplicateQuestion(question, pageIndex, sectionIndex, questionIndex); } else { handleDuplicateQuestion(question, pageIndex, sectionIndex); } } }, [handleDuplicateQuestion, isDragging, question, pageIndex, sectionIndex, questionIndex, subQuestionIndex]); return (
0, })} onClick={toggleCollapse} >

{question.questionOptions.rendering === 'markdown' ? ( ) : ( question.label )}

{question?.questions && question?.questions.length > 0 && ( {isCollapsed ? ( ) : ( )} )}
{question?.questions && (
qn.id)}>{children}
)}
); }; export default DraggableQuestion;