front/src/routes/integration/all/ewelink/EweLinkDeviceBox.jsx
import { Component } from 'preact';
import { Text, Localizer, MarkupText } from 'preact-i18n';
import cx from 'classnames';
import { Link } from 'preact-router';
import get from 'get-value';
import { DEVICE_FIRMWARE, DEVICE_ONLINE } from '../../../../../../server/services/ewelink/lib/utils/constants';
import DeviceFeatures from '../../../../components/device/view/DeviceFeatures';
class EweLinkDeviceBox extends Component {
updateName = e => {
this.props.updateDeviceField(this.props.listName, this.props.deviceIndex, 'name', e.target.value);
};
updateRoom = e => {
this.props.updateDeviceField(this.props.listName, this.props.deviceIndex, 'room_id', e.target.value);
};
saveDevice = async () => {
this.setState({
loading: true,
errorMessage: null
});
try {
await this.props.saveDevice(this.props.listName, this.props.deviceIndex);
} catch (e) {
let errorMessage = 'integration.eWeLink.error.defaultError';
if (e.response.status === 409) {
errorMessage = 'integration.eWeLink.error.conflictError';
}
this.setState({
errorMessage
});
}
this.setState({
loading: false
});
};
deleteDevice = async () => {
this.setState({
loading: true,
errorMessage: null,
tooMuchStatesError: false,
statesNumber: undefined
});
try {
await this.props.deleteDevice(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({
errorMessage: 'integration.eWeLink.error.defaultDeletionError'
});
}
}
this.setState({
loading: false
});
};
render(
{ deviceIndex, device, housesWithRooms, editable, ...props },
{ loading, errorMessage, tooMuchStatesError, statesNumber }
) {
const validModel = device.features && device.features.length > 0;
const online = device.params.find(param => param.name === DEVICE_ONLINE).value === '1';
const firmware = device.params.find(param => param.name === DEVICE_FIRMWARE).value;
return (
<div class="col-md-6">
<div class="card">
<div class="card-header">
<Localizer>
<div title={<Text id={`integration.eWeLink.status.${online ? 'online' : 'offline'}`} />}>
<i class={`fe fe-radio text-${online ? 'success' : 'danger'}`} />
{device.name}
</div>
</Localizer>
<div class="page-options d-flex">
{device.params.find(param => param.name === DEVICE_FIRMWARE) && (
<div class="tag tag-blue">{`Firmware: ${firmware}`}</div>
)}
</div>
</div>
<div
class={cx('dimmer', {
active: loading
})}
>
<div class="loader" />
<div class="dimmer-content">
<div class="card-body">
{errorMessage && (
<div class="alert alert-danger">
<Text id={errorMessage} />
</div>
)}
{tooMuchStatesError && (
<div class="alert alert-warning">
<MarkupText id="device.tooMuchStatesToDelete" fields={{ count: statesNumber }} />
</div>
)}
<div class="form-group">
<label class="form-label" for={`name_${deviceIndex}`}>
<Text id="integration.eWeLink.nameLabel" />
</label>
<Localizer>
<input
id={`name_${deviceIndex}`}
type="text"
value={device.name}
onInput={this.updateName}
class="form-control"
placeholder={<Text id="integration.eWeLink.namePlaceholder" />}
disabled={!editable || !validModel}
/>
</Localizer>
</div>
<div class="form-group">
<label class="form-label" for={`model_${deviceIndex}`}>
<Text id="integration.eWeLink.modelLabel" />
</label>
<input
id={`model_${deviceIndex}`}
type="text"
value={device.model}
class="form-control"
disabled="true"
/>
</div>
<div class="form-group">
<label class="form-label" for={`room_${deviceIndex}`}>
<Text id="integration.eWeLink.roomLabel" />
</label>
<select
id={`room_${deviceIndex}`}
onChange={this.updateRoom}
class="form-control"
disabled={!editable || !validModel}
>
<option value="">
<Text id="global.emptySelectOption" />
</option>
{housesWithRooms &&
housesWithRooms.map(house => (
<optgroup label={house.name}>
{house.rooms.map(room => (
<option selected={room.id === device.room_id} value={room.id}>
{room.name}
</option>
))}
</optgroup>
))}
</select>
</div>
{validModel && (
<div class="form-group">
<label class="form-label">
<Text id="integration.eWeLink.device.featuresLabel" />
</label>
<DeviceFeatures features={device.features} />
</div>
)}
<div class="form-group">
{validModel && props.alreadyCreatedButton && (
<button class="btn btn-primary mr-2" disabled="true">
<Text id="integration.eWeLink.alreadyCreatedButton" />
</button>
)}
{validModel && props.updateButton && (
<button onClick={this.saveDevice} class="btn btn-success mr-2">
<Text id="integration.eWeLink.updateButton" />
</button>
)}
{validModel && props.saveButton && (
<button onClick={this.saveDevice} class="btn btn-success mr-2">
<Text id="integration.eWeLink.saveButton" />
</button>
)}
{validModel && props.deleteButton && (
<button onClick={this.deleteDevice} class="btn btn-danger">
<Text id="integration.eWeLink.deleteButton" />
</button>
)}
{!validModel && (
<button class="btn btn-dark" disabled>
<Text id="integration.eWeLink.unmanagedModelButton" />
</button>
)}
{validModel && props.editButton && (
<Link href={`/dashboard/integration/device/ewelink/edit/${device.selector}`}>
<button class="btn btn-secondary float-right">
<Text id="integration.eWeLink.device.editButton" />
</button>
</Link>
)}
</div>
</div>
</div>
</div>
</div>
</div>
);
}
}
export default EweLinkDeviceBox;