@@ -19,7 +19,7 @@ import { DatePicker } from '@mui/x-date-pickers';
1919import { useToast } from '../../hooks/toasts.hooks' ;
2020import { useState } from 'react' ;
2121import { meetingStartTimePipe , wbsNamePipe } from '../../utils/pipes' ;
22- import { TeamType , WbsNumber , WorkPackage , validateWBS , wbsNumComparator , wbsPipe } from 'shared' ;
22+ import { TeamType , WorkPackage , validateWBS , wbsNumComparator , wbsPipe } from 'shared' ;
2323import { useCreateDesignReviews } from '../../hooks/design-reviews.hooks' ;
2424import { useAllUsers } from '../../hooks/users.hooks' ;
2525import ErrorPage from '../ErrorPage' ;
@@ -36,7 +36,8 @@ const schema = yup.object().shape({
3636 endTime : yup
3737 . number ( )
3838 . moreThan ( yup . ref ( 'startTime' ) , `End time must be after the start time` )
39- . required ( 'End time is required' )
39+ . required ( 'End time is required' ) ,
40+ wbsNum : yup . string ( ) . required ( 'Work Package is required' )
4041} ) ;
4142
4243interface CreateDesignReviewFormInput {
@@ -46,7 +47,7 @@ interface CreateDesignReviewFormInput {
4647 teamTypeId : string ;
4748 requiredMemberIds : number [ ] ;
4849 optionalMemberIds : number [ ] ;
49- wbsNum : WbsNumber ;
50+ wbsNum : string ;
5051}
5152
5253interface DesignReviewCreateModalProps {
@@ -69,17 +70,15 @@ export const DesignReviewCreateModal: React.FC<DesignReviewCreateModalProps> = (
6970 const [ datePickerOpen , setDatePickerOpen ] = useState ( false ) ;
7071 const [ requiredMembers , setRequiredMembers ] = useState ( [ ] . map ( userToAutocompleteOption ) ) ;
7172 const [ optionalMembers , setOptionalMembers ] = useState ( [ ] . map ( userToAutocompleteOption ) ) ;
72- const [ wbsNum , setWbsNum ] = useState ( query . get ( 'wbsNum' ) || '' ) ;
7373 const { isLoading : allUsersIsLoading , isError : allUsersIsError , error : allUsersError , data : users } = useAllUsers ( ) ;
7474 const {
7575 isLoading : allWorkPackagesIsLoading ,
7676 isError : allWorkPackagesIsError ,
7777 error : allWorkPackagesError ,
7878 data : allWorkPackages
7979 } = useAllWorkPackages ( ) ;
80- const [ teamError , setTeamError ] = useState ( true ) ;
8180
82- const { mutateAsync } = useCreateDesignReviews ( ) ;
81+ const { mutateAsync, isLoading } = useCreateDesignReviews ( ) ;
8382
8483 const onSubmit = async ( data : CreateDesignReviewFormInput ) => {
8584 const day = data . date . getDay ( ) ;
@@ -93,7 +92,7 @@ export const DesignReviewCreateModal: React.FC<DesignReviewCreateModalProps> = (
9392 teamTypeId : data . teamTypeId ,
9493 requiredMemberIds : requiredMembers . map ( ( member ) => parseInt ( member . id ) ) ,
9594 optionalMemberIds : optionalMembers . map ( ( member ) => parseInt ( member . id ) ) ,
96- wbsNum : validateWBS ( wbsNum ) ,
95+ wbsNum : validateWBS ( data . wbsNum ) ,
9796 meetingTimes : times
9897 } ) ;
9998 } catch ( error : unknown ) {
@@ -108,20 +107,22 @@ export const DesignReviewCreateModal: React.FC<DesignReviewCreateModalProps> = (
108107 handleSubmit,
109108 control,
110109 reset,
110+ setValue,
111111 formState : { errors }
112112 } = useForm ( {
113113 resolver : yupResolver ( schema ) ,
114114 defaultValues : {
115115 date : defaultDate ,
116116 startTime : 0 ,
117117 endTime : 1 ,
118- teamTypeId : ''
118+ teamTypeId : '' ,
119+ wbsNum : query . get ( 'wbsNum' ) || ''
119120 }
120121 } ) ;
121122
122123 if ( allUsersIsError ) return < ErrorPage error = { allUsersError } message = { allUsersError ?. message } /> ;
123124 if ( allWorkPackagesIsError ) return < ErrorPage error = { allWorkPackagesError } message = { allWorkPackagesError ?. message } /> ;
124- if ( allUsersIsLoading || ! users || allWorkPackagesIsLoading || ! allWorkPackages ) return < LoadingIndicator /> ;
125+ if ( allUsersIsLoading || ! users || allWorkPackagesIsLoading || ! allWorkPackages || isLoading ) return < LoadingIndicator /> ;
125126
126127 const memberOptions = users . map ( userToAutocompleteOption ) ;
127128
@@ -136,17 +137,6 @@ export const DesignReviewCreateModal: React.FC<DesignReviewCreateModalProps> = (
136137
137138 wbsDropdownOptions . sort ( ( wp1 , wp2 ) => wbsNumComparator ( wp1 . id , wp2 . id ) ) ;
138139
139- const wbsAutocompleteOnChange = (
140- _event : React . SyntheticEvent < Element , Event > ,
141- value : { label : string ; id : string } | null
142- ) => {
143- if ( value ) {
144- setWbsNum ( value . id ) ;
145- } else {
146- setWbsNum ( '' ) ;
147- }
148- } ;
149-
150140 return (
151141 < NERFormModal
152142 open = { showModal }
@@ -190,7 +180,6 @@ export const DesignReviewCreateModal: React.FC<DesignReviewCreateModalProps> = (
190180 />
191181 < FormHelperText error > { errors . date ?. message } </ FormHelperText >
192182 </ FormControl >
193-
194183 < FormControl >
195184 < FormLabel sx = { { alignSelf : 'start' , paddingTop : '10px' } } > Meeting Start Time</ FormLabel >
196185 < Controller
@@ -271,16 +260,30 @@ export const DesignReviewCreateModal: React.FC<DesignReviewCreateModalProps> = (
271260 < Grid container gap = { 1 } >
272261 < Grid item xs = { 8 } sx = { { alignSelf : 'start' , paddingTop : '10px' , width : '100%' } } >
273262 < FormLabel > Work Package</ FormLabel >
274- < NERAutocomplete
275- id = "wbs-autocomplete"
276- sx = { { bgcolor : 'inherit' } }
277- onChange = { wbsAutocompleteOnChange }
278- options = { wbsDropdownOptions }
279- size = "medium"
280- placeholder = "Select a work package"
281- value = { wbsDropdownOptions . find ( ( element ) => element . id === wbsNum ) || null }
263+ < Controller
264+ name = "wbsNum"
265+ control = { control }
266+ render = { ( { field : { onChange, value } } ) => {
267+ const onClear = ( ) => {
268+ setValue ( 'wbsNum' , '' ) ;
269+ onChange ( '' ) ;
270+ } ;
271+ return (
272+ < NERAutocomplete
273+ id = "wbs-autocomplete"
274+ sx = { { bgcolor : 'inherit' } }
275+ onChange = { ( _event , newValue ) => {
276+ newValue ? onChange ( newValue . id ) : onClear ( ) ;
277+ } }
278+ options = { wbsDropdownOptions }
279+ size = "medium"
280+ placeholder = "Select a work package"
281+ value = { wbsDropdownOptions . find ( ( element ) => element . id === value ) || null }
282+ />
283+ ) ;
284+ } }
282285 />
283- { wbsNum === '' && < FormHelperText error > Work Package is required </ FormHelperText > }
286+ < FormHelperText error > { errors . wbsNum ?. message } </ FormHelperText >
284287 </ Grid >
285288 < Grid item xs = { 3 } >
286289 < FormControl >
@@ -294,17 +297,13 @@ export const DesignReviewCreateModal: React.FC<DesignReviewCreateModalProps> = (
294297 value = { value }
295298 displayEmpty
296299 renderValue = { ( ) => {
297- console . log ( value ) ;
298300 return value ? (
299301 < Typography > { teamTypes . find ( ( teamType ) => teamType . teamTypeId === value ) ?. name } </ Typography >
300302 ) : (
301303 < Typography style = { { color : 'gray' } } > Select Subteam</ Typography >
302304 ) ;
303305 } }
304- onChange = { ( event : SelectChangeEvent < string > ) => {
305- setTeamError ( false ) ;
306- return onChange ( event . target . value ) ;
307- } }
306+ onChange = { ( event : SelectChangeEvent < string > ) => onChange ( event . target . value ) }
308307 sx = { { height : 56 , width : '100%' , textAlign : 'left' } }
309308 MenuProps = { {
310309 anchorOrigin : {
@@ -327,7 +326,7 @@ export const DesignReviewCreateModal: React.FC<DesignReviewCreateModalProps> = (
327326 </ Select >
328327 ) }
329328 />
330- < FormHelperText error > { teamError ? 'Team Type is required' : errors . teamTypeId ?. message } </ FormHelperText >
329+ < FormHelperText error > { errors . teamTypeId ?. message } </ FormHelperText >
331330 </ FormControl >
332331 </ Grid >
333332 </ Grid >
0 commit comments