|
| 1 | +import { ChangeRequest, ChangeRequestStatus, isLeadership, wbsPipe } from 'shared'; |
| 2 | +import ActionsMenu from '../../components/ActionsMenu'; |
| 3 | +import { Autocomplete, Checkbox, TextField, Box } from '@mui/material'; |
| 4 | +import EditIcon from '@mui/icons-material/Edit'; |
| 5 | +import DeleteIcon from '@mui/icons-material/Delete'; |
| 6 | +import ContentPasteIcon from '@mui/icons-material/ContentPaste'; |
| 7 | +import CheckBoxIcon from '@mui/icons-material/CheckBox'; |
| 8 | +import CheckBoxOutlineBlankIcon from '@mui/icons-material/CheckBoxOutlineBlank'; |
| 9 | +import PostAddIcon from '@mui/icons-material/PostAdd'; |
| 10 | +import CreateNewFolderIcon from '@mui/icons-material/CreateNewFolder'; |
| 11 | +import { useHistory } from 'react-router-dom'; |
| 12 | +import { NERButton } from '../../components/NERButton'; |
| 13 | +import { useRequestCRReview } from '../../hooks/change-requests.hooks'; |
| 14 | +import { useToast } from '../../hooks/toasts.hooks'; |
| 15 | +import { useCurrentUser, useAllUsers } from '../../hooks/users.hooks'; |
| 16 | +import { projectWbsPipe } from '../../utils/pipes'; |
| 17 | +import { routes } from '../../utils/routes'; |
| 18 | +import { userToAutocompleteOption } from '../../utils/task.utils'; |
| 19 | +import { useState } from 'react'; |
| 20 | +import ErrorPage from '../ErrorPage'; |
| 21 | +import LoadingIndicator from '../../components/LoadingIndicator'; |
| 22 | + |
| 23 | +interface ChangeRequestActionMenuProps { |
| 24 | + isUserAllowedToReview: boolean; |
| 25 | + isUserAllowedToImplement: boolean; |
| 26 | + isUserAllowedToDelete: boolean; |
| 27 | + changeRequest: ChangeRequest; |
| 28 | + handleReviewOpen: () => void; |
| 29 | + handleDeleteOpen: () => void; |
| 30 | +} |
| 31 | + |
| 32 | +const ChangeRequestActionMenu: React.FC<ChangeRequestActionMenuProps> = ({ |
| 33 | + isUserAllowedToReview, |
| 34 | + isUserAllowedToImplement, |
| 35 | + isUserAllowedToDelete, |
| 36 | + changeRequest, |
| 37 | + handleReviewOpen, |
| 38 | + handleDeleteOpen |
| 39 | +}: ChangeRequestActionMenuProps) => { |
| 40 | + const { mutateAsync: requestCRReview } = useRequestCRReview(changeRequest.crId.toString()); |
| 41 | + const toast = useToast(); |
| 42 | + const currentUser = useCurrentUser(); |
| 43 | + const history = useHistory(); |
| 44 | + const [reviewerIds, setReviewerIds] = useState<number[]>([]); |
| 45 | + const { data: users, isLoading: isLoadingAllUsers, isError: isErrorAllUsers, error: errorAllUsers } = useAllUsers(); |
| 46 | + |
| 47 | + if (isErrorAllUsers) return <ErrorPage message={errorAllUsers?.message} />; |
| 48 | + if (isLoadingAllUsers || !users) return <LoadingIndicator />; |
| 49 | + |
| 50 | + const handleRequestReviewerClick = async () => { |
| 51 | + if (reviewerIds.length === 0) { |
| 52 | + toast.error('Must select at least one reviewer to request review from'); |
| 53 | + } else { |
| 54 | + try { |
| 55 | + await requestCRReview({ userIds: reviewerIds }); |
| 56 | + } catch (e) { |
| 57 | + if (e instanceof Error) { |
| 58 | + toast.error(e.message); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + const isRequestAllowed = |
| 65 | + changeRequest.submitter.userId === currentUser.userId && changeRequest.status === ChangeRequestStatus.Open; |
| 66 | + |
| 67 | + const UnreviewedActionsDropdown = () => ( |
| 68 | + <div style={{ marginTop: '10px' }}> |
| 69 | + <ActionsMenu |
| 70 | + buttons={[ |
| 71 | + { |
| 72 | + title: 'Review', |
| 73 | + onClick: handleReviewOpen, |
| 74 | + disabled: !isUserAllowedToReview, |
| 75 | + icon: <ContentPasteIcon fontSize="small" /> |
| 76 | + }, |
| 77 | + { |
| 78 | + title: 'Delete', |
| 79 | + onClick: handleDeleteOpen, |
| 80 | + disabled: !isUserAllowedToDelete, |
| 81 | + icon: <DeleteIcon fontSize="small" />, |
| 82 | + dividerTop: true |
| 83 | + } |
| 84 | + ]} |
| 85 | + /> |
| 86 | + </div> |
| 87 | + ); |
| 88 | + |
| 89 | + const requestReviewerDropdown = () => ( |
| 90 | + <> |
| 91 | + <Autocomplete |
| 92 | + isOptionEqualToValue={(option, value) => option.id === value.id} |
| 93 | + limitTags={1} |
| 94 | + disableCloseOnSelect |
| 95 | + multiple |
| 96 | + options={users.filter((user) => isLeadership(user.role)).map(userToAutocompleteOption)} |
| 97 | + getOptionLabel={(option) => option.label} |
| 98 | + onChange={(_, values) => setReviewerIds(values.map((value) => value.id))} |
| 99 | + renderTags={() => null} |
| 100 | + renderOption={(props, option, { selected }) => ( |
| 101 | + <li {...props}> |
| 102 | + <Checkbox |
| 103 | + icon={<CheckBoxOutlineBlankIcon />} |
| 104 | + checkedIcon={<CheckBoxIcon />} |
| 105 | + style={{ marginRight: 8 }} |
| 106 | + checked={selected} |
| 107 | + /> |
| 108 | + {option.label} |
| 109 | + </li> |
| 110 | + )} |
| 111 | + renderInput={(params) => ( |
| 112 | + <TextField {...params} variant="standard" placeholder={`${reviewerIds.length} Reviewers Selected`} /> |
| 113 | + )} |
| 114 | + /> |
| 115 | + |
| 116 | + <Box display="flex" flexDirection="row" gap="10px" justifyContent="right"> |
| 117 | + <NERButton |
| 118 | + sx={{ mt: '10px', float: 'right' }} |
| 119 | + variant="contained" |
| 120 | + disabled={!isRequestAllowed} |
| 121 | + onClick={handleRequestReviewerClick} |
| 122 | + > |
| 123 | + Request Review |
| 124 | + </NERButton> |
| 125 | + <UnreviewedActionsDropdown /> |
| 126 | + </Box> |
| 127 | + </> |
| 128 | + ); |
| 129 | + |
| 130 | + const renderUnreviewedActionsDropdown = () => |
| 131 | + isRequestAllowed ? requestReviewerDropdown() : <UnreviewedActionsDropdown />; |
| 132 | + |
| 133 | + const ImplementCrDropdown = () => ( |
| 134 | + <ActionsMenu |
| 135 | + buttons={[ |
| 136 | + { |
| 137 | + title: 'Create New Project', |
| 138 | + onClick: () => |
| 139 | + history.push(`${routes.PROJECTS_NEW}?crId=${changeRequest.crId}&wbs=${projectWbsPipe(changeRequest.wbsNum)}`), |
| 140 | + disabled: !isUserAllowedToImplement, |
| 141 | + icon: <CreateNewFolderIcon fontSize="small" /> |
| 142 | + }, |
| 143 | + { |
| 144 | + title: 'Create New Work Package', |
| 145 | + onClick: () => |
| 146 | + history.push( |
| 147 | + `${routes.WORK_PACKAGE_NEW}?crId=${changeRequest.crId}&wbs=${projectWbsPipe(changeRequest.wbsNum)}` |
| 148 | + ), |
| 149 | + disabled: !isUserAllowedToImplement, |
| 150 | + icon: <PostAddIcon fontSize="small" /> |
| 151 | + }, |
| 152 | + { |
| 153 | + title: `Edit ${changeRequest.wbsNum.workPackageNumber === 0 ? 'Project' : 'Work Package'}`, |
| 154 | + onClick: () => |
| 155 | + history.push(`${routes.PROJECTS}/${wbsPipe(changeRequest.wbsNum)}?crId=${changeRequest.crId}&edit=${true}`), |
| 156 | + disabled: !isUserAllowedToImplement, |
| 157 | + icon: <EditIcon fontSize="small" /> |
| 158 | + } |
| 159 | + ]} |
| 160 | + title="Implement Change Request" |
| 161 | + /> |
| 162 | + ); |
| 163 | + |
| 164 | + return changeRequest.accepted ? <ImplementCrDropdown /> : <>{renderUnreviewedActionsDropdown()}</>; |
| 165 | +}; |
| 166 | + |
| 167 | +export default ChangeRequestActionMenu; |
0 commit comments