1- import React , { useContext , useEffect , useState } from 'react' ;
1+ import React , { useContext , useEffect , useState , useRef } from 'react' ;
22import {
33 IconButton ,
44 Grid ,
@@ -10,33 +10,37 @@ import {
1010} from '@material-ui/core' ;
1111import DeleteForeverOutlinedIcon from '@material-ui/icons/DeleteForeverOutlined' ;
1212import { makeStyles } from '@material-ui/core/styles' ;
13- import { DashboardContext } from '../context/DashboardContext' ;
1413
14+ import { DashboardContext } from '../context/DashboardContext' ;
1515import ServicesModal from '../modals/ServicesModal' ;
1616import '../stylesheets/Applications.css' ;
1717
18+ type ClickEvent = React . MouseEvent < HTMLElement > ;
19+
1820const Applications = ( ) => {
1921 const { applications, getApplications, deleteApp } = useContext ( DashboardContext ) ;
2022 const [ open , setOpen ] = useState < boolean > ( false ) ;
2123 const [ index , setIndex ] = useState < number > ( 0 ) ;
2224 const [ app , setApp ] = useState < string > ( '' ) ;
25+ const delRef = useRef < any > ( null ) ;
2326
2427 useEffect ( ( ) => {
2528 getApplications ( ) ;
2629 } , [ ] ) ;
2730
2831 // Ask user for deletetion confirmation
29- const confirmDelete = ( app : string , i : number ) => {
32+ const confirmDelete = ( event : ClickEvent , app : string , i : number ) => {
3033 const message = `The application '${ app } ' will be permanently deleted. Continue?` ;
3134 if ( confirm ( message ) ) deleteApp ( i ) ;
3235 } ;
3336
3437 // Handle clicks on Application cards
35- const handleClick = ( selectedApp : string , i : number ) => {
36- setIndex ( i ) ;
37- setApp ( selectedApp ) ;
38- console . log ( 'handle' , selectedApp ) ;
39- setOpen ( true ) ;
38+ const handleClick = ( event : ClickEvent , selectedApp : string , i : number ) => {
39+ if ( ! delRef . current . contains ( event . target ) ) {
40+ setIndex ( i ) ;
41+ setApp ( selectedApp ) ;
42+ setOpen ( true ) ;
43+ }
4044 } ;
4145
4246 const useStyles = makeStyles ( theme => ( {
@@ -82,14 +86,15 @@ const Applications = () => {
8286 < Card
8387 className = { classes . paper }
8488 variant = "outlined"
85- onClick = { ( event : React . MouseEvent < HTMLElement > ) => handleClick ( app [ 0 ] , i ) }
89+ onClick = { event => handleClick ( event , app [ 0 ] , i ) }
8690 >
8791 < CardHeader
8892 avatar = {
8993 < IconButton
94+ ref = { delRef }
9095 className = { classes . hover }
9196 aria-label = "Delete"
92- onClick = { ( event : React . MouseEvent < HTMLElement > ) => confirmDelete ( app [ 0 ] , i ) }
97+ onClick = { event => confirmDelete ( event , app [ 0 ] , i ) }
9398 >
9499 < DeleteForeverOutlinedIcon />
95100 </ IconButton >
0 commit comments