@@ -52,7 +52,10 @@ import {
5252} from '../../form.schemas' ;
5353import handleIMS_APIError from '../../handleIMS_APIError' ;
5454import ManufacturerDialog from '../../manufacturer/manufacturerDialog.component' ;
55- import { sortDataList } from '../../utils' ;
55+ import {
56+ createFormControlWithRootErrorClearing ,
57+ sortDataList ,
58+ } from '../../utils' ;
5659
5760const RECENT_MANUFACTURER_CUTOFF_TIME = 10 * 60 * 1000 ;
5861
@@ -157,6 +160,12 @@ function convertToCatalogueItemDetailsStepPost(
157160 } ;
158161}
159162
163+ const formControlDetailsStep =
164+ createFormControlWithRootErrorClearing < CatalogueItemDetailsStep > ( ) ;
165+
166+ const formControlPropertiesStep =
167+ createFormControlWithRootErrorClearing < PropertiesStep > ( ) ;
168+
160169export interface CatalogueItemsDialogProps {
161170 open : boolean ;
162171 onClose : ( ) => void ;
@@ -181,8 +190,9 @@ function CatalogueItemsDialog(props: CatalogueItemsDialogProps) {
181190 [ parentInfo ]
182191 ) ;
183192
184- const CatalogueItemDetailsStepFormMethods = useForm < CatalogueItemDetailsStep > (
193+ const catalogueItemDetailsStepFormMethods = useForm < CatalogueItemDetailsStep > (
185194 {
195+ formControl : formControlDetailsStep ,
186196 resolver : zodResolver ( CatalogueItemDetailsStepSchema ( requestType ) ) ,
187197 defaultValues : toCatalogueItemDetailsStep ( selectedCatalogueItem ) ,
188198 }
@@ -195,10 +205,10 @@ function CatalogueItemsDialog(props: CatalogueItemsDialogProps) {
195205 control : controlDetailsStep ,
196206 clearErrors : clearErrorsDetailsStep ,
197207 reset : resetDetailsStep ,
198- watch : watchDetailsStep ,
199- } = CatalogueItemDetailsStepFormMethods ;
208+ } = catalogueItemDetailsStepFormMethods ;
200209
201210 const catalogueItemPropertiesStepFormMethods = useForm < PropertiesStep > ( {
211+ formControl : formControlPropertiesStep ,
202212 resolver : zodResolver ( PropertiesStepSchema ) ,
203213 defaultValues : {
204214 properties : convertToPropertyValueList (
@@ -215,7 +225,6 @@ function CatalogueItemsDialog(props: CatalogueItemsDialogProps) {
215225 control : controlPropertiesStep ,
216226 clearErrors : clearErrorsPropertiesStep ,
217227 reset : resetPropertiesStep ,
218- watch : watchPropertiesStep ,
219228 setError : setErrorPropertiesStep ,
220229 } = catalogueItemPropertiesStepFormMethods ;
221230
@@ -251,21 +260,6 @@ function CatalogueItemsDialog(props: CatalogueItemsDialogProps) {
251260 selectedCatalogueItem ?. properties ,
252261 ] ) ;
253262
254- // Clears form errors when a value has been changed
255- React . useEffect ( ( ) => {
256- const subscription1 = watchDetailsStep ( ( ) =>
257- clearErrorsPropertiesStep ( 'root.formError' )
258- ) ;
259- return ( ) => subscription1 . unsubscribe ( ) ;
260- } , [ clearErrorsPropertiesStep , watchDetailsStep ] ) ;
261-
262- React . useEffect ( ( ) => {
263- const subscription = watchPropertiesStep ( ( ) =>
264- clearErrorsPropertiesStep ( 'root.formError' )
265- ) ;
266- return ( ) => subscription . unsubscribe ( ) ;
267- } , [ clearErrorsPropertiesStep , watchPropertiesStep ] ) ;
268-
269263 const { mutateAsync : postCatalogueItem , isPending : isAddPending } =
270264 usePostCatalogueItem ( ) ;
271265 const { mutateAsync : patchCatalogueItem , isPending : isEditPending } =
@@ -387,12 +381,17 @@ function CatalogueItemsDialog(props: CatalogueItemsDialogProps) {
387381 if ( response && error . response ?. status === 409 ) {
388382 if ( response . detail . includes ( 'child elements' ) ) {
389383 // find the name of the manufacturer, so it can be used in the error message
390- const manufacturerName = manufacturerList ?. find (
391- ( manufacturer ) => manufacturer . id === selectedCatalogueItem ?. manufacturer_id
392- ) || null ;
384+ const manufacturerName =
385+ manufacturerList ?. find (
386+ ( manufacturer ) =>
387+ manufacturer . id ===
388+ selectedCatalogueItem ?. manufacturer_id
389+ ) || null ;
393390 // add the manufacturer name into the error message
394- const childElementsMessage = "Unable to update catalogue item properties and manufacturer ("
395- + manufacturerName ?. name + "), as the catalogue item has associated items." ;
391+ const childElementsMessage =
392+ 'Unable to update catalogue item properties and manufacturer (' +
393+ manufacturerName ?. name +
394+ '), as the catalogue item has associated items.' ;
396395 setErrorPropertiesStep ( 'root.formError' , {
397396 message : childElementsMessage ,
398397 } ) ;
@@ -504,10 +503,7 @@ function CatalogueItemsDialog(props: CatalogueItemsDialogProps) {
504503 const options = ( ) : Array < Manufacturer & { isRecent : string } > => {
505504 const classifiedManufacturers = manufacturerList
506505 ? manufacturerList . map ( ( option ) => {
507- return {
508- ...option ,
509- isRecent : 'A-Z' ,
510- } ;
506+ return { ...option , isRecent : 'A-Z' } ;
511507 } )
512508 : [ ] ;
513509
@@ -521,10 +517,7 @@ function CatalogueItemsDialog(props: CatalogueItemsDialogProps) {
521517 return isRecent ;
522518 } )
523519 . map ( ( option ) => {
524- return {
525- ...option ,
526- isRecent : 'Recently Added' ,
527- } ;
520+ return { ...option , isRecent : 'Recently Added' } ;
528521 } ) ;
529522
530523 /*returns them in reverse alphabetical order, since they will be sorted by "isRecent",
@@ -940,10 +933,8 @@ function CatalogueItemsDialog(props: CatalogueItemsDialogProps) {
940933 sx = { { marginTop : 2 } }
941934 >
942935 { STEPS . map ( ( label , index ) => {
943- const labelProps : {
944- optional ?: React . ReactNode ;
945- error ?: boolean ;
946- } = { } ;
936+ const labelProps : { optional ?: React . ReactNode ; error ?: boolean } =
937+ { } ;
947938
948939 if ( isStepFailed ( index ) ) {
949940 labelProps . optional = (
@@ -1002,11 +993,7 @@ function CatalogueItemsDialog(props: CatalogueItemsDialogProps) {
1002993 ) }
1003994 </ DialogActions >
1004995 < Box
1005- sx = { {
1006- width : '100%' ,
1007- justifyContent : 'center' ,
1008- alignItems : 'center' ,
1009- } }
996+ sx = { { width : '100%' , justifyContent : 'center' , alignItems : 'center' } }
1010997 >
1011998 { errorsPropertiesStep . root ?. formError && (
1012999 < FormHelperText sx = { { marginBottom : 2 , textAlign : 'center' } } error >
0 commit comments