Skip to content

Commit 387e9bc

Browse files
authored
Merge pull request #26 from oslabs-beta/ted-modular
Ted modular
2 parents 2a00b5d + 7918cf5 commit 387e9bc

File tree

2 files changed

+62
-70
lines changed

2 files changed

+62
-70
lines changed
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;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { useContext } from 'react';
2+
import { useNavigate } from 'react-router-dom';
3+
import { DashboardContext } from '../../context/DashboardContext';
4+
import { ApplicationContext } from '../../context/ApplicationContext';
5+
6+
type ClickEvent = React.MouseEvent<HTMLElement>;
7+
8+
interface EventHandlersProps {
9+
application: any[];
10+
setModal: (modalState: { isOpen: boolean; type: string }) => void;
11+
}
12+
13+
export const getEventHandlers = ({ application, setModal }: EventHandlersProps) => {
14+
const { deleteApp, user } = useContext(DashboardContext);
15+
const { setAppIndex, setApp, setServicesData, app, example, connectToDB, setChart } =
16+
useContext(ApplicationContext);
17+
const navigate = useNavigate();
18+
19+
const handleClick = (selectedApp: string, selectedService: string, i: number) => {
20+
const services = ['auth', 'client', 'event-bus', 'items', 'inventory', 'orders'];
21+
setAppIndex(i);
22+
setApp(selectedApp);
23+
if (['AWS', 'AWS/EC2', 'AWS/ECS', 'AWS/EKS'].includes(selectedService)) {
24+
navigate(`/aws/:${app}`, { state: { typeOfService: selectedService } });
25+
} else if (example) {
26+
setServicesData([]);
27+
setChart('communications');
28+
connectToDB(user, i, app, application[2], application[1]);
29+
navigate(`/applications/example/${services.join(' ')}`);
30+
} else {
31+
setServicesData([]);
32+
setModal({ isOpen: true, type: 'serviceModal' });
33+
}
34+
};
35+
36+
const confirmDelete = (event: ClickEvent, i: number) => {
37+
event.stopPropagation();
38+
const message = `The application '${app}' will be permanently deleted. Continue?`;
39+
if (confirm(message)) deleteApp(i, '');
40+
};
41+
42+
return { handleClick, confirmDelete };
43+
};

0 commit comments

Comments
 (0)