@@ -46,12 +46,16 @@ import { ProductKnowledgeBase } from "@/types/inventory/productKnowledge/product
4646interface SubstitutionSheetProps {
4747 open : boolean ;
4848 onOpenChange : ( open : boolean ) => void ;
49- originalProductKnowledge : ProductKnowledgeBase | undefined ;
49+ originalProductKnowledge ?: ProductKnowledgeBase ;
50+ /** Used when no original product knowledge exists (e.g., medication without linked product) */
51+ originalMedicationName ?: string ;
5052 currentSubstitution ?: {
5153 substitutedProductKnowledge ?: ProductKnowledgeBase ;
5254 type ?: SubstitutionType ;
5355 reason ?: SubstitutionReason ;
5456 } ;
57+ /** Pre-selected substitute product (optional) */
58+ preSelectedProduct ?: ProductKnowledgeBase ;
5559 onSave : (
5660 substitutionDetails ?: {
5761 substitutedProductKnowledge : ProductKnowledgeBase ;
@@ -76,37 +80,41 @@ export function SubstitutionSheet({
7680 open,
7781 onOpenChange,
7882 originalProductKnowledge,
83+ originalMedicationName,
7984 currentSubstitution,
85+ preSelectedProduct,
8086 onSave,
8187 facilityId : _facilityId ,
8288} : SubstitutionSheetProps ) {
8389 const { t } = useTranslation ( ) ;
8490 const [ selectedSubstitute , setSelectedSubstitute ] = useState <
8591 ProductKnowledgeBase | undefined
86- > ( currentSubstitution ?. substitutedProductKnowledge ) ;
92+ > ( currentSubstitution ?. substitutedProductKnowledge || preSelectedProduct ) ;
8793
8894 const form = useForm < SubstitutionFormValues > ( {
8995 resolver : zodResolver ( substitutionSchema ) ,
9096 defaultValues : {
9197 substitutedProductKnowledge :
92- currentSubstitution ?. substitutedProductKnowledge || undefined ,
98+ currentSubstitution ?. substitutedProductKnowledge ||
99+ preSelectedProduct ||
100+ undefined ,
93101 type : currentSubstitution ?. type || SubstitutionType . E ,
94102 reason : currentSubstitution ?. reason || SubstitutionReason . OS ,
95103 } ,
96104 } ) ;
97105
98106 useEffect ( ( ) => {
99107 if ( open ) {
108+ const initialProduct =
109+ currentSubstitution ?. substitutedProductKnowledge || preSelectedProduct ;
100110 form . reset ( {
101- substitutedProductKnowledge :
102- currentSubstitution ?. substitutedProductKnowledge || undefined ,
111+ substitutedProductKnowledge : initialProduct || undefined ,
103112 type : currentSubstitution ?. type || SubstitutionType . E ,
104113 reason : currentSubstitution ?. reason || SubstitutionReason . OS ,
105114 } ) ;
106- setSelectedSubstitute ( currentSubstitution ?. substitutedProductKnowledge ) ;
107- // No need to set search term anymore
115+ setSelectedSubstitute ( initialProduct ) ;
108116 }
109- } , [ open , currentSubstitution , form ] ) ;
117+ } , [ open , currentSubstitution , preSelectedProduct , form ] ) ;
110118
111119 useEffect ( ( ) => {
112120 form . setValue ( "substitutedProductKnowledge" , selectedSubstitute , {
@@ -130,7 +138,8 @@ export function SubstitutionSheet({
130138 setSelectedSubstitute ( product ) ;
131139 } ;
132140
133- if ( ! originalProductKnowledge ) return null ;
141+ const displayName =
142+ originalProductKnowledge ?. name || originalMedicationName || "" ;
134143
135144 const handleClearSubstitution = ( ) => {
136145 onSave ( null ) ; // Pass null to indicate clearing
@@ -148,9 +157,7 @@ export function SubstitutionSheet({
148157 < div className = "space-y-2" >
149158 < div className = "flex items-center gap-2" >
150159 < span > { t ( "substituting_for" ) } :</ span >
151- < Badge variant = "secondary" >
152- { originalProductKnowledge . name }
153- </ Badge >
160+ < Badge variant = "secondary" > { displayName } </ Badge >
154161 </ div >
155162 < p className = "text-sm text-muted-foreground" >
156163 { t ( "select_alternative_medication_and_provide_details" ) }
0 commit comments