|
1 | | -import React, { useContext, useState, useEffect } from 'react'; |
2 | | -import OverviewContext from '../context/OverviewContext'; |
3 | | -import HealthInformationContext from '../context/DetailsContext'; |
4 | | -import ServiceDetails from '../components/ServiceDetails.jsx'; |
| 1 | +import React from 'react'; |
| 2 | +import '../stylesheets/Microservices.css'; |
5 | 3 |
|
6 | | -const { ipcRenderer } = window.require('electron'); |
7 | 4 |
|
8 | 5 | const Microservices = (props) => { |
9 | | - const { index, setDetails } = props; |
10 | | - // Overview state used to create service buttons |
11 | | - const [overviewState, setOverviewState] = useState([]); |
12 | | - |
13 | | - // Contexts have data added to them following successful IPC return. Data is later used to create charts. |
14 | | - const serviceComponents = useContext(OverviewContext); |
15 | | - |
16 | | - useEffect(() => { |
17 | | - // IPC communication used to initiate query for information on microservices. |
18 | | - ipcRenderer.send('overviewRequest', index); |
19 | | - // IPC listener responsible for retrieving infomation from asynchronous main process message. |
20 | | - ipcRenderer.on('overviewResponse', (event, data) => { |
21 | | - // Adds to state and context. |
22 | | - // console.log(JSON.parse(data)); |
23 | | - setOverviewState(Object.values(JSON.parse(data))); |
24 | | - serviceComponents.overviewData = JSON.parse(data); |
25 | | - }); |
26 | | - }, []); |
27 | | - |
28 | | - // Holds the buttons generated for unique services. |
29 | | - const componentButtons = []; |
30 | | - |
31 | | - // Tracks which services already have button created. |
32 | | - const serviceCache = {}; |
33 | | - // Contexts have data added to them following successful IPC return. Data is later used to create charts. |
34 | | - const healthdata = useContext(HealthInformationContext); |
35 | | - |
36 | | - for (let i = 0; i < overviewState.length; i += 1) { |
37 | | - const element = overviewState[i]; |
38 | | - // If SQL |
39 | | - if (element.currentmicroservice) { |
40 | | - if (!(element.currentmicroservice in serviceCache)) { |
41 | | - const button = ( |
42 | | - <button |
43 | | - className="servicesBtn" |
44 | | - currentMicroservice={element.currentmicroservice} |
45 | | - type="button" |
46 | | - key={`serviceItem${index}${i}`} |
47 | | - onClick={() => { |
48 | | - // IPC communication used to initiate query for information on microservice health information. |
49 | | - ipcRenderer.send('detailsRequest', index); |
50 | | - |
51 | | - // IPC listener responsible for retrieving infomation from asynchronous main process message. |
52 | | - ipcRenderer.on('detailsResponse', (event, data) => { |
53 | | - // Adds returned data to context |
54 | | - healthdata.detailData = Object.values(JSON.parse(data)); |
55 | | - // Updates state. Triggers rerender. |
56 | | - setDetails( |
57 | | - <ServiceDetails service={element.currentmicroservice} /> |
58 | | - ); |
59 | | - }); |
60 | | - }} |
61 | | - > |
62 | | - {element.currentmicroservice} |
63 | | - </button> |
64 | | - ); |
65 | | - componentButtons.push(button); |
66 | | - serviceCache[element.currentmicroservice] = true; |
67 | | - } |
68 | | - } else if (element.currentMicroservice) { |
69 | | - // If Mongo |
70 | | - if (element.currentMicroservice) { |
71 | | - if (!(element.currentMicroservice in serviceCache)) { |
72 | | - const button = ( |
73 | | - <button |
74 | | - className="servicesBtn" |
75 | | - type="button" |
76 | | - key={`serviceItem${index}${i}`} |
77 | | - onClick={() => { |
78 | | - ipcRenderer.send('detailsRequest', index); |
79 | | - |
80 | | - // IPC listener responsible for retrieving infomation from asynchronous main process message. |
81 | | - ipcRenderer.on('detailsResponse', (event, data) => { |
82 | | - // Adds returned data to context. |
83 | | - healthdata.detailData = Object.values(JSON.parse(data)); |
84 | | - // Updates state. Triggers rerender. |
85 | | - setDetails( |
86 | | - <ServiceDetails service={element.currentMicroservice} /> |
87 | | - ); |
88 | | - }); |
89 | | - }} |
90 | | - > |
91 | | - {element.currentMicroservice} |
92 | | - </button> |
93 | | - ); |
94 | | - componentButtons.push(button); |
95 | | - serviceCache[element.currentMicroservice] = true; |
96 | | - } |
97 | | - } |
98 | | - } |
99 | | - } |
100 | | - // If there's no data, return 'No data present', else return microservices button |
101 | | - if (componentButtons.length === 0) { |
102 | | - return <p>Loading</p>; |
| 6 | + const { context, Click, isclicked } = props; |
| 7 | + const buttonStore = []; |
| 8 | + for (let i = 0; i < context.length; i += 1) { |
| 9 | + buttonStore.push( |
| 10 | + <button |
| 11 | + className="microserviceBtn" |
| 12 | + id={i} |
| 13 | + type="button" |
| 14 | + key={`${i}${context[i]}`} |
| 15 | + onClick={Click} |
| 16 | + isclicked={isclicked} |
| 17 | + > |
| 18 | + {context[i]} |
| 19 | + </button> |
| 20 | + ); |
103 | 21 | } |
104 | | - return componentButtons; |
| 22 | + return ( |
| 23 | + buttonStore |
| 24 | + ); |
105 | 25 | }; |
106 | 26 |
|
107 | 27 | export default Microservices; |
0 commit comments