Merge branch '112-remove-possibility-to-reselect-a-device-already-added-to-a-scene' into 'dev'

fixed add states daoes not show states already in scene

Closes #112

See merge request sa4-2020/the-sanmarinoes/frontend!143
This commit is contained in:
Andrea Brites Marto 2020-05-10 11:05:24 +02:00
commit 1a3b631062
2 changed files with 48 additions and 14 deletions

View File

@ -32,11 +32,16 @@ class NewSceneDevice extends Component {
openModal: false, openModal: false,
sceneDevices: this.props.scene ? this.props.scene.sceneStates : {}, sceneDevices: this.props.scene ? this.props.scene.sceneStates : {},
deviceName: "", deviceName: "",
availableDevices: [],
}; };
this.getDevices(); this.getDevices();
//this.getSceneStates();
this.availableDevices();
//console.log(this.state);
this.setSceneState = this.setSceneState.bind(this); this.setSceneState = this.setSceneState.bind(this);
this.createState = this.createState.bind(this); this.createState = this.createState.bind(this);
this.availableDevices = this.availableDevices.bind(this);
} }
getDevices() { getDevices() {
@ -45,6 +50,12 @@ class NewSceneDevice extends Component {
.catch((err) => console.error(`error fetching devices:`, err)); .catch((err) => console.error(`error fetching devices:`, err));
} }
// getSceneStates() {
// this.props
// .fetchStates(this.props.activeScene)
// .catch((err) => console.error(`error fetching states`, err));
// }
handleOpen = () => { handleOpen = () => {
this.setState({ openModal: true }); this.setState({ openModal: true });
}; };
@ -53,6 +64,28 @@ class NewSceneDevice extends Component {
this.setState({ openModal: false }); this.setState({ openModal: false });
}; };
availableDevices() {
let availableDevices = [];
this.props.devices.forEach((e) => {
if (
Object.values(this.props.sceneStates).filter((d) => e.id === d.deviceId)
.length < 1
) {
if (e.flowType === "OUTPUT") {
availableDevices.push({
key: e.id,
text: e.name,
value: e.id,
});
}
} else {
//console.log("NOT FOUND", e);
}
});
this.setState({ availableDevices: availableDevices });
//return availableDevices;
}
resetState = () => { resetState = () => {
this.setState(this.baseState); this.setState(this.baseState);
this.handleClose(); this.handleClose();
@ -80,18 +113,6 @@ class NewSceneDevice extends Component {
} }
render() { render() {
const availableDevices = [];
this.props.devices.forEach((e) => {
if (!Object.keys(this.state.sceneDevices).find((d) => e.id === d)) {
if (e.flowType === "OUTPUT") {
availableDevices.push({
key: e.id,
text: e.name,
value: e.id,
});
}
}
});
return ( return (
<Modal <Modal
closeIcon closeIcon
@ -122,7 +143,8 @@ class NewSceneDevice extends Component {
fluid fluid
multiple multiple
onChange={this.setSceneState} onChange={this.setSceneState}
options={availableDevices} onClick={() => this.availableDevices()}
options={this.state.availableDevices}
/> />
</Form.Field> </Form.Field>
</Form> </Form>
@ -145,6 +167,17 @@ class NewSceneDevice extends Component {
const mapStateToProps = (state, _) => ({ const mapStateToProps = (state, _) => ({
devices: Object.values(state.devices), devices: Object.values(state.devices),
get sceneStates() {
if (state.active.activeScene !== -1) {
const stateArray = [
...state.scenes[state.active.activeScene].sceneStates,
].sort();
console.log(state.scenes[state.active.activeScene]);
return stateArray.map((id) => state.sceneStates[id]);
} else {
return [];
}
},
activeScene: state.active.activeScene, activeScene: state.active.activeScene,
}); });
const NewSceneDeviceContainer = connect( const NewSceneDeviceContainer = connect(

View File

@ -30,7 +30,7 @@ class ScenesPanel extends Component {
<Header as="h3">Add devices - Apply Scene</Header> <Header as="h3">Add devices - Apply Scene</Header>
</Card.Header> </Card.Header>
<Segment basic style={{ width: "100%", height: "100%" }}> <Segment basic style={{ width: "100%", height: "100%" }}>
<NewSceneDevice /> <NewSceneDevice states={this.props.sceneStates} />
</Segment> </Segment>
</Card.Content> </Card.Content>
<Card.Content extra> <Card.Content extra>
@ -68,6 +68,7 @@ const mapStateToProps = (state, _) => ({
const stateArray = [ const stateArray = [
...state.scenes[state.active.activeScene].sceneStates, ...state.scenes[state.active.activeScene].sceneStates,
].sort(); ].sort();
console.log(state.scenes[state.active.activeScene]);
return stateArray.map((id) => state.sceneStates[id]); return stateArray.map((id) => state.sceneStates[id]);
} else { } else {
return []; return [];