Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat)O3-4172: Adding functionality to change concept mapping ID #401

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback, useMemo, useState } from 'react';
import { FormLabel, InlineNotification, FormGroup, Stack } from '@carbon/react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { FormLabel, InlineNotification, FormGroup, Stack, Checkbox } from '@carbon/react';
import { useTranslation } from 'react-i18next';
import ConceptSearch from '../../../common/concept-search/concept-search.component';
import { useFormField } from '../../../../form-field-context';
Expand All @@ -9,6 +9,13 @@ import styles from './obs-type-question.scss';
const ObsTypeQuestion: React.FC = () => {
const { t } = useTranslation();
const { formField, setFormField, concept, setConcept } = useFormField();
const [selectedMapping, setSelectedMapping] = useState<string | null>(null);

useEffect(() => {
if (formField.questionOptions?.concept) {
setSelectedMapping(formField.questionOptions.concept);
}
}, [formField.questionOptions?.concept]);

const getDatePickerType = useCallback((concept: Concept): DatePickerType | null => {
const conceptDataType = concept.datatype.name;
Expand Down Expand Up @@ -37,13 +44,15 @@ const ObsTypeQuestion: React.FC = () => {
},
...(datePickerType && { datePickerFormat: datePickerType }),
}));
setSelectedMapping(null);
}
},
[getDatePickerType, setFormField, setConcept],
);

const clearSelectedConcept = useCallback(() => {
setConcept(null);
setSelectedMapping(null);
setFormField((prevFormField) => {
const updatedFormField = { ...prevFormField };
if (updatedFormField.questionOptions) {
Expand All @@ -57,6 +66,34 @@ const ObsTypeQuestion: React.FC = () => {
});
}, [setFormField, setConcept]);

const handleCheckboxChange = useCallback(
(mapping: ConceptMapping) => {
const newConceptValue = `${mapping.type}:${mapping.value}`;
setSelectedMapping(newConceptValue);
setFormField((prevField) => ({
...prevField,
questionOptions: {
...prevField.questionOptions,
concept: newConceptValue,
},
}));
},
[setFormField],
);

const handleUncheckAll = useCallback(() => {
setSelectedMapping(null);
if (concept) {
setFormField((prevField) => ({
...prevField,
questionOptions: {
...prevField.questionOptions,
concept: concept.uuid,
},
}));
}
}, [concept, setFormField]);

const conceptMappings: ConceptMapping[] = useMemo(() => {
if (concept && concept.mappings) {
return concept.mappings.map((conceptMapping) => {
Expand Down Expand Up @@ -97,6 +134,7 @@ const ObsTypeQuestion: React.FC = () => {
<th>{t('relationship', 'Relationship')}</th>
<th>{t('source', 'Source')}</th>
<th>{t('code', 'Code')}</th>
<th>{t('select', 'Select')}</th>
</tr>
</thead>
<tbody>
Expand All @@ -105,6 +143,21 @@ const ObsTypeQuestion: React.FC = () => {
<td>{mapping.relationship}</td>
<td>{mapping.type}</td>
<td>{mapping.value}</td>
<td>
{mapping.relationship === 'SAME-AS' && (
<Checkbox
id={`checkbox-${index}`}
checked={selectedMapping === `${mapping.type}:${mapping.value}`}
onChange={() => {
if (selectedMapping === `${mapping.type}:${mapping.value}`) {
handleUncheckAll();
} else {
handleCheckboxChange(mapping);
}
}}
/>
)}
</td>
</tr>
))}
</tbody>
Expand All @@ -114,5 +167,4 @@ const ObsTypeQuestion: React.FC = () => {
</Stack>
);
};

export default ObsTypeQuestion;