-
-
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.
- Loading branch information
1 parent
8f5c259
commit 4eca352
Showing
10 changed files
with
323 additions
and
13 deletions.
There are no files selected for viewing
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
123 changes: 123 additions & 0 deletions
123
front/src/routes/scene/edit-scene/actions/SetAlarmMode.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,123 @@ | ||
import Select from 'react-select'; | ||
import { Component } from 'preact'; | ||
import { connect } from 'unistore/preact'; | ||
import { Text } from 'preact-i18n'; | ||
import withIntlAsProp from '../../../../utils/withIntlAsProp'; | ||
import get from 'get-value'; | ||
|
||
import { ALARM_MODES_LIST } from '../../../../../../server/utils/constants'; | ||
|
||
const capitalizeFirstLetter = string => { | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
}; | ||
|
||
class SetAlarmMode extends Component { | ||
getOptions = async () => { | ||
try { | ||
const houses = await this.props.httpClient.get('/api/v1/house'); | ||
const houseOptions = []; | ||
houses.forEach(house => { | ||
houseOptions.push({ | ||
label: house.name, | ||
value: house.selector | ||
}); | ||
}); | ||
await this.setState({ houseOptions }); | ||
this.refreshSelectedOptions(this.props); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
handleHouseChange = selectedOption => { | ||
if (selectedOption && selectedOption.value) { | ||
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'house', selectedOption.value); | ||
} else { | ||
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'house', null); | ||
} | ||
}; | ||
handleAlarmModeChange = selectedOption => { | ||
if (selectedOption && selectedOption.value) { | ||
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'alarm_mode', selectedOption.value); | ||
} else { | ||
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'alarm_mode', null); | ||
} | ||
}; | ||
refreshSelectedOptions = nextProps => { | ||
let selectedHouseOption = ''; | ||
if (nextProps.action.house && this.state.houseOptions) { | ||
const houseOption = this.state.houseOptions.find(option => option.value === nextProps.action.house); | ||
|
||
if (houseOption) { | ||
selectedHouseOption = houseOption; | ||
} | ||
} | ||
let selectedAlarmModeOption = ''; | ||
if (nextProps.action.alarm_mode && this.state.alarmModesOptions) { | ||
const alarmModeOption = this.state.alarmModesOptions.find(option => option.value === nextProps.action.alarm_mode); | ||
|
||
if (alarmModeOption) { | ||
selectedAlarmModeOption = alarmModeOption; | ||
} | ||
} | ||
this.setState({ selectedHouseOption, selectedAlarmModeOption }); | ||
}; | ||
constructor(props) { | ||
super(props); | ||
this.props = props; | ||
const alarmModesOptions = ALARM_MODES_LIST.map(alarmMode => { | ||
return { | ||
value: alarmMode, | ||
label: capitalizeFirstLetter(get(props.intl.dictionary, `alarmModes.${alarmMode}`, { default: alarmMode })) | ||
}; | ||
}); | ||
this.state = { | ||
alarmModesOptions, | ||
selectedHouseOption: '' | ||
}; | ||
} | ||
componentDidMount() { | ||
this.getOptions(); | ||
} | ||
componentWillReceiveProps(nextProps) { | ||
this.refreshSelectedOptions(nextProps); | ||
} | ||
render(props, { alarmModesOptions, houseOptions, selectedHouseOption, selectedAlarmModeOption }) { | ||
return ( | ||
<div> | ||
<p> | ||
<Text id="editScene.actionsCard.alarmSetMode.description" /> | ||
</p> | ||
<div class="form-group"> | ||
<label class="form-label"> | ||
<Text id="editScene.actionsCard.alarmSetMode.houseLabel" /> | ||
<span class="form-required"> | ||
<Text id="global.requiredField" /> | ||
</span> | ||
</label> | ||
<Select | ||
options={houseOptions} | ||
class="scene-check-alarm-mode-choose-house" | ||
value={selectedHouseOption} | ||
onChange={this.handleHouseChange} | ||
/> | ||
</div> | ||
<div class="form-group"> | ||
<label class="form-label"> | ||
<Text id="editScene.actionsCard.alarmSetMode.alarmModeLabel" /> | ||
<span class="form-required"> | ||
<Text id="global.requiredField" /> | ||
</span> | ||
</label> | ||
<Select | ||
options={alarmModesOptions} | ||
class="scene-check-alarm-mode-choose-alarm-mode" | ||
value={selectedAlarmModeOption} | ||
onChange={this.handleAlarmModeChange} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default withIntlAsProp(connect('httpClient', {})(SetAlarmMode)); |
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
Oops, something went wrong.