Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dashboard improvements #1022

Merged
merged 8 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gsa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"i18next": "^11.3.3",
"i18next-xhr-backend": "1.5.1",
"ical.js": "^1.2.2",
"memoize-one": "^4.0.2",
"moment": "^2.22.2",
"moment-timezone": "^0.5.21",
"prop-types": "^15.6.2",
Expand Down
7 changes: 6 additions & 1 deletion gsa/src/web/components/chart/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class LineChart extends React.Component {
nextState.infoX !== this.state.infoX ||
nextState.mouseX !== this.state.mouseX ||
nextState.mouseY !== this.state.mouseY ||
nextState.data !== this.state.data ||
nextState.displayInfo !== this.state.displayInfo;
}

Expand Down Expand Up @@ -301,7 +302,10 @@ class LineChart extends React.Component {

update() {
const width = this.getWidth();
if (width !== this.state.width) {
if (width !== this.state.width ||
this.props.data !== this.state.data ||
this.props.height !== this.state.height) {
// update state if width, data or height has changed since last render
this.setState(this.stateFromWidth(width));
}
}
Expand Down Expand Up @@ -361,6 +365,7 @@ class LineChart extends React.Component {
.nice();

return {
data,
xScale,
yScale,
y2Scale,
Expand Down
101 changes: 69 additions & 32 deletions gsa/src/web/components/dashboard/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import 'core-js/fn/array/find-index';
import 'core-js/fn/object/entries';

import memoize from 'memoize-one';

import React from 'react';

import {connect} from 'react-redux';
Expand Down Expand Up @@ -114,6 +116,8 @@ export class Dashboard extends React.Component {
this.handleRowResize = this.handleRowResize.bind(this);
this.handleUpdateDisplay = this.handleUpdateDisplay.bind(this);
this.handleRemoveDisplay = this.handleRemoveDisplay.bind(this);

this.getDisplaysById = memoize(rows => getDisplaysById(rows));
}

componentDidMount() {
Expand All @@ -140,37 +144,13 @@ export class Dashboard extends React.Component {
handleItemsChange(gridItems = []) {
const rows = this.getRows();

const displaysById = getDisplaysById(rows);
const displaysById = this.getDisplaysById(rows);

this.updateRows(convertGridItemsToDisplays(gridItems, displaysById));
}

handleUpdateDisplay(id, props) {
const rows = this.getRows();

const rowIndex = rows.findIndex(
row => row.items.some(item => item.id === id));

const row = rows[rowIndex];

const rowItems = [
...row.items,
];

const displayIndex = rowItems.findIndex(i => i.id === id);

rowItems[displayIndex] = {
...rowItems[displayIndex],
...props,
};

const newRows = [...rows];
newRows[rowIndex] = {
...row,
rows: rowItems,
};

this.updateRows(newRows);
this.updateDisplay(id, props);
}

handleRemoveDisplay(id) {
Expand All @@ -188,18 +168,15 @@ export class Dashboard extends React.Component {
const newRows = [
...rows,
];
newRows[rowIndex] = {
const newRow = {
...row,
height,
};
newRows[rowIndex] = newRow;

this.updateRows(newRows);
}

getRows(defaultRows) {
return getRows(this.props.settings, defaultRows);
}

handleInteraction() {
const {onInteraction} = this.props;

Expand All @@ -208,6 +185,63 @@ export class Dashboard extends React.Component {
}
}

handleSetDisplayState(id, stateFunc) {
const currentState = this.getDisplayState(id);
const newState = stateFunc(currentState);

this.updateDisplayState(id, {
...currentState,
...newState,
});
};

getRows(defaultRows) {
return getRows(this.props.settings, defaultRows);
}

getDisplayState(id) {
const rows = this.getRows();
const displaysById = this.getDisplaysById(rows);
const display = displaysById[id];
return isDefined(display) ? display.state : undefined;
}

updateDisplayState(id, state) {
this.updateDisplay(id, {state});
}

updateDisplay(id, props) {
const rows = this.getRows();

const rowIndex = rows.findIndex(
row => row.items.some(item => item.id === id));

const row = rows[rowIndex];

const rowItems = [
...row.items,
];

const displayIndex = rowItems.findIndex(i => i.id === id);

const newDisplay = {
...rowItems[displayIndex],
...props,
};

rowItems[displayIndex] = newDisplay;

const newRows = [...rows];
const newRow = {
...row,
items: rowItems,
};

newRows[rowIndex] = newRow;

this.updateRows(newRows);
}

updateRows(rows) {
this.save({rows});
}
Expand Down Expand Up @@ -245,7 +279,7 @@ export class Dashboard extends React.Component {
);
}

const displaysById = getDisplaysById(rows);
const displaysById = this.getDisplaysById(rows);

const getDisplayComponent = displayId => this.components[displayId];
const getDisplaySettings = id => displaysById[id];
Expand Down Expand Up @@ -273,6 +307,7 @@ export class Dashboard extends React.Component {
}) => {
const {displayId, ...displayProps} = getDisplaySettings(id);
const Component = getDisplayComponent(displayId);
const state = this.getDisplayState(id);
return (
<Component
{...other}
Expand All @@ -281,6 +316,8 @@ export class Dashboard extends React.Component {
height={height}
width={width}
id={id}
state={state}
setState={stateFunc => this.handleSetDisplayState(id, stateFunc)}
onFilterIdChanged={
filterId => this.handleUpdateDisplay(id, {filterId})}
onInteractive={this.props.onInteraction}
Expand Down
47 changes: 18 additions & 29 deletions gsa/src/web/components/dashboard/display/datadisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import equal from 'fast-deep-equal';

import _ from 'gmp/locale';

import {isDefined, isFunction} from 'gmp/utils/identity';
import {isDefined} from 'gmp/utils/identity';
import {excludeObjectProps} from 'gmp/utils/object';

import IconDivider from 'web/components/layout/icondivider';
Expand Down Expand Up @@ -125,15 +125,11 @@ class DataDisplay extends React.Component {
data,
originalData: this.props.data,
title: this.props.title({data, id: this.props.id}),
childState: {
showLegend: true,
...this.props.initialState,
},
};

this.handleDownloadSvg = this.handleDownloadSvg.bind(this);
this.handleDownloadCsv = this.handleDownloadCsv.bind(this);
this.handleSetChildState = this.setChildState.bind(this);
this.handleSetState = this.handleSetState.bind(this);
}

static getDerivedStateFromProps(nextProps, prevState) {
Expand Down Expand Up @@ -171,7 +167,7 @@ class DataDisplay extends React.Component {
nextProps.width !== this.props.width ||
nextState.data !== this.state.data ||
nextProps.showFilterString !== this.props.showFilterString ||
nextState.childState !== this.state.childState ||
nextProps.state !== this.props.state ||
this.hasFilterChanged(nextProps);
}

Expand Down Expand Up @@ -215,23 +211,12 @@ class DataDisplay extends React.Component {
}
}

setChildState(state) {
if (isFunction(state)) {
this.setState(({childState}) => ({
childState: {
...childState,
...state(childState),
},
}));
}
else {
this.setState(({childState}) => ({
childState: {
...childState,
...state,
},
}));
}
getCurrentState(state = this.props.state) {
return {
showLegend: true,
...this.props.initialState,
...state,
};
}

handleDownloadSvg() {
Expand Down Expand Up @@ -274,9 +259,12 @@ class DataDisplay extends React.Component {
download.click();
}

handleSetState(stateFunc) {
return this.props.setState(state => stateFunc(this.getCurrentState(state)));
}

render() {
const {
childState,
data: transformedData,
title,
} = this.state;
Expand Down Expand Up @@ -316,6 +304,7 @@ class DataDisplay extends React.Component {
}

const showContent = height > 0 && width > 0; // > 0 also checks for null, undefined and null
const state = this.getCurrentState();
return (
<Display
title={`${title}`}
Expand All @@ -334,16 +323,16 @@ class DataDisplay extends React.Component {
width,
height,
svgRef: this.svgRef,
state: childState,
setState: this.handleSetChildState,
state,
setState: this.handleSetState,
})}
</React.Fragment>
}
<IconBar>
<IconDivider flex="column">
{icons && icons({
state: childState,
setState: this.handleSetChildState,
state,
setState: this.handleSetState,
showFilterSelection,
showCsvDownload,
showSvgDownload,
Expand Down
4 changes: 4 additions & 0 deletions gsa/src/web/pages/start/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const StartDashboard = ({
id,
loadSettings,
saveSettings,
settings,
onInteraction,
onNewDisplay,
onResetDashboard,
Expand All @@ -78,6 +79,7 @@ const StartDashboard = ({
<Layout flex="column" grow>
<Layout align="end">
<DashboardControls
settings={settings}
canAdd={canAddDisplay(props)}
dashboardId={id}
displayIds={ALL_DISPLAYS}
Expand All @@ -89,6 +91,8 @@ const StartDashboard = ({
<Dashboard
{...props}
id={id}
isLoading={false}
settings={settings}
showFilterSelection
showFilterString
defaultDisplays={DEFAULT_DISPLAYS}
Expand Down
Loading