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

Fix update devices list into devices widget #1822

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 28 additions & 5 deletions front/src/components/boxs/device-in-room/EditDevices.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class EditDevices extends Component {
// we get the rooms with the devices
const devices = await this.props.httpClient.get(`/api/v1/device`);
const deviceOptions = [];
const selectedDeviceFeaturesOptions = [];

devices.forEach(device => {
const deviceFeatures = [];
Expand All @@ -41,9 +40,6 @@ class EditDevices extends Component {
if (feature.read_only || SUPPORTED_FEATURE_TYPES.includes(feature.type)) {
deviceFeatures.push(featureOption);
}
if (this.props.box.device_features && this.props.box.device_features.indexOf(feature.selector) !== -1) {
selectedDeviceFeaturesOptions.push(featureOption);
}
});
if (deviceFeatures.length > 0) {
deviceFeatures.sort((a, b) => {
Expand All @@ -60,17 +56,44 @@ class EditDevices extends Component {
});
}
});
await this.setState({ deviceOptions, selectedDeviceFeaturesOptions, loading: false });
await this.setState({ deviceOptions, loading: false });
await this.refreshSelectedOptions(this.props);
} catch (e) {
console.error(e);
this.setState({ loading: false });
}
};

refreshSelectedOptions = async props => {
const selectedDeviceFeaturesOptions = [];
if (this.state.deviceOptions) {
this.state.deviceOptions.forEach(deviceOption => {
deviceOption.options.forEach(featureOption => {
if (props.box.device_features && props.box.device_features.indexOf(featureOption.value) !== -1) {
selectedDeviceFeaturesOptions.push(featureOption);
}
});
});
}
await this.setState({ selectedDeviceFeaturesOptions });
};

componentDidMount() {
this.getDeviceFeatures();
}

componentWillReceiveProps(nextProps) {
if (nextProps.box && nextProps.box.device_features) {
if (
!this.props.box ||
!this.props.box.device_features ||
nextProps.box.device_features !== this.props.box.device_features
) {
this.refreshSelectedOptions(nextProps);
}
}
}

render(props, { selectedDeviceFeaturesOptions, deviceOptions, loading }) {
return (
<BaseEditBox {...props} titleKey="dashboard.boxTitle.devices">
Expand Down