1- import React , { useState } from 'react' ;
2- import { Grid , Modal , Button , Typography } from '@material-ui/core' ;
1+ import React , { useContext , useEffect , useState , useRef } from 'react' ;
2+
3+ // MATERIAL UI METHODS
4+ import {
5+ IconButton ,
6+ Modal ,
7+ Card ,
8+ CardHeader ,
9+ CardContent ,
10+ Button ,
11+ Typography } from '@material-ui/core' ;
312import { Theme , makeStyles } from '@material-ui/core/styles' ;
4- import AddCircleOutlineTwoToneIcon from '@material-ui/icons/AddCircleOutlineTwoTone' ;
5- import Copyright from '../components/Copyright' ;
6- import AddModal from '../modals/AddModal' ;
7- import Applications from './Applications' ;
13+ import { BaseCSSProperties } from '@material-ui/core/styles/withStyles' ;
14+
15+ // MATERIAL UI ICONS
816import MoreVertIcon from '@material-ui/icons/MoreVert' ;
17+ import AddCircleOutlineTwoToneIcon from '@material-ui/icons/AddCircleOutlineTwoTone' ;
18+ import DeleteForeverOutlinedIcon from '@material-ui/icons/DeleteForeverOutlined' ;
919import ListIcon from '@material-ui/icons/List' ;
1020import SearchIcon from '@material-ui/icons/Search' ;
1121import DashboardIcon from '@material-ui/icons/Dashboard' ;
1222import NotificationsIcon from '@material-ui/icons/Notifications' ;
1323import PersonIcon from '@material-ui/icons/Person' ;
24+
25+ // MODALS
26+ import AddModal from '../modals/AddModal' ;
27+ import ServicesModal from '../modals/ServicesModal' ;
28+
29+ // STYLESHEETS
1430import '../stylesheets/Occupied.scss' ;
15- import { BaseCSSProperties } from '@material-ui/core/styles/withStyles' ;
1631
32+ // DASHBOARD CONTEXT
33+ import { DashboardContext } from '../context/DashboardContext' ;
34+
35+ // TYPESCRIPT
1736interface StyleProps {
1837 root : BaseCSSProperties ,
1938} ;
39+ type ClickEvent = React . MouseEvent < HTMLElement > ;
2040
2141const Occupied : React . FC = ( ) => {
22- const [ open , setOpen ] = useState ( false ) ;
42+ const { applications, getApplications, deleteApp } = useContext ( DashboardContext ) ;
43+ const [ open , setOpen ] = useState < boolean > ( false ) ;
44+ const [ index , setIndex ] = useState < number > ( 0 ) ;
45+ const [ app , setApp ] = useState < string > ( '' ) ;
46+
47+ // Dynamic refs
48+ const delRef = useRef < any > ( [ ] ) ;
49+
50+ useEffect ( ( ) => {
51+ getApplications ( ) ;
52+ } , [ ] ) ;
53+
54+ // Ask user for deletetion confirmation
55+ const confirmDelete = ( event : ClickEvent , app : string , i : number ) => {
56+ const message = `The application '${ app } ' will be permanently deleted. Continue?` ;
57+ if ( confirm ( message ) ) deleteApp ( i ) ;
58+ } ;
59+
60+ // Handle clicks on Application cards
61+ const handleClick = ( event : ClickEvent , selectedApp : string , i : number ) => {
62+ if ( delRef . current [ i ] && ! delRef . current [ i ] . contains ( event . target ) ) {
63+ setIndex ( i ) ;
64+ setApp ( selectedApp ) ;
65+ setOpen ( true ) ;
66+ }
67+ } ;
2368
2469 const useStyles = makeStyles < Theme , StyleProps > ( theme => ( {
2570 // card: "+" button only
@@ -56,7 +101,7 @@ const Occupied: React.FC = () => {
56101
57102 return (
58103
59- < div >
104+ < div className = "entireArea" >
60105 < div className = "sidebarArea" >
61106 </ div >
62107 < div className = "dashboardArea" >
@@ -80,13 +125,46 @@ const Occupied: React.FC = () => {
80125 < PersonIcon className = "sideIcon" id = "personIcon" />
81126 </ section >
82127 </ header >
83- < Modal open = { open } onClose = { ( ) => setOpen ( false ) } >
84- < AddModal setOpen = { setOpen } />
85- </ Modal >
86- < Button className = { classes . paper } onClick = { ( ) => setOpen ( true ) } >
87- < AddCircleOutlineTwoToneIcon className = { classes . icon } />
88- </ Button >
89- < Applications />
128+
129+ < div className = "cardContainer" >
130+ < div className = "card" id = { `card-add` } >
131+ < Button className = { classes . paper } onClick = { ( ) => setOpen ( true ) } >
132+ < AddCircleOutlineTwoToneIcon className = { classes . icon } />
133+ </ Button >
134+ </ div >
135+ { applications . map ( ( app : string [ ] , i : number | any | string | undefined ) => (
136+ < div className = "card" id = { `card-${ i } ` } >
137+ < Card
138+ key = { `card-${ i } ` }
139+ className = { classes . paper }
140+ variant = "outlined"
141+ onClick = { event => handleClick ( event , app [ 0 ] , i ) }
142+ >
143+ < CardHeader
144+ avatar = {
145+ < IconButton
146+ ref = { element => ( delRef . current [ i ] = element ) }
147+ className = { classes . hover }
148+ aria-label = "Delete"
149+ onClick = { event => confirmDelete ( event , app [ 0 ] , i ) }
150+ >
151+ < DeleteForeverOutlinedIcon />
152+ </ IconButton >
153+ }
154+ > </ CardHeader >
155+ < CardContent >
156+ < Typography className = { 'cardContent' } > { app [ 0 ] } </ Typography >
157+ </ CardContent >
158+ </ Card >
159+ </ div >
160+ ) ) }
161+ < Modal open = { open } onClose = { ( ) => setOpen ( false ) } >
162+ < ServicesModal open = { open } onClose = { ( ) => setOpen ( false ) } i = { index } app = { app } />
163+ </ Modal >
164+ < Modal open = { open } onClose = { ( ) => setOpen ( false ) } >
165+ < AddModal open = { open } onClose = { ( ) => setOpen ( false ) } setOpen = { setOpen } />
166+ </ Modal >
167+ </ div >
90168 </ div >
91169 </ div >
92170 ) ;
0 commit comments