|
| 1 | +import { Component } from 'preact'; |
| 2 | +import { Localizer, Text } from 'preact-i18n'; |
| 3 | +import BaseEditBox from '../baseEditBox'; |
| 4 | +import withIntlAsProp from '../../../utils/withIntlAsProp'; |
| 5 | +import { connect } from 'unistore/preact'; |
| 6 | +import Select from 'react-select'; |
| 7 | +import { RequestStatus } from '../../../utils/consts'; |
| 8 | + |
| 9 | +class EditSceneBox extends Component { |
| 10 | + updateScenes = selectedSceneOptions => { |
| 11 | + selectedSceneOptions = selectedSceneOptions || []; |
| 12 | + const selectedScenes = selectedSceneOptions.map(option => option.value); |
| 13 | + this.props.updateBoxConfig(this.props.x, this.props.y, { |
| 14 | + scenes: selectedScenes |
| 15 | + }); |
| 16 | + this.setState({ selectedSceneOptions }); |
| 17 | + }; |
| 18 | + |
| 19 | + updateName = e => { |
| 20 | + this.props.updateBoxConfig(this.props.x, this.props.y, { |
| 21 | + name: e.target.value |
| 22 | + }); |
| 23 | + }; |
| 24 | + |
| 25 | + getScenes = async () => { |
| 26 | + try { |
| 27 | + this.setState({ status: RequestStatus.Getting }); |
| 28 | + const params = { |
| 29 | + order_dir: 'asc' |
| 30 | + }; |
| 31 | + const sceneOptions = []; |
| 32 | + const scenes = await this.props.httpClient.get(`/api/v1/scene`, params); |
| 33 | + scenes.forEach(scene => { |
| 34 | + const sceneOption = { |
| 35 | + value: scene.selector, |
| 36 | + label: scene.name |
| 37 | + }; |
| 38 | + sceneOptions.push(sceneOption); |
| 39 | + }); |
| 40 | + |
| 41 | + await this.setState({ |
| 42 | + sceneOptions, |
| 43 | + status: RequestStatus.Success |
| 44 | + }); |
| 45 | + |
| 46 | + await this.refreshSelectedOptions(this.props); |
| 47 | + } catch (e) { |
| 48 | + this.setState({ |
| 49 | + status: RequestStatus.Error |
| 50 | + }); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + refreshSelectedOptions = async props => { |
| 55 | + const selectedSceneOptions = []; |
| 56 | + if (this.state.sceneOptions) { |
| 57 | + this.state.sceneOptions.forEach(sceneOption => { |
| 58 | + if (props.box.scenes && props.box.scenes.indexOf(sceneOption.value) !== -1) { |
| 59 | + selectedSceneOptions.push(sceneOption); |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + await this.setState({ selectedSceneOptions }); |
| 64 | + }; |
| 65 | + |
| 66 | + componentDidMount = () => { |
| 67 | + this.getScenes(); |
| 68 | + }; |
| 69 | + |
| 70 | + componentWillReceiveProps(nextProps) { |
| 71 | + if (nextProps.box && nextProps.box.scenes) { |
| 72 | + if (!this.props.box || !this.props.box.scenes || nextProps.box.scenes !== this.props.box.scenes) { |
| 73 | + this.refreshSelectedOptions(nextProps); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + render(props, { status, selectedSceneOptions, sceneOptions }) { |
| 78 | + const loading = status === RequestStatus.Getting && !status; |
| 79 | + return ( |
| 80 | + <BaseEditBox {...props} titleKey="dashboard.boxTitle.scene"> |
| 81 | + <div class={loading ? 'dimmer active' : 'dimmer'}> |
| 82 | + <div class="loader" /> |
| 83 | + <div class="dimmer-content"> |
| 84 | + <div class="form-group"> |
| 85 | + <label> |
| 86 | + <Text id="dashboard.boxes.scene.editNameLabel" /> |
| 87 | + </label> |
| 88 | + <Localizer> |
| 89 | + <input |
| 90 | + type="text" |
| 91 | + className="form-control" |
| 92 | + placeholder={<Text id="dashboard.boxes.scene.editNamePlaceholder" />} |
| 93 | + value={props.box.name} |
| 94 | + onInput={this.updateName} |
| 95 | + /> |
| 96 | + </Localizer> |
| 97 | + </div> |
| 98 | + {sceneOptions && ( |
| 99 | + <div class="form-group"> |
| 100 | + <label> |
| 101 | + <Text id="dashboard.boxes.scene.editSceneLabel" /> |
| 102 | + </label> |
| 103 | + <Select |
| 104 | + defaultValue={[]} |
| 105 | + value={selectedSceneOptions} |
| 106 | + options={sceneOptions} |
| 107 | + isMulti |
| 108 | + onChange={this.updateScenes} |
| 109 | + maxMenuHeight={220} |
| 110 | + /> |
| 111 | + </div> |
| 112 | + )} |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + </BaseEditBox> |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +export default withIntlAsProp(connect('httpClient', {})(EditSceneBox)); |
0 commit comments