1- import { TableRow , TableCell , Box } from '@mui/material' ;
1+ import { TableRow , TableCell , Box , IconButton , Typography } from '@mui/material' ;
22import AdminToolTable from '../AdminToolTable' ;
33import { NERButton } from '../../../components/NERButton' ;
44import { isAdmin } from 'shared/src/permission-utils' ;
55import { useCurrentUser } from '../../../hooks/users.hooks' ;
66import LoadingIndicator from '../../../components/LoadingIndicator' ;
77import ErrorPage from '../../ErrorPage' ;
8- import { useAllWorkPackageTemplates } from '../../../hooks/work-packages.hooks' ;
8+ import { useAllWorkPackageTemplates , useDeleteWorkPackageTemplate } from '../../../hooks/work-packages.hooks' ;
9+ import { Delete } from '@mui/icons-material' ;
10+ import { useState } from 'react' ;
11+ import NERModal from '../../../components/NERModal' ;
12+ import { WorkPackageTemplate } from 'shared' ;
913
1014const WorkPackageTemplateTable = ( ) => {
1115 const currentUser = useCurrentUser ( ) ;
@@ -16,27 +20,55 @@ const WorkPackageTemplateTable = () => {
1620 error : workPackageTemplatesError
1721 } = useAllWorkPackageTemplates ( ) ;
1822
23+ const [ templateToDelete , setTemplateToDelete ] = useState < WorkPackageTemplate > ( ) ;
24+
25+ const { mutateAsync } = useDeleteWorkPackageTemplate ( ) ;
26+
1927 if ( ! workPackageTemplates || workPackageTemplatesIsLoading ) return < LoadingIndicator /> ;
2028 if ( workPackageTemplatesIsError ) return < ErrorPage message = { workPackageTemplatesError . message } /> ;
2129
22- const workPackageTemplateRows = workPackageTemplates . map ( ( workPackageTemplateId ) => (
30+ const workPackageTemplateRows = workPackageTemplates . map ( ( workPackageTemplate ) => (
2331 < TableRow >
2432 < TableCell align = "left" sx = { { border : '2px solid black' } } >
25- { workPackageTemplateId . templateName }
33+ { workPackageTemplate . templateName }
2634 </ TableCell >
27- < TableCell sx = { { border : '2px solid black' , verticalAlign : 'middle' } } >
28- { workPackageTemplateId . templateNotes }
35+ < TableCell sx = { { border : '2px solid black' , verticalAlign : 'middle' } } > { workPackageTemplate . templateNotes } </ TableCell >
36+ < TableCell align = "center" sx = { { border : '2px solid black' , verticalAlign : 'middle' } } >
37+ < IconButton onClick = { ( ) => setTemplateToDelete ( workPackageTemplate ) } >
38+ < Delete />
39+ </ IconButton >
2940 </ TableCell >
3041 </ TableRow >
3142 ) ) ;
3243
3344 return (
34- < Box >
35- < AdminToolTable columns = { [ { name : 'Name' } , { name : 'Description' } ] } rows = { workPackageTemplateRows } />
36- < Box sx = { { display : 'flex' , justifyContent : 'right' , marginTop : '10px' } } >
37- { isAdmin ( currentUser . role ) && < NERButton variant = "contained" > New Work Package Template</ NERButton > }
45+ < >
46+ < Box >
47+ < AdminToolTable columns = { [ { name : 'Name' } , { name : 'Description' } , { name : '' } ] } rows = { workPackageTemplateRows } />
48+ < Box sx = { { display : 'flex' , justifyContent : 'right' , marginTop : '10px' } } >
49+ { isAdmin ( currentUser . role ) && < NERButton variant = "contained" > New Work Package Template</ NERButton > }
50+ </ Box >
3851 </ Box >
39- </ Box >
52+ < NERModal
53+ open = { ! ! templateToDelete }
54+ title = "Warning!"
55+ onHide = { ( ) => setTemplateToDelete ( undefined ) }
56+ submitText = "Delete"
57+ onSubmit = { ( ) => {
58+ mutateAsync ( templateToDelete ! . workPackageTemplateId ) ;
59+ setTemplateToDelete ( undefined ) ;
60+ } }
61+ >
62+ < Typography gutterBottom >
63+ Are you sure you want to delete the work package template < i > { templateToDelete ?. templateName } </ i > ?
64+ </ Typography >
65+ < Typography gutterBottom >
66+ This will also delete all templates blocked by this one. If you would like to delete this template only, first
67+ remove all references to it from all other templates' "Blocked By" sections.
68+ </ Typography >
69+ < Typography fontWeight = "bold" > This action cannot be undone!</ Typography >
70+ </ NERModal >
71+ </ >
4072 ) ;
4173} ;
4274
0 commit comments