@@ -26,19 +26,19 @@ import {
2626 wbsPipe ,
2727 ReimbursementProductFormArgs ,
2828 IndexCode ,
29- CreateRefundSourceArgs
29+ CreateRefundSourceArgs ,
30+ Material
3031} from 'shared' ;
3132import { RemoveCircleOutline , AddCircleOutline } from '@mui/icons-material' ;
3233import { Control , Controller , FieldErrors , UseFormRegister , UseFormSetValue , UseFormWatch } from 'react-hook-form' ;
3334import { ReimbursementRequestFormInput } from './ReimbursementRequestForm' ;
3435import { useTheme } from '@mui/system' ;
35- import { useEffect , useState , useRef } from 'react' ;
36+ import { useEffect , useState , useRef , useMemo } from 'react' ;
3637import { useGetAllOtherProductReason } from '../../../hooks/finance.hooks' ;
3738import { useGetMaterialsForWbsElement } from '../../../hooks/bom.hooks' ;
3839import LoadingIndicator from '../../../components/LoadingIndicator' ;
3940import ErrorPage from '../../ErrorPage' ;
4041import { formatReasonName } from '../../../utils/reimbursement-request.utils' ;
41- import { Material } from 'shared' ;
4242
4343interface ReimbursementProductTableProps {
4444 reimbursementProducts : ReimbursementProductFormArgs [ ] ;
@@ -66,42 +66,59 @@ const ListItem = styled('li')(({ theme }) => ({
6666
6767const MaterialAutocomplete : React . FC < {
6868 wbsNum : WbsNumber ;
69- onSelect : ( material : Material ) => void ;
69+ onSelect : ( material : Material | null ) => void ;
7070 initialValue ?: string ;
7171} > = ( { wbsNum, onSelect, initialValue } ) => {
7272 const { data : materials , isLoading, isError, error } = useGetMaterialsForWbsElement ( wbsNum ) ;
73- const [ userSelected , setUserSelected ] = useState < { id : string ; label : string } | null > ( null ) ;
73+ const [ materialSelected , setMaterialSelected ] = useState < { id : string ; label : string } | null > ( null ) ;
74+
75+ const materialOptions = useMemo (
76+ ( ) =>
77+ ( materials ?? [ ] ) . map ( ( material ) => ( {
78+ id : material . materialId ,
79+ label : `${ material . name } (${ material . materialTypeName } ): ${ material . manufacturerName ?? 'N/A' } , ${ material . manufacturerPartNumber ?? 'N/A' } `
80+ } ) ) ,
81+ [ materials ]
82+ ) ;
83+
84+ useEffect ( ( ) => {
85+ if ( materials && materialSelected === null && initialValue ) {
86+ const match = materialOptions . find ( ( o ) => o . label === initialValue ) ;
87+ if ( match ) setMaterialSelected ( match ) ;
88+ }
89+ // eslint-disable-next-line react-hooks/exhaustive-deps
90+ } , [ materials ] ) ;
7491
75- if ( isLoading ) {
92+ if ( isLoading || ! materials ) {
7693 return < LoadingIndicator /> ;
7794 }
7895
7996 if ( isError ) {
80- return < ErrorPage message = { error ?. message || 'Failed to load materials' } /> ;
97+ return (
98+ < TextField
99+ variant = "outlined"
100+ placeholder = "Select Material"
101+ fullWidth
102+ size = "small"
103+ error
104+ disabled
105+ helperText = { error ?. message || 'Failed to load materials' }
106+ />
107+ ) ;
81108 }
82109
83- const materialOptions = ( materials || [ ] ) . map ( ( material ) => ( {
84- id : material . materialId ,
85- label : `${ material . name } : ${ material . manufacturerName } , ${ material . manufacturerPartNumber } `
86- } ) ) ;
87-
88- const selectedOption = userSelected ?? materialOptions . find ( ( o ) => o . label === initialValue ) ?? null ;
89-
90110 return (
91111 < Autocomplete
92112 sx = { { flex : 1 } }
93113 options = { materialOptions }
94114 getOptionLabel = { ( option ) => option . label }
115+ isOptionEqualToValue = { ( option , value ) => option . id === value . id }
95116 onChange = { ( _ , value ) => {
96- setUserSelected ( value ) ;
97- if ( value ) {
98- const selectedMaterial = materials ?. find ( ( m ) => m . materialId === value . id ) ;
99- if ( selectedMaterial ) {
100- onSelect ( selectedMaterial ) ;
101- }
102- }
117+ setMaterialSelected ( value ) ;
118+ const selectedMaterial = value ? materials . find ( ( m ) => m . materialId === value . id ) ?? null : null ;
119+ onSelect ( selectedMaterial ) ;
103120 } }
104- value = { selectedOption }
121+ value = { materialSelected }
105122 blurOnSelect = { true }
106123 size = { 'small' }
107124 renderInput = { ( params ) => < TextField { ...params } variant = "outlined" placeholder = "Select Material" fullWidth /> }
@@ -491,8 +508,18 @@ const ReimbursementProductTable: React.FC<ReimbursementProductTableProps> = ({
491508 wbsNum = { product . reason as WbsNumber }
492509 initialValue = { currentName }
493510 onSelect = { ( material ) => {
494- const label = `${ material . name } : ${ material . manufacturerName } , ${ material . manufacturerPartNumber } ` ;
495- setValue ( `reimbursementProducts.${ product . index } .name` , label ) ;
511+ if ( material ) {
512+ const label = `${ material . name } (${ material . materialTypeName } ): ${ material . manufacturerName ?? 'N/A' } , ${ material . manufacturerPartNumber ?? 'N/A' } ` ;
513+ setValue ( `reimbursementProducts.${ product . index } .name` , label , {
514+ shouldValidate : true ,
515+ shouldDirty : true
516+ } ) ;
517+ } else {
518+ setValue ( `reimbursementProducts.${ product . index } .name` , '' , {
519+ shouldValidate : true ,
520+ shouldDirty : true
521+ } ) ;
522+ }
496523 } }
497524 />
498525 < FormHelperText error >
0 commit comments