Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
fix bug for editing device Tags (#669)
Browse files Browse the repository at this point in the history
* fix for update Tags

* update

* update

* update Rx

* update selected devices

* update device

* update filter
  • Loading branch information
maple10001 authored and spryor committed Oct 20, 2017
1 parent 62e13a8 commit 1cad871
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/components/flyout/deviceReconfigureFlyout.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class DeviceReconfigureFlyout extends React.Component {
checkJobStatus (devices, propertyUpdateJobs) {
if(!devices || !propertyUpdateJobs || !devices.length || !propertyUpdateJobs.length) return;
const jobs = getRelatedJobs(devices, propertyUpdateJobs);
const deviceIdSet = new Set(devices.map(({Id}) => Id));
Rx.Observable.from(jobs)
.flatMap(({ jobId, deviceIds }) =>
Rx.Observable
Expand All @@ -73,6 +74,7 @@ class DeviceReconfigureFlyout extends React.Component {
.flatMap(_ => deviceIds)
)
.distinct()
.filter(deviceId => deviceIdSet.has(deviceId))
.flatMap(deviceId =>
Rx.Observable
.fromPromise(ApiService.getDeviceById(deviceId))
Expand Down
40 changes: 21 additions & 19 deletions src/components/flyout/deviceTagFlyout.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DeviceTagFlyout extends React.Component {
this.inputReferences = {};
this.state = {
commonTags: [],
deletedTagNames: [],
deletedTagNames: {},
jobInputValue: '',
overiddenDeviceTagValues: {
//key will be device Id and value will be tag name/value map
Expand Down Expand Up @@ -145,21 +145,26 @@ class DeviceTagFlyout extends React.Component {
}

checkJobStatus (devices, twinUpdateJobs) {
if(!devices || !twinUpdateJobs || !devices.length || !twinUpdateJobs.length) return;
const jobs = getRelatedJobs(devices, twinUpdateJobs);
const jobStream = Rx.Observable.from(jobs);
jobStream
const deviceIdSet = new Set(devices.map(({Id}) => Id));
Rx.Observable.from(jobs)
.flatMap(({ jobId, deviceIds }) =>
Rx.Observable
.fromPromise(ApiService.getJobStatus(jobId))
// Get completed jobs
.filter(({ status }) => status === 3)
.map(({ updateTwin }) => ({
tags: updateTwin.tags,
deviceIds
}))
.flatMap(_ => deviceIds)
)
.distinct()
.filter(deviceId => deviceIdSet.has(deviceId))
.flatMap(deviceId =>
Rx.Observable
.fromPromise(ApiService.getDeviceById(deviceId))
)
.reduce((devices, device) => [...devices, device], [])
.subscribe(
completedJobs => this.props.actions.updateDeviceTwin(completedJobs),
devices => this.props.actions.updateDevices(devices),
error => console.log('error', error)
);
}
Expand Down Expand Up @@ -213,19 +218,13 @@ class DeviceTagFlyout extends React.Component {

applyDeviceTagJobsData() {
const { devices } = this.props;
const { newTags, deletedTagNames } = this.state;
const { newTags, deletedTagNames, commonTagValues } = this.state;
const deviceIds = devices.map(({ Id }) => `'${Id}'`).join(',');
const tags = {
...this.state.commonTagValues,
...this.state.overiddenDeviceTagValues
...commonTagValues,
...deletedTagNames,
};

Object.keys(tags).forEach(key => {
if (deletedTagNames.indexOf(key) !== -1) {
tags[key] = null;
}
});

newTags.forEach(tag => {
tags[tag.name] = tag.value;
});
Expand Down Expand Up @@ -305,7 +304,7 @@ class DeviceTagFlyout extends React.Component {
const { commonTags, commonTagValues, commonTagTypes, deletedTagNames } = this.state;
return (
<div className="common-tags">
{commonTags.filter(tagName => deletedTagNames.indexOf(tagName) === -1).map((tagName, idx) => {
{commonTags.filter(tagName => !(tagName in deletedTagNames)).map((tagName, idx) => {
return (
<div className="device-tag-items name-value-type" key={tagName} onClick={() => {this.inputReferences[idx] && this.inputReferences[idx].focus()}}>
<span className="device-tag">
Expand Down Expand Up @@ -342,7 +341,10 @@ class DeviceTagFlyout extends React.Component {

deleteExistingTag(tagName) {
this.setState({
deletedTagNames: [...this.state.deletedTagNames, tagName]
deletedTagNames: {
...this.state.deletedTagNames,
[tagName]: null
}
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/reducers/flyoutReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const flyoutReducer = (state = initialState.flyout, action) => {
return {
...state,
devices: [
...state.devices.filter(device => action.devices.some(({ Id }) => device.Id !== Id)),
...action.devices
...state.devices.filter(device => action.devices.every(({ Id }) => device.Id !== Id)),
...action.devices.filter(device => state.devices.some(({ Id }) => device.Id === Id)),
]
}

Expand Down

0 comments on commit 1cad871

Please sign in to comment.