|
| 1 | +import { Box, FormControl, FormLabel, MenuItem, Select, SelectChangeEvent } from '@mui/material'; |
| 2 | +import { isWithinInterval, subDays } from 'date-fns'; |
| 3 | +import { Control, Controller } from 'react-hook-form'; |
| 4 | +import { AuthenticatedUser, ChangeRequest, wbsPipe } from 'shared'; |
| 5 | +import { useAllChangeRequests } from '../hooks/change-requests.hooks'; |
| 6 | +import { useCurrentUser } from '../hooks/users.hooks'; |
| 7 | +import LoadingIndicator from './LoadingIndicator'; |
| 8 | + |
| 9 | +// Filter and sort change requests to display in the dropdown |
| 10 | +const getFilteredChangeRequests = (changeRequests: ChangeRequest[], user: AuthenticatedUser): ChangeRequest[] => { |
| 11 | + const today = new Date(); |
| 12 | + const fiveDaysAgo = subDays(today, 5); |
| 13 | + |
| 14 | + const filteredRequests = changeRequests.filter( |
| 15 | + (cr) => cr.dateReviewed && cr.accepted && isWithinInterval(cr.dateReviewed, { start: fiveDaysAgo, end: today }) |
| 16 | + ); |
| 17 | + |
| 18 | + // The current user's CRs should be at the top |
| 19 | + filteredRequests.sort((a, b) => { |
| 20 | + const isSubmitterAUser = a.submitter.userId === user.userId; |
| 21 | + const isSubmitterBUser = b.submitter.userId === user.userId; |
| 22 | + |
| 23 | + if (isSubmitterAUser && isSubmitterBUser) return 0; |
| 24 | + if (isSubmitterAUser) return -1; |
| 25 | + if (isSubmitterBUser) return 1; |
| 26 | + |
| 27 | + return a.crId - b.crId; |
| 28 | + }); |
| 29 | + |
| 30 | + return filteredRequests; |
| 31 | +}; |
| 32 | + |
| 33 | +interface ChangeRequestDropdownProps { |
| 34 | + control: Control<any, any>; |
| 35 | + name: string; |
| 36 | +} |
| 37 | + |
| 38 | +const ChangeRequestDropdown = ({ control, name }: ChangeRequestDropdownProps) => { |
| 39 | + const user = useCurrentUser(); |
| 40 | + const { isLoading, data: changeRequests } = useAllChangeRequests(); |
| 41 | + if (isLoading || !changeRequests) return <LoadingIndicator />; |
| 42 | + |
| 43 | + const filteredRequests = getFilteredChangeRequests(changeRequests, user); |
| 44 | + |
| 45 | + const approvedChangeRequestOptions = filteredRequests.map((cr) => ({ |
| 46 | + label: `${cr.crId} - ${wbsPipe(cr.wbsNum)} - ${cr.submitter.firstName} ${cr.submitter.lastName} - ${cr.type}`, |
| 47 | + value: cr.crId |
| 48 | + })); |
| 49 | + |
| 50 | + return ( |
| 51 | + <Box sx={{ display: 'flex', justifyContent: 'end' }}> |
| 52 | + <FormControl> |
| 53 | + <FormLabel sx={{ alignSelf: 'start' }}>Change Request ID</FormLabel> |
| 54 | + <Controller |
| 55 | + control={control} |
| 56 | + name={name} |
| 57 | + render={({ field: { onChange, value } }) => ( |
| 58 | + <Select |
| 59 | + id="cr-autocomplete" |
| 60 | + displayEmpty |
| 61 | + renderValue={(value) => value} |
| 62 | + value={value} |
| 63 | + onChange={(event: SelectChangeEvent<number>) => onChange(event.target.value)} |
| 64 | + size={'small'} |
| 65 | + placeholder={'Change Request Id'} |
| 66 | + sx={{ width: 200, textAlign: 'left' }} |
| 67 | + MenuProps={{ |
| 68 | + anchorOrigin: { |
| 69 | + vertical: 'bottom', |
| 70 | + horizontal: 'right' |
| 71 | + }, |
| 72 | + transformOrigin: { |
| 73 | + vertical: 'top', |
| 74 | + horizontal: 'right' |
| 75 | + } |
| 76 | + }} |
| 77 | + > |
| 78 | + {approvedChangeRequestOptions.map((option) => ( |
| 79 | + <MenuItem key={option.value} value={option.value}> |
| 80 | + {option.label} |
| 81 | + </MenuItem> |
| 82 | + ))} |
| 83 | + </Select> |
| 84 | + )} |
| 85 | + /> |
| 86 | + </FormControl> |
| 87 | + </Box> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +export default ChangeRequestDropdown; |
0 commit comments