|
| 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 useEventHandlers = ({ application, setModal }: EventHandlersProps) => { |
| 14 | + const { deleteApp, user } = useContext(DashboardContext); |
| 15 | + const { setAppIndex, setApp, setServicesData, app, example, connectToDB, setChart } = useContext(ApplicationContext); |
| 16 | + const navigate = useNavigate(); |
| 17 | + |
| 18 | + const handleClick = (selectedApp: string, selectedService: string, i: number) => { |
| 19 | + const services = ['auth', 'client', 'event-bus', 'items', 'inventory', 'orders']; |
| 20 | + setAppIndex(i); |
| 21 | + setApp(selectedApp); |
| 22 | + if (['AWS', 'AWS/EC2', 'AWS/ECS', 'AWS/EKS'].includes(selectedService)) { |
| 23 | + navigate(`/aws/:${app}`, { state: { typeOfService: selectedService } }); |
| 24 | + } else if (example) { |
| 25 | + setServicesData([]); |
| 26 | + setChart('communications'); |
| 27 | + connectToDB(user, i, app, application[2], application[1]); |
| 28 | + navigate(`/applications/example/${services.join(' ')}`); |
| 29 | + } else { |
| 30 | + setServicesData([]); |
| 31 | + setModal({ isOpen: true, type: 'serviceModal' }); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + const confirmDelete = (event: ClickEvent, i: number) => { |
| 36 | + event.stopPropagation(); |
| 37 | + const message = `The application '${app}' will be permanently deleted. Continue?`; |
| 38 | + if (confirm(message)) deleteApp(i, ""); |
| 39 | + }; |
| 40 | + |
| 41 | + return { handleClick, confirmDelete }; |
| 42 | +}; |
0 commit comments