import React, { useState, useEffect } from 'react'; import { TextareaControl } from "../ui/textarea-control"; import { Card, CardBody, CardHeader, CardDivider } from '../ui/wp-card'; import { Elevation } from '../ui/elevation'; import { InputControl } from '../ui/input-control'; import { Divider } from '../ui/divider'; import { DismissibleAlert } from '../ui/dismissible-alert'; import { Heading } from '../ui/heading'; import { Flex, FlexItem } from '../ui/flex'; import { Spacer } from '../ui/spacer'; import { TabPanel } from '../ui/tab-panel'; import { Controller, useFormContext, useFieldArray } from 'react-hook-form'; import { IValueSet, useAuthServiceContext, useValueSetServiceContext } from '../../services'; import { FhirAge, FhirAnnotation, FhirCode, FhirCodeableConcept, FhirPeriod, FhirRange, FhirReference, FhirValueSetSearch, TypeSelector } from '../../components'; import { PlusCircle, Trash2 } from 'lucide-react'; import { useFhirEncounters } from '../../hooks'; interface FhirAllergyIntoleranceProps { elevated?: boolean; disabled?: boolean; enableInlineActions?: boolean; } export const FhirAllergyIntolerance: React.FC = ({ elevated = true, disabled = false, enableInlineActions = true }) => { const { watch, control } = useFormContext(); const { fetchValueSet, valueSetUrls } = useValueSetServiceContext(); const { userRole, USER_ROLES } = useAuthServiceContext(); const isPatientRole = [USER_ROLES.PATIENT, USER_ROLES.RELATED_PERSON].includes(userRole as string); const [loading, setLoading] = useState(false); // value sets const [allergyIntoleranceClinical, setAllergyIntoleranceClinical] = useState([]); const [allergyIntoleranceVerification, setAllergyIntoleranceVerification] = useState([]); const [allergyIntoleranceType, setAllergyIntoleranceType] = useState([]); const [allergyIntoleranceCategory, setAllergyIntoleranceCategory] = useState([]); const [allergyIntoleranceCriticality, setAllergyIntoleranceCriticality] = useState([]); const [reactionEventSeverity, setReactionEventSeverity] = useState([]); const spacerMargin = '24px'; const { encountersList } = useFhirEncounters(); const reactionFieldArray = useFieldArray({ control, name: 'reaction' }); const onsetType = watch('onsetType'); useEffect(() => { const loadValueSets = async () => { setLoading(true); try { const [ allergyIntoleranceClinicalValueSet, allergyIntoleranceVerificationValueSet, allergyIntoleranceTypeValueSet, allergyIntoleranceCategoryValueSet, allergyIntoleranceCriticalityValueSet, reactionEventSeverityValueSet ] = await Promise.all([ fetchValueSet(valueSetUrls.allergyIntoleranceClinical), fetchValueSet(valueSetUrls.allergyIntoleranceVerification), fetchValueSet(valueSetUrls.allergyIntoleranceType), fetchValueSet(valueSetUrls.allergyIntoleranceCategory), fetchValueSet(valueSetUrls.allergyIntoleranceCriticality), fetchValueSet(valueSetUrls.reactionEventSeverity) ]); setAllergyIntoleranceClinical(allergyIntoleranceClinicalValueSet); setAllergyIntoleranceVerification(allergyIntoleranceVerificationValueSet); setAllergyIntoleranceType(allergyIntoleranceTypeValueSet); setAllergyIntoleranceCategory(allergyIntoleranceCategoryValueSet); setAllergyIntoleranceCriticality(allergyIntoleranceCriticalityValueSet); setReactionEventSeverity(reactionEventSeverityValueSet); } catch (err) { console.error('Error loading value sets:', err); } finally { setLoading(false); } }; loadValueSets(); }, [fetchValueSet, valueSetUrls]); const canAddReaction = () => { return true; }; const addReaction = () => { reactionFieldArray.append({ substance: null, manifestation: [], description: '', onset: '', severity: '', exposureRoute: null, note: [] }); }; const removeReaction = (index: number) => { reactionFieldArray.remove(index); }; return ( {elevated && } Details {/* code */} {/* clinicalStatus */} {/* verificationStatus */} {/* type */} {/* category */} {/* criticality */} {/* recordedDate */} ( )}/> {/* lastOccurrence */} ( )}/> {/* onsetType */} {onsetType === 'dateTime' && ( <> {/* onsetDateTime */} ( )}/> )} {onsetType === 'age' && ( <> {/* onsetAge */} )} {onsetType === 'period' && ( <> {/* onsetPeriod */} )} {onsetType === 'range' && ( <> {/* onsetRange */} )} {onsetType === 'string' && ( <> {/* onsetString */} ( )}/> )} {/* encounter */} {!isPatientRole && <> } {(tab) => { // reactions if (tab.name === 'reactions') { return (
{reactionFieldArray.fields.length === 0 && ( <> Reactions to this allergy or intolerance No reactions added yet. )} {reactionFieldArray.fields.map((reaction, index) => ( {reactionFieldArray.fields.length > 0 && (

Reaction {index + 1}

)} {/* substance */} {/* manifestation */} Manifestation {/* manifestation */} {/* description */} ( )}/> {/* onset */} ( )}/> {/* severity */} {/* exposureRoute */} {/* note */}
))} {reactionFieldArray.fields.length > 0 && ( )}
); } // notes if (tab.name === 'notes') { return (
{/* note */}
); } }}
); };