-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
Dashboard.jsx
517 lines (466 loc) · 15.6 KB
/
Dashboard.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import React, { Component } from 'react';
import _ from 'lodash';
import PropTypes from 'prop-types';
import { defaults } from 'react-chartjs-2';
import { connect } from 'react-redux';
import { SortableContainer } from 'react-sortable-hoc';
import {
addToIndicators,
fetchConfig,
fetchConfigAndData,
fetchIndicators,
reloadIndicator,
reorderIndicators,
resetIndicators,
} from 'actions';
import Filter from 'components/dashboard/Filter';
import GraphCard from 'components/dashboard/GraphCard';
import LoadingNumbers from 'components/dashboard/LoadingNumbers';
import NumberCard from 'components/dashboard/NumberCard';
import UnarchiveIndicators from 'components/dashboard/UnarchiveIndicators';
import apiClient from 'utils/apiClient';
import 'react-table/react-table.css';
import 'react-confirm-alert/src/react-confirm-alert.css';
import 'components/dashboard/Dashboard.scss';
// Disable charts legends by default.
defaults.scale.ticks.beginAtZero = true;
// TODO: OBPIH-4385 Refactor/Split this file into separate components
// eslint-disable-next-line no-shadow
const SortableCards = SortableContainer(({
data, loadIndicator, allLocations, personalDashboardActive,
}) => (
<div className="card-component">
{data.map((value, index) =>
(
<GraphCard
key={`item-${value.id}`}
index={index}
cardId={value.id}
widgetId={value.widgetId}
cardTitle={value.title}
cardType={value.type}
cardLink={value.link}
cardInfo={value.info}
data={value.data}
timeFilter={value.timeFilter}
timeLimit={value.timeLimit}
locationFilter={value.locationFilter}
yearTypeFilter={value.yearTypeFilter}
options={value.options}
loadIndicator={loadIndicator}
allLocations={allLocations}
size={value.size}
disabled={!personalDashboardActive}
hideDraghandle={!personalDashboardActive}
/>
))}
</div>
));
const SortableNumberCards = SortableContainer(({ data, personalDashboardActive }) => (
<div className="card-component">
{data.map((value, index) => (
(
<NumberCard
key={`item-${value.id}`}
index={index}
widgetId={value.widgetId}
cardTitle={value.title}
cardNumberType={value.numberType}
cardNumber={value.number}
cardSubtitle={value.subtitle}
cardLink={value.link}
cardDataTooltip={value.tooltipData}
cardInfo={value.info}
sparklineData={value.data}
disabled={!personalDashboardActive}
hideDraghandle={!personalDashboardActive}
/>
)
))}
</div>
));
const ArchiveIndicator = ({ hideArchive }) => (
<div className={hideArchive ? 'archive-div hide-archive' : 'archive-div'}>
<span>
Archive indicator <i className="fa fa-archive" />
</span>
</div>
);
const ConfigurationsList = ({
configs, activeConfig, loadConfigData, showNav, toggleNav, configModified, updateConfig,
}) => {
if (!configs) {
return null;
}
return (
<div className={`configs-left-nav ${!showNav ? 'hidden' : ''}`}>
<button className="toggle-nav" onClick={toggleNav}>
{showNav ?
<i className="fa fa-chevron-left" aria-hidden="true" />
:
<i className="fa fa-chevron-right" aria-hidden="true" />
}
</button>
<ul className="configs-list">
{Object.entries(configs).map(([key, value]) => (
<li className={`configs-list-item ${activeConfig === key ? 'active' : ''}`} key={key}>
<button onClick={() => loadConfigData(key)}>
<i className="fa fa-bar-chart" aria-hidden="true" />
{value.name}
</button>
</li>
))}
</ul>
{
(activeConfig === 'personal' && configModified) ?
<div className="update-section">
<div className="division-line" />
<span> <i className="fa fa-info-circle" aria-hidden="true" />The dashboard layout has been edited</span>
<button onClick={updateConfig} >
<i className="fa fa-floppy-o" aria-hidden="true" />
Save configuration
</button>
</div>
: null
}
</div>
);
};
const MAIN_DASHBOARD_CONFIG = 'mainDashboard';
class Dashboard extends Component {
constructor(props) {
super(props);
this.state = {
isDragging: false,
showPopout: false,
// Not showing if screen too small
showNav: window.innerWidth > 1115,
configModified: false,
allLocations: [],
pageFilters: [],
subdashboardKeys: [],
};
this.fetchLocations();
}
componentDidMount() {
if (this.props.currentLocation !== '') {
this.getSubdashboardKeys().then(() => this.fetchData(this.determineActiveConfig()));
}
this.loadPageFilters(this.props.activeConfig);
}
componentDidUpdate(prevProps) {
const prevLocation = prevProps.currentLocation;
const newLocation = this.props.currentLocation;
if (prevLocation !== newLocation) {
this.getSubdashboardKeys().then(() => this.fetchData(this.determineActiveConfig()));
}
if (prevProps.dashboardConfig.dashboard !== this.props.dashboardConfig.dashboard) {
this.loadPageFilters(this.props.activeConfig);
}
}
getDashboardIdFromParams() {
const dashboardId = this.props.match.params.configId;
if (dashboardId && dashboardId !== 'index') {
return dashboardId;
}
return MAIN_DASHBOARD_CONFIG;
}
getSubdashboardKeys() {
const dashboardId = this.getDashboardIdFromParams();
const url = `/openboxes/api/dashboard/${dashboardId}/subdashboardKeys`;
return apiClient.get(url)
.then((res) => {
const subdashboardKeys = res.data;
if (subdashboardKeys) {
this.setState({ subdashboardKeys });
}
});
}
determineActiveConfig() {
if (this.props.match.params.configId && this.props.match.params.configId !== 'index') {
return this.props.match.params.configId;
}
const configFromSessionStorage = sessionStorage.getItem('dashboardKey');
// eslint-disable-next-line max-len
if (configFromSessionStorage && this.state.subdashboardKeys.includes(configFromSessionStorage)) {
return configFromSessionStorage;
}
return 'personal';
}
dataFetched = false;
loadPageFilters(config = '') {
let pageFilters = [];
if (this.props.dashboardConfig.dashboard) {
const allPages = Object.entries(this.props.dashboardConfig.dashboard)
.map(([key, value]) => [key, value]);
allPages.forEach((page) => {
const filters = Object.entries(page[1].filters)
.map(([keyFilter, valueFilter]) => {
const filter = {
name: keyFilter,
endpoint: valueFilter.endpoint,
};
return filter;
});
if (filters.length > 0 && page[0] === config) {
pageFilters = filters;
}
});
}
this.setState({ pageFilters });
}
fetchLocations() {
const url = '/openboxes/api/dashboard/fillRateDestinations';
return apiClient.get(url)
.then((response) => {
this.setState({
allLocations: response.data.data
.sort((a, b) => a.id.localeCompare(b.id)),
});
});
}
fetchData = (config = 'personal') => {
sessionStorage.setItem('dashboardKey', config);
this.props.resetIndicators();
if (this.props.dashboardConfig && this.props.dashboardConfig.dashboard) {
this.props.fetchIndicators(
this.props.dashboardConfig,
config,
this.props.currentLocation,
this.props.currentUser,
);
this.loadPageFilters(config);
} else {
this.props.fetchConfigAndData(
this.props.currentLocation,
config,
this.props.currentUser,
// Determine which dashboard (id) to fetch
this.getDashboardIdFromParams(),
);
}
};
updateConfig = () => {
const widgets = [];
_.forEach(this.props.indicatorsData, (widgetData, index) => {
widgets.push({ widgetId: widgetData.widgetId, order: index + 1 });
});
_.forEach(this.props.numberData, (widgetData, index) => {
widgets.push({ widgetId: widgetData.widgetId, order: index + 1 });
});
const url = '/openboxes/api/dashboard/config';
const payload = {
...this.props.dashboardConfig.dashboard,
[this.props.activeConfig]: {
...this.props.dashboardConfig.dashboard[this.props.activeConfig],
widgets,
},
};
apiClient.post(url, payload).then(() => {
this.props.fetchConfig(this.getDashboardIdFromParams());
this.setState({ configModified: false });
});
};
loadIndicator = (widgetId, params) => {
const dashboardConf = this.props.dashboardConfig.dashboard[this.props.activeConfig];
const widget = _.find(dashboardConf.widgets, w => w.widgetId === widgetId);
const widgetConf = {
...this.props.dashboardConfig.dashboardWidgets[widgetId],
...widget,
};
this.props.reloadIndicator(widgetConf, params, this.props.currentLocation);
};
toggleNav = () => {
this.setState({ showNav: !this.state.showNav });
};
sortStartHandle = () => {
this.setState({ isDragging: true });
};
sortEndHandle = ({ oldIndex, newIndex }, e, type) => {
const maxHeight = window.innerHeight - (((6 * window.innerHeight) / 100) + 80);
if (e.clientY > maxHeight) {
e.target.id = 'archive';
}
this.props.reorderIndicators({ oldIndex, newIndex }, e, type);
if (this.props.activeConfig === 'personal' && (oldIndex !== newIndex || e.target.id === 'archive')) {
this.setState({
configModified: true,
isDragging: false,
});
} else {
this.setState({ isDragging: false });
}
};
sortEndHandleNumber = ({ oldIndex, newIndex }, e) => {
this.sortEndHandle({ oldIndex, newIndex }, e, 'number');
};
sortEndHandleGraph = ({ oldIndex, newIndex }, e) => {
this.sortEndHandle({ oldIndex, newIndex }, e, 'graph');
};
unarchiveHandler = () => {
const size = _.size(this.props.dashboardConfig.dashboardWidgets)
- (this.props.indicatorsData.length + this.props.numberData.length);
if (size) {
this.setState({ showPopout: !this.state.showPopout });
} else {
this.setState({ showPopout: false });
}
};
handleAdd = (widgetId) => {
const widget = this.props.dashboardConfig.dashboardWidgets[widgetId];
const widgetConf = {
...widget,
widgetId,
order: (widget.type === 'number' ? this.props.numberData.length : this.props.indicatorsData.length) + 1,
};
this.props.addToIndicators(widgetConf, this.props.currentLocation, this.props.currentUser);
const size = _.size(this.props.dashboardConfig.dashboardWidgets)
- (this.props.indicatorsData.length + this.props.numberData.length);
if (this.props.activeConfig === 'personal') {
this.setState({
configModified: true,
showPopout: (size > 0),
});
} else {
this.setState({ showPopout: (size > 0) });
}
};
isPersonalDashboardActive() {
return this.props.activeConfig === 'personal';
}
render() {
const isPersonalDashboardActive = this.isPersonalDashboardActive();
let numberCards;
if (this.props.numberData.length) {
numberCards = (
<SortableNumberCards
data={this.props.numberData}
onSortStart={this.sortStartHandle}
onSortEnd={this.sortEndHandleNumber}
axis="xy"
useDragHandle
personalDashboardActive={isPersonalDashboardActive}
/>
);
} else {
numberCards = <LoadingNumbers />;
}
return (
<div className="dashboard-container">
<ConfigurationsList
configs={this.props.dashboardConfig.dashboard || {}}
loadConfigData={this.fetchData}
activeConfig={this.props.activeConfig}
showNav={this.state.showNav}
toggleNav={this.toggleNav}
configModified={this.state.configModified}
updateConfig={this.updateConfig}
/>
<div
className={`overlay ${this.state.showNav ? 'visible' : ''}`}
role="button"
tabIndex={0}
onClick={this.toggleNav}
onKeyPress={this.toggleNav}
/>
<div className="filter-and-cards-container">
<Filter
configs={this.props.dashboardConfig.dashboard || {}}
activeConfig={this.props.activeConfig}
fetchData={this.fetchData}
pageFilters={this.state.pageFilters}
/>
<div className="cards-container">
{numberCards}
<SortableCards
allLocations={this.state.allLocations}
data={this.props.indicatorsData.filter(indicator => indicator)}
onSortStart={this.sortStartHandle}
onSortEnd={this.sortEndHandleGraph}
loadIndicator={this.loadIndicator}
axis="xy"
useDragHandle
personalDashboardActive={isPersonalDashboardActive}
/>
<ArchiveIndicator hideArchive={!this.state.isDragging} />
{isPersonalDashboardActive &&
<UnarchiveIndicators
graphData={this.props.indicatorsData}
numberData={this.props.numberData}
dashboardConfig={this.props.dashboardConfig}
activeConfig={this.props.activeConfig}
showPopout={this.state.showPopout}
unarchiveHandler={this.unarchiveHandler}
handleAdd={this.handleAdd}
/>
}
</div>
</div>
</div>
);
}
}
const mapStateToProps = state => ({
indicatorsData: state.indicators.data,
numberData: state.indicators.numberData,
dashboardConfig: state.indicators.config,
activeConfig: state.indicators.activeConfig,
currentLocation: state.session.currentLocation.id,
currentUser: state.session.user.id,
});
export default connect(mapStateToProps, {
fetchIndicators,
reloadIndicator,
addToIndicators,
reorderIndicators,
resetIndicators,
fetchConfigAndData,
fetchConfig,
})(Dashboard);
Dashboard.defaultProps = {
currentLocation: '',
currentUser: '',
indicatorsData: null,
numberData: [],
configModified: false,
match: {
params: { configId: 'personal' },
},
};
Dashboard.propTypes = {
fetchIndicators: PropTypes.func.isRequired,
reorderIndicators: PropTypes.func.isRequired,
indicatorsData: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.number,
})).isRequired,
numberData: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.number,
})).isRequired,
dashboardConfig: PropTypes.shape({
dashboard: PropTypes.shape({}),
dashboardWidgets: PropTypes.shape({}),
}).isRequired,
activeConfig: PropTypes.string.isRequired,
currentLocation: PropTypes.string.isRequired,
currentUser: PropTypes.string.isRequired,
addToIndicators: PropTypes.func.isRequired,
reloadIndicator: PropTypes.func.isRequired,
resetIndicators: PropTypes.func.isRequired,
fetchConfigAndData: PropTypes.func.isRequired,
fetchConfig: PropTypes.func.isRequired,
match: PropTypes.shape({
params: PropTypes.shape({ configId: PropTypes.string }),
}),
};
ArchiveIndicator.propTypes = {
hideArchive: PropTypes.bool.isRequired,
};
ConfigurationsList.propTypes = {
configs: PropTypes.shape({}).isRequired,
activeConfig: PropTypes.string.isRequired,
loadConfigData: PropTypes.func.isRequired,
showNav: PropTypes.bool.isRequired,
toggleNav: PropTypes.func.isRequired,
updateConfig: PropTypes.func.isRequired,
configModified: PropTypes.bool.isRequired,
};