Skip to content

Commit f3e688f

Browse files
committed
modularized ApplicationCard,added EventHandler
1 parent e2cb1be commit f3e688f

2 files changed

Lines changed: 25 additions & 75 deletions

File tree

Lines changed: 19 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,20 @@
1-
2-
import React, { useContext, useRef } from "react";
3-
import { useNavigate } from 'react-router-dom';
4-
import { Card,CardHeader,IconButton,CardContent,Typography } from "@mui/material";
5-
import { DashboardContext } from "../../context/DashboardContext";
6-
import { ApplicationContext } from "../../context/ApplicationContext";
1+
import React from 'react';
2+
import { Card, CardHeader, IconButton, CardContent, Typography } from '@mui/material';
73
import HighlightOffIcon from '@mui/icons-material/HighlightOff';
84
import UpdateIcon from '@mui/icons-material/Update';
9-
import './styles.scss'
10-
11-
type ClickEvent = React.MouseEvent<HTMLElement>;
12-
13-
const ApplicationsCard = (props) => {
14-
15-
const { application, i, setModal, classes } = props
16-
const { deleteApp,user,applications } = useContext(DashboardContext)
17-
const { setAppIndex, setApp, setServicesData, app,example,connectToDB,setChart } = useContext(ApplicationContext)
18-
const [ cardName,dbType,dbURI,description,serviceType ] = application
19-
20-
//dynamic refs
21-
// const delRef = useRef<any>([]);
5+
import './styles.scss';
6+
import { getEventHandlers } from './EventHandlers';
227

23-
const navigate = useNavigate();
24-
25-
// Handle clicks on Application cards
26-
// v10 info: when card is clicked (not on the delete button) if the service is an AWS instance,
27-
// you are redirected to AWSGraphsContainer passing in the state object containing typeOfService
28-
const handleClick = (
29-
selectedApp: string,
30-
selectedService: string,
31-
i: number
32-
) => {
33-
const services = ['auth','client','event-bus','items','inventory','orders']
34-
// if (delRef.current[i] && !delRef.current[i].contains(event.target)) {
35-
setAppIndex(i);
36-
setApp(selectedApp);
37-
if (
38-
selectedService === 'AWS' ||
39-
selectedService === 'AWS/EC2' ||
40-
selectedService === 'AWS/ECS' ||
41-
selectedService === 'AWS/EKS'
42-
) {
43-
navigate(`/aws/:${app}`, { state: { typeOfService: selectedService } });
44-
}
45-
else if(example) {
46-
setServicesData([]);
47-
setChart('communications')
48-
49-
connectToDB(user, i, app, dbURI, dbType)
50-
navigate(`/applications/example/${services.join(' ')}`)
51-
}
52-
else {
53-
setServicesData([]);
54-
//When we open the service modal a connection is made to the db in a useEffect inside of the service modal component
55-
setModal({isOpen:true,type:'serviceModal'})
56-
}
57-
// }
58-
};
8+
interface ApplicationCardProps {
9+
application: any[];
10+
i: number;
11+
setModal: (modalState: { isOpen: boolean; type: string }) => void;
12+
classes: any;
13+
}
5914

60-
// Asks user to confirm deletion
61-
const confirmDelete = (event: ClickEvent, i: number) => {
62-
event.stopPropagation()
63-
const message = `The application '${app}' will be permanently deleted. Continue?`;
64-
if (confirm(message)) deleteApp(i,"");
65-
};
15+
const ApplicationsCard: React.FC<ApplicationCardProps> = (props: ApplicationCardProps) => {
16+
const { application, i, setModal, classes } = props;
17+
const { handleClick, confirmDelete } = getEventHandlers({ application, setModal });
6618

6719
return (
6820
<div className="card" key={`card-${i}`} id={`card-${application[1]}`}>
@@ -73,8 +25,7 @@ const ApplicationsCard = (props) => {
7325
onClick={() => handleClick(application[0], application[3], i)}
7426
>
7527
<div className="databaseIconContainer">
76-
<div className="databaseIconHeader">
77-
</div>
28+
<div className="databaseIconHeader"></div>
7829
</div>
7930

8031
<CardHeader
@@ -84,11 +35,9 @@ const ApplicationsCard = (props) => {
8435
className={classes.iconbutton}
8536
aria-label="Delete"
8637
onClick={event => confirmDelete(event, i)}
87-
size="large">
88-
<HighlightOffIcon
89-
className={classes.btnStyle}
90-
id="deleteIcon"
91-
/>
38+
size="large"
39+
>
40+
<HighlightOffIcon className={classes.btnStyle} id="deleteIcon" />
9241
</IconButton>
9342
}
9443
/>
@@ -110,6 +59,6 @@ const ApplicationsCard = (props) => {
11059
</Card>
11160
</div>
11261
);
113-
}
62+
};
11463

115-
export default ApplicationsCard
64+
export default ApplicationsCard;

app/components/ApplicationsCard/EventHandlers.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useContext } from 'react';
22
import { useNavigate } from 'react-router-dom';
3-
import { DashboardContext } from "../../context/DashboardContext";
4-
import { ApplicationContext } from "../../context/ApplicationContext";
3+
import { DashboardContext } from '../../context/DashboardContext';
4+
import { ApplicationContext } from '../../context/ApplicationContext';
55

66
type ClickEvent = React.MouseEvent<HTMLElement>;
77

@@ -10,9 +10,10 @@ interface EventHandlersProps {
1010
setModal: (modalState: { isOpen: boolean; type: string }) => void;
1111
}
1212

13-
export const useEventHandlers = ({ application, setModal }: EventHandlersProps) => {
13+
export const getEventHandlers = ({ application, setModal }: EventHandlersProps) => {
1414
const { deleteApp, user } = useContext(DashboardContext);
15-
const { setAppIndex, setApp, setServicesData, app, example, connectToDB, setChart } = useContext(ApplicationContext);
15+
const { setAppIndex, setApp, setServicesData, app, example, connectToDB, setChart } =
16+
useContext(ApplicationContext);
1617
const navigate = useNavigate();
1718

1819
const handleClick = (selectedApp: string, selectedService: string, i: number) => {
@@ -35,7 +36,7 @@ export const useEventHandlers = ({ application, setModal }: EventHandlersProps)
3536
const confirmDelete = (event: ClickEvent, i: number) => {
3637
event.stopPropagation();
3738
const message = `The application '${app}' will be permanently deleted. Continue?`;
38-
if (confirm(message)) deleteApp(i, "");
39+
if (confirm(message)) deleteApp(i, '');
3940
};
4041

4142
return { handleClick, confirmDelete };

0 commit comments

Comments
 (0)