-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into new-integration-freemobile
- Loading branch information
Showing
33 changed files
with
4,547 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
front/src/routes/integration/all/airplay/AirplayDeviceBox.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
import { Component } from 'preact'; | ||
import { Text, Localizer, MarkupText } from 'preact-i18n'; | ||
import cx from 'classnames'; | ||
import get from 'get-value'; | ||
|
||
import { connect } from 'unistore/preact'; | ||
|
||
class AirplayDeviceBox extends Component { | ||
componentWillMount() { | ||
this.setState({ | ||
device: this.props.device | ||
}); | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
this.setState({ | ||
device: nextProps.device | ||
}); | ||
} | ||
|
||
updateName = e => { | ||
this.setState({ | ||
device: { | ||
...this.state.device, | ||
name: e.target.value | ||
} | ||
}); | ||
}; | ||
|
||
updateRoom = e => { | ||
this.setState({ | ||
device: { | ||
...this.state.device, | ||
room_id: e.target.value | ||
} | ||
}); | ||
}; | ||
|
||
saveDevice = async () => { | ||
this.setState({ | ||
loading: true, | ||
errorMessage: null | ||
}); | ||
try { | ||
let deviceDidNotExist = this.state.device.id === undefined; | ||
const savedDevice = await this.props.httpClient.post(`/api/v1/device`, this.state.device); | ||
if (deviceDidNotExist) { | ||
savedDevice.alreadyExist = true; | ||
} | ||
this.setState({ | ||
device: savedDevice | ||
}); | ||
} catch (e) { | ||
let errorMessage = 'integration.airplay.error.defaultError'; | ||
if (e.response.status === 409) { | ||
errorMessage = 'integration.airplay.error.conflictError'; | ||
} | ||
this.setState({ | ||
errorMessage | ||
}); | ||
} | ||
this.setState({ | ||
loading: false | ||
}); | ||
}; | ||
|
||
deleteDevice = async () => { | ||
this.setState({ | ||
loading: true, | ||
errorMessage: null, | ||
tooMuchStatesError: false, | ||
statesNumber: undefined | ||
}); | ||
try { | ||
if (this.state.device.created_at) { | ||
await this.props.httpClient.delete(`/api/v1/device/${this.state.device.selector}`); | ||
} | ||
this.props.getAirplayDevices(); | ||
} 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.airplay.error.defaultDeletionError' | ||
}); | ||
} | ||
} | ||
this.setState({ | ||
loading: false | ||
}); | ||
}; | ||
|
||
render( | ||
{ deviceIndex, editable, deleteButton, housesWithRooms }, | ||
{ device, loading, errorMessage, tooMuchStatesError, statesNumber } | ||
) { | ||
const validModel = device.features && device.features.length > 0; | ||
|
||
return ( | ||
<div class="col-md-6"> | ||
<div class="card"> | ||
<div class="card-header">{device.name}</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.airplay.nameLabel" /> | ||
</label> | ||
<Localizer> | ||
<input | ||
id={`name_${deviceIndex}`} | ||
type="text" | ||
value={device.name} | ||
onInput={this.updateName} | ||
class="form-control" | ||
placeholder={<Text id="integration.airplay.namePlaceholder" />} | ||
disabled={!editable || !validModel} | ||
/> | ||
</Localizer> | ||
</div> | ||
|
||
{housesWithRooms && ( | ||
<div class="form-group"> | ||
<label class="form-label" for={`room_${deviceIndex}`}> | ||
<Text id="integration.airplay.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> | ||
)} | ||
|
||
<div class="form-group"> | ||
{device.alreadyExist && ( | ||
<button class="btn btn-primary mr-2" disabled="true"> | ||
<Text id="integration.airplay.alreadyCreatedButton" /> | ||
</button> | ||
)} | ||
|
||
{!device.alreadyExist && ( | ||
<button onClick={this.saveDevice} class="btn btn-success mr-2"> | ||
<Text id="integration.airplay.saveButton" /> | ||
</button> | ||
)} | ||
|
||
{deleteButton && ( | ||
<button onClick={this.deleteDevice} class="btn btn-danger"> | ||
<Text id="integration.airplay.deleteButton" /> | ||
</button> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default connect('httpClient', {})(AirplayDeviceBox); |
Oops, something went wrong.