|
| 1 | +import React from 'react'; |
| 2 | +import { useAllWorkPackages } from '../../hooks/work-packages.hooks'; |
| 3 | +import { WbsElementStatus } from 'shared'; |
| 4 | +import { useCurrentUser } from '../../hooks/users.hooks'; |
| 5 | +import { wbsPipe } from 'shared'; |
| 6 | +import { Box, Alert, Typography, AlertTitle, Grid } from '@mui/material'; |
| 7 | +import { routes } from '../../utils/routes'; |
| 8 | +import { datePipe } from '../../utils/pipes'; |
| 9 | +import { NERButton } from '../../components/NERButton'; |
| 10 | +import { useHistory } from 'react-router-dom'; |
| 11 | + |
| 12 | +const OverdueWorkPackageAlerts: React.FC = () => { |
| 13 | + const user = useCurrentUser(); |
| 14 | + const workPackages = useAllWorkPackages({ status: WbsElementStatus.Active }); |
| 15 | + const currentDate = new Date(); |
| 16 | + const history = useHistory(); |
| 17 | + |
| 18 | + // Filter for work packages that are overdue and the user is the project lead |
| 19 | + const userOverdueWorkPackages = workPackages.data |
| 20 | + ?.filter((wp) => wp.projectLead?.userId === user.userId) |
| 21 | + ?.filter((wp) => new Date(wp.endDate) < currentDate); |
| 22 | + |
| 23 | + // If there are no overdue work packages, don't display anything |
| 24 | + if (!userOverdueWorkPackages || userOverdueWorkPackages.length === 0) { |
| 25 | + return null; |
| 26 | + } else { |
| 27 | + return ( |
| 28 | + <Box sx={{ width: '100%', my: 2 }}> |
| 29 | + <Alert |
| 30 | + variant="filled" |
| 31 | + severity="warning" |
| 32 | + sx={{ |
| 33 | + '& .MuiAlert-message': { |
| 34 | + width: '100%' |
| 35 | + } |
| 36 | + }} |
| 37 | + > |
| 38 | + <Box> |
| 39 | + <AlertTitle> |
| 40 | + {userOverdueWorkPackages.length > 1 ? 'Overdue Work Packages:' : 'Overdue Work Package:'} |
| 41 | + </AlertTitle> |
| 42 | + <Grid container spacing={2}> |
| 43 | + {userOverdueWorkPackages.map((wp) => ( |
| 44 | + <Grid item xs={6} md={3} key={wp.id}> |
| 45 | + {wbsPipe(wp.wbsNum)} - {wp.name} |
| 46 | + <Typography fontWeight={'regular'} variant="inherit" noWrap my={0.5}> |
| 47 | + {'Due: ' + datePipe(wp.endDate)} |
| 48 | + </Typography> |
| 49 | + <NERButton |
| 50 | + variant="contained" |
| 51 | + size="small" |
| 52 | + onClick={() => history.push(`${routes.PROJECTS}/${wbsPipe(wp.wbsNum)}`)} |
| 53 | + > |
| 54 | + Create Change Request |
| 55 | + </NERButton> |
| 56 | + </Grid> |
| 57 | + ))} |
| 58 | + </Grid> |
| 59 | + </Box> |
| 60 | + </Alert> |
| 61 | + </Box> |
| 62 | + ); |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +export default OverdueWorkPackageAlerts; |
0 commit comments