Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
#945 Updating charts on websocket events
Browse files Browse the repository at this point in the history
  • Loading branch information
damianprzygodzki committed Jun 27, 2017
1 parent 3d4ec7a commit 0424c8e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/components/dashboard/DndWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const cardTarget = {
drop(props, monitor) {
if(monitor.getItem().isNew && props.addCard){
props.addCard(props.entity, monitor.getItem().id);
}else{
props.onDrop && props.onDrop(props.entity, monitor.getItem().id)
}
}
};
Expand Down
51 changes: 49 additions & 2 deletions src/components/dashboard/DraggableWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,24 @@ import {
removeDashboardWidget
} from '../../actions/BoardActions';

import {
connectWS,
disconnectWS
} from '../../actions/WindowActions';

import {
patchRequest
} from '../../actions/GenericActions';

export class DraggableWrapper extends Component {
constructor(props) {
super(props);
this.state = {
cards: [],
indicators: [],
sidenav: null,
idMaximized: null
idMaximized: null,
websocketEndpoint: null
};
}

Expand All @@ -38,6 +48,31 @@ export class DraggableWrapper extends Component {
this.getIndicators();
}

componentDidUpdate = (prevProps, prevState) => {
const {websocketEndpoint} = this.state;
if(
websocketEndpoint !== null &&
prevState.websocketEndpoint !== websocketEndpoint
){
connectWS.call(this, websocketEndpoint, msg => {
msg.events.map(event => {
switch(event.widgetType){
case 'TargetIndicator':
this.getIndicators();
break;
case 'KPI':
this.getDashboard();
break;
}
})
})
}
}

componentWillUnmount = () => {
disconnectWS.call(this);
}

getType = (entity) => entity === 'cards' ? 'kpis' : 'targetIndicators';

getIndicators = () => {
Expand All @@ -51,7 +86,8 @@ export class DraggableWrapper extends Component {
getDashboard = () => {
getKPIsDashboard().then(response => {
this.setState({
cards: response.data.items
cards: response.data.items,
websocketEndpoint: response.data.websocketEndpoint
});
});
}
Expand All @@ -67,6 +103,15 @@ export class DraggableWrapper extends Component {
});
}

onDrop = (entity, id) => {
console.log(entity, id);
const tmpItemIndex = this.state[entity].findIndex(i => i.id === id);
patchRequest(
'dashboard', null, null, null, null, 'position',
tmpItemIndex, this.getType(entity), id
);
}

moveCard = (entity, dragIndex, hoverIndex, item) => {
const draggedItem = this.state[entity][dragIndex];
if(draggedItem){
Expand Down Expand Up @@ -149,6 +194,7 @@ export class DraggableWrapper extends Component {
index={id}
moveCard={this.moveCard}
addCard={this.addCard}
onDrop={this.onDrop}
removeCard={this.removeCard}
entity={'indicators'}
transparent={!editmode}
Expand Down Expand Up @@ -200,6 +246,7 @@ export class DraggableWrapper extends Component {
id={item.id}
moveCard={this.moveCard}
addCard={this.addCard}
onDrop={this.onDrop}
removeCard={this.removeCard}
entity={'cards'}
className={
Expand Down

0 comments on commit 0424c8e

Please sign in to comment.