1
- import React , { useEffect , useContext } from 'react' ;
1
+ import React , { useState , useEffect , useContext } from 'react' ;
2
2
import { JsxElement } from 'typescript' ;
3
3
import { ApplicationContext } from '../context/ApplicationContext' ;
4
4
import * as DashboardContext from '../context/DashboardContext' ;
5
5
import lightAndDark from '../components/Styling' ;
6
6
import '../stylesheets/ModifyMetrics.scss' ;
7
+ import { Button } from '@material-ui/core' ;
7
8
const { ipcRenderer } = window . require ( 'electron' ) ;
8
9
9
- const MetricsContainer :React . FC = React . memo ( props => {
10
-
10
+ const MetricsContainer : React . FC = React . memo ( props => {
11
11
const { savedMetrics, setSavedMetrics } = useContext ( ApplicationContext ) ;
12
+ const [ showCheckMark , setShowCheckMark ] = useState ( true ) ;
12
13
const kubernetesMetrics : any [ ] = [ ] ;
13
14
const kafkaMetrics : any [ ] = [ ] ;
14
15
const healthMetrics : any [ ] = [ ] ;
@@ -18,47 +19,96 @@ const MetricsContainer:React.FC = React.memo(props => {
18
19
19
20
const metricsToChange = { } ;
20
21
21
- const changeMetric = ( event ) => {
22
- const m = { ...savedMetrics [ event . target . id ] } ;
22
+ const changeMetric = event => {
23
+ const m = { ...savedMetrics [ event . target . id ] } ;
23
24
m [ 'selected' ] = event . target . checked ;
24
25
metricsToChange [ event . target . id ] = m ;
25
- }
26
+
27
+ const updatedMetric = { ...savedMetrics , [ event . target . id ] : m } ;
28
+ setSavedMetrics ( updatedMetric ) ;
29
+ } ;
26
30
27
31
const updateMetrics = ( ) => {
28
32
// Sets state for savedMetrics with metrics "selected" option updated based on checkbox selection
29
- const newMetrics = { ...savedMetrics } ;
33
+ const newMetrics = { ...savedMetrics } ;
30
34
for ( let key in metricsToChange ) {
31
35
newMetrics [ key ] = metricsToChange [ key ] ;
32
36
}
33
37
setSavedMetrics ( newMetrics ) ;
34
38
// Sends patch request to db to update which metrics get saved to db
35
39
ipcRenderer . send ( 'updateSavedMetrics' , Object . values ( metricsToChange ) ) ;
36
40
// ipcRenderer.on('updateResponse')
37
- }
41
+ } ;
42
+
43
+ /**
44
+ * @desc Toggle functionality to deselect and select all metrics.
45
+ */
46
+ const toggleCheckbox = ( ) => {
47
+ const newMetrics = { ...savedMetrics } ;
48
+ for ( let key in newMetrics ) {
49
+ newMetrics [ key ] . selected = ! showCheckMark ;
50
+ }
51
+ setSavedMetrics ( newMetrics ) ;
52
+ setShowCheckMark ( ! showCheckMark ) ;
53
+ } ;
38
54
39
55
Object . values ( savedMetrics ) . forEach ( ( el : any ) => {
40
56
const jsxEl = (
41
57
< div key = { el . metric } className = "modifyMetric" >
42
58
< label style = { currentMode } >
43
- < input type = "checkbox" key = { el . metric } id = { el . metric } defaultChecked = { el . selected } onClick = { changeMetric } > </ input >
59
+ < input
60
+ type = "checkbox"
61
+ key = { el . metric }
62
+ id = { el . metric }
63
+ checked = { el . selected }
64
+ onChange = { changeMetric }
65
+ > </ input >
44
66
{ el . metric }
45
67
</ label >
46
- </ div > )
68
+ </ div >
69
+ ) ;
47
70
if ( el . mode === 'kubernetes' ) kubernetesMetrics . push ( jsxEl ) ;
48
71
else if ( el . mode === 'kafka' ) kafkaMetrics . push ( jsxEl ) ;
49
72
else healthMetrics . push ( jsxEl ) ;
50
- } )
73
+ } ) ;
51
74
52
75
return (
53
76
< div className = "metricsSelector" >
54
- < h2 style = { currentMode } > Modify which metrics your Chronos app will track by selecting or deselecting from the list below.</ h2 >
55
- < p style = { currentMode } > This can be helpful if you find that you and your team often only care to track a small handful of metrics, and don't want your database to be overwhelmed with thousands of datapoints that you don't necessarily need.</ p >
56
- < button id = "changeDatabaseSettingsButton" className = "select" onClick = { updateMetrics } > Change Database Settings</ button >
57
- { ! ! kubernetesMetrics . length && < div className = 'metricsSublist' > < h3 style = { currentMode } > Kubernetes Metrics:</ h3 > { kubernetesMetrics } </ div > }
58
- { ! ! kafkaMetrics . length && < div className = 'metricsSublist' > < h3 style = { currentMode } > Kafka Metrics:</ h3 > { kafkaMetrics } </ div > }
59
- { ! ! healthMetrics . length && < div className = 'metricsSublist' > < h3 style = { currentMode } > Health Metrics:</ h3 > { healthMetrics } </ div > }
77
+ < h2 style = { currentMode } >
78
+ Modify which metrics your Chronos app will track by selecting or deselecting from the list
79
+ below.
80
+ </ h2 >
81
+ < p style = { currentMode } >
82
+ This can be helpful if you find that you and your team often only care to track a small
83
+ handful of metrics, and don't want your database to be overwhelmed with thousands of
84
+ datapoints that you don't necessarily need.
85
+ </ p >
86
+ < button id = "changeDatabaseSettingsButton" className = "select" onClick = { updateMetrics } >
87
+ Change Database Settings
88
+ </ button >
89
+ < Button variant = "contained" color = "primary" id = "toggleCheck" onClick = { toggleCheckbox } >
90
+ { showCheckMark ? 'Deselect All' : 'Select All' }
91
+ </ Button >
92
+ { ! ! kubernetesMetrics . length && (
93
+ < div className = "metricsSublist" >
94
+ < h3 style = { currentMode } > Kubernetes Metrics:</ h3 >
95
+ { kubernetesMetrics }
96
+ </ div >
97
+ ) }
98
+ { ! ! kafkaMetrics . length && (
99
+ < div className = "metricsSublist" >
100
+ < h3 style = { currentMode } > Kafka Metrics:</ h3 >
101
+ { kafkaMetrics }
102
+ </ div >
103
+ ) }
104
+ { ! ! healthMetrics . length && (
105
+ < div className = "metricsSublist" >
106
+ < h3 style = { currentMode } > Health Metrics:</ h3 >
107
+ { healthMetrics }
108
+ </ div >
109
+ ) }
60
110
</ div >
61
111
) ;
62
112
} ) ;
63
113
64
- export default MetricsContainer ;
114
+ export default MetricsContainer ;
0 commit comments