front/src/routes/integration/all/philips-hue/device-page/Device.jsx
import { Text, MarkupText } from 'preact-i18n';
import { Component } from 'preact';
import cx from 'classnames';
import { RequestStatus } from '../../../../../utils/consts';
import DeviceForm from './DeviceForm';
import get from 'get-value';
class PhilipsHueDeviceBox extends Component {
saveDevice = async () => {
this.setState({ loading: true });
try {
await this.props.saveDevice(this.props.device, this.props.deviceIndex);
} catch (e) {
this.setState({ error: RequestStatus.Error });
}
this.setState({ loading: false });
};
deleteDevice = async () => {
this.setState({ loading: true, tooMuchStatesError: false, statesNumber: undefined });
try {
await this.props.deleteDevice(this.props.device, this.props.deviceIndex);
} catch (e) {
const status = get(e, 'response.status');
const dataMessage = get(e, 'response.data.message');
if (status === 400 && dataMessage && dataMessage.includes('Too much states')) {
const statesNumber = new Intl.NumberFormat().format(dataMessage.split(' ')[0]);
this.setState({ tooMuchStatesError: true, statesNumber });
} else {
this.setState({ error: RequestStatus.Error });
}
}
this.setState({ loading: false });
};
render(props, { loading, tooMuchStatesError, statesNumber }) {
return (
<div class="col-md-6">
<div class="card">
<div class="card-header">{props.device.name || <Text id="integration.mqtt.device.noNameLabel" />}</div>
<div
class={cx('dimmer', {
active: loading
})}
>
<div class="loader" />
<div class="dimmer-content">
<div class="card-body">
{tooMuchStatesError && (
<div class="alert alert-warning">
<MarkupText id="device.tooMuchStatesToDelete" fields={{ count: statesNumber }} />
</div>
)}
<DeviceForm {...props} />
<div class="form-group">
<button onClick={this.saveDevice} class="btn btn-success mr-2">
<Text id="integration.mqtt.device.saveButton" />
</button>
<button onClick={this.deleteDevice} class="btn btn-danger mr-2">
<Text id="integration.mqtt.device.deleteButton" />
</button>
</div>
</div>
</div>
</div>
</div>
</div>
);
}
}
export default PhilipsHueDeviceBox;