|
1 | | -import Box from '@mui/material/Box'; |
| 1 | +import React from 'react'; |
2 | 2 | import { useAllWorkPackages } from '../../hooks/work-packages.hooks'; |
3 | 3 | import { WbsElementStatus } from 'shared'; |
4 | | -import OverdueWorkPackageAlert from './OverdueWorkPackageAlert'; |
5 | 4 | import { useCurrentUser } from '../../hooks/users.hooks'; |
| 5 | +import { useState } from 'react'; |
| 6 | +import { wbsPipe } from 'shared'; |
| 7 | +import { Box, Alert, IconButton, Collapse, Link, Typography, AlertTitle } from '@mui/material'; |
| 8 | +import CloseIcon from '@mui/icons-material/Close'; |
| 9 | +import { routes } from '../../utils/routes'; |
| 10 | +import { Link as RouterLink } from 'react-router-dom'; |
| 11 | +import { datePipe } from '../../utils/pipes'; |
6 | 12 |
|
7 | 13 | const OverdueWorkPackageAlerts: React.FC = () => { |
8 | 14 | const user = useCurrentUser(); |
9 | 15 | const workPackages = useAllWorkPackages({ status: WbsElementStatus.Active }); |
10 | 16 | const currentDate = new Date(); |
| 17 | + const [open, setOpen] = useState(true); |
11 | 18 |
|
12 | | - return ( |
13 | | - <Box> |
14 | | - {workPackages.data |
15 | | - ?.filter((wp) => wp.projectLead?.userId === user.userId) |
16 | | - ?.filter((wp) => new Date(wp.endDate) < currentDate) |
17 | | - .map((wp) => ( |
18 | | - <OverdueWorkPackageAlert wp={wp} /> |
19 | | - ))} |
20 | | - </Box> |
21 | | - ); |
| 19 | + // Filter for work packages that are overdue and the user is the project lead |
| 20 | + const userOverdueWorkPackages = workPackages.data |
| 21 | + ?.filter((wp) => wp.projectLead?.userId === user.userId) |
| 22 | + ?.filter((wp) => new Date(wp.endDate) < currentDate); |
| 23 | + |
| 24 | + // If there are no overdue work packages, don't display anything |
| 25 | + if (!userOverdueWorkPackages || userOverdueWorkPackages.length === 0) { |
| 26 | + return null; |
| 27 | + } else { |
| 28 | + return ( |
| 29 | + <Box sx={{ width: '100%', my: 2 }}> |
| 30 | + <Collapse in={open}> |
| 31 | + <Alert |
| 32 | + variant="filled" |
| 33 | + severity="warning" |
| 34 | + action={ |
| 35 | + <IconButton |
| 36 | + aria-label="close" |
| 37 | + color="inherit" |
| 38 | + size="small" |
| 39 | + onClick={() => { |
| 40 | + setOpen(false); |
| 41 | + }} |
| 42 | + > |
| 43 | + <CloseIcon fontSize="inherit" /> |
| 44 | + </IconButton> |
| 45 | + } |
| 46 | + > |
| 47 | + <AlertTitle> |
| 48 | + {userOverdueWorkPackages.length > 1 ? 'Overdue Work Packages:' : 'Overdue Work Package:'} |
| 49 | + </AlertTitle> |
| 50 | + {userOverdueWorkPackages.map((wp) => ( |
| 51 | + <React.Fragment key={wp.id}> |
| 52 | + <Link color="inherit" component={RouterLink} to={`${routes.PROJECTS}/${wbsPipe(wp.wbsNum)}`} noWrap> |
| 53 | + <Typography fontWeight={'regular'} variant="inherit"> |
| 54 | + {wbsPipe(wp.wbsNum)} - {wp.name} |
| 55 | + </Typography> |
| 56 | + </Link> |
| 57 | + <Typography fontWeight={'regular'} variant="inherit" noWrap my={0.5}> |
| 58 | + {'Due: ' + datePipe(wp.endDate)} |
| 59 | + </Typography> |
| 60 | + </React.Fragment> |
| 61 | + ))} |
| 62 | + </Alert> |
| 63 | + </Collapse> |
| 64 | + </Box> |
| 65 | + ); |
| 66 | + } |
22 | 67 | }; |
23 | 68 |
|
24 | 69 | export default OverdueWorkPackageAlerts; |
0 commit comments