frontend/smart-hut/src/components/SceneModal.js

287 lines
7.8 KiB
JavaScript
Raw Normal View History

import React, { Component } from "react";
2020-04-16 13:15:33 +00:00
import {
Button,
Header,
Modal,
Icon,
Responsive,
Form,
Input,
2020-05-04 14:09:56 +00:00
Dropdown,
2020-05-05 13:53:07 +00:00
Checkbox,
2020-04-16 13:15:33 +00:00
} from "semantic-ui-react";
2020-05-04 13:08:17 +00:00
import SelectIcons from "./SelectIcons";
import { connect } from "react-redux";
import { RemoteService } from "../remote";
import { appActions } from "../storeActions";
//import { update } from "immutability-helper";
class SceneModal extends Component {
constructor(props) {
super(props);
this.state = this.initialState;
this.addSceneModal = this.addSceneModal.bind(this);
this.modifySceneModal = this.modifySceneModal.bind(this);
this.deleteScene = this.deleteScene.bind(this);
2020-05-04 13:08:17 +00:00
this.updateIcon = this.updateIcon.bind(this);
2020-05-05 13:53:07 +00:00
this.setGuestAccessEnabled = this.setGuestAccessEnabled.bind(this);
2020-05-04 14:09:56 +00:00
this.setCopyFrom = this.setCopyFrom.bind(this);
}
componentDidUpdate(oldProps) {
// this might bug out since we are just checking the length
// to see if the elements inside this.props.scenes are changing
if (this.props.scenes.length !== oldProps.scenes.length) {
this.setState({ ...this.state, scenes: this.scenes });
}
}
get initialState() {
return {
2020-04-18 14:26:12 +00:00
name: this.type === "new" ? "New Scene" : this.props.scene.name,
2020-04-16 13:15:33 +00:00
openModal: false,
2020-05-04 13:08:17 +00:00
selectedIcon: "home",
2020-05-04 14:09:56 +00:00
scenes: this.scenes,
copyFrom: null,
2020-05-05 13:53:07 +00:00
guestAccessEnabled:
this.type === "new" ? null : this.props.scene.guestAccessEnabled,
};
}
2020-05-04 14:09:56 +00:00
get scenes() {
return this.props.scenes.map((s) => ({
key: s.id,
text: s.name,
value: s.id,
}));
}
setInitialState() {
this.setState(this.initialState);
}
get type() {
return !this.props.id ? "new" : "modify";
}
addSceneModal = (e) => {
2020-04-18 14:26:12 +00:00
let data = {
name: this.state.name,
2020-05-04 13:08:17 +00:00
icon: this.state.selectedIcon,
2020-04-18 14:26:12 +00:00
};
this.props
2020-05-04 14:09:56 +00:00
.saveScene(data, null, this.state.copyFrom)
.then(() => {
this.setInitialState();
this.closeModal();
})
2020-04-18 14:26:12 +00:00
.catch((err) => console.error("error in creating room", err));
};
modifySceneModal = (e) => {
2020-04-18 14:26:12 +00:00
let data = {
name: this.state.name,
2020-05-04 13:08:17 +00:00
icon: this.state.selectedIcon,
2020-05-05 13:53:07 +00:00
guestAccessEnabled: this.state.guestAccessEnabled,
2020-04-18 14:26:12 +00:00
};
2020-05-05 13:53:07 +00:00
console.log(data);
2020-04-18 14:26:12 +00:00
this.props
.saveScene(data, this.props.id)
.then(() => {
this.setInitialState();
this.closeModal();
})
2020-04-18 14:26:12 +00:00
.catch((err) => console.error("error in updating room", err));
};
deleteScene = (e) => {
this.props
2020-04-18 14:26:12 +00:00
.deleteScene(this.props.id)
.then(() => this.closeModal())
2020-04-18 14:26:12 +00:00
.catch((err) => console.error("error in deleting room", err));
};
changeSomething = (event) => {
let nam = event.target.name;
let val = event.target.value;
this.setState({ [nam]: val });
};
closeModal = (e) => {
this.setState({ openModal: false });
};
openModal = (e) => {
this.setState({ openModal: true });
};
2020-05-04 13:08:17 +00:00
updateIcon(e) {
this.setState({ selectedIcon: e });
}
2020-05-04 14:09:56 +00:00
setCopyFrom(_, copyFrom) {
this.setState({ ...this.state, copyFrom: copyFrom.value });
}
2020-05-05 13:53:07 +00:00
setGuestAccessEnabled(val) {
console.log(this.state, val);
this.setState({ guestAccessEnabled: val });
}
render() {
2020-05-04 13:08:17 +00:00
const spaceDiv = {
background: "#f4f4f4",
padding: "10px 10px",
margin: "10px 0px",
};
return (
<div>
{!this.props.nicolaStop ? (
<div>
<Responsive minWidth={768}>
{this.type === "new" ? (
<Button
icon
labelPosition="left"
inverted
onClick={this.openModal}
>
<Icon name="plus" size="small" />
ADD SCENE
</Button>
) : (
<Icon name="pencil" size="small" onClick={this.openModal} />
)}
</Responsive>
<Responsive maxWidth={768}>
{this.type === "new" ? (
<Button
icon
fluid
labelPosition="left"
onClick={this.openModal}
>
<Icon name="plus" size="small" />
ADD SCENE
</Button>
) : (
<Button
icon
fluid
labelPosition="left"
onClick={this.openModal}
>
<Icon name="pencil" size="small" />
EDIT SCENE
</Button>
)}
</Responsive>
</div>
) : null}
<Modal closeIcon onClose={this.closeModal} open={this.state.openModal}>
<Header>
{this.type === "new" ? "Add new scene" : "Modify scene"}
</Header>
<Modal.Content>
2020-04-16 13:15:33 +00:00
<Form>
<p>Insert the name of the scene:</p>
<Form.Field>
<Input
2020-05-04 14:09:56 +00:00
required
2020-04-16 13:15:33 +00:00
label="Scene name"
placeholder="Scene Name"
name="name"
type="text"
onChange={this.changeSomething}
value={this.state.name}
/>
</Form.Field>
2020-05-04 13:08:17 +00:00
<div style={spaceDiv}>
2020-05-04 14:09:56 +00:00
<label>Icon:</label>
2020-05-04 13:08:17 +00:00
<SelectIcons
updateIcon={this.updateIcon}
currentIcon={
this.type === "new" ? "home" : this.props.scene.icon
}
/>
</div>
2020-05-04 14:09:56 +00:00
{this.type === "new" && (
<Form.Field>
<label>Copy configuration from:</label>
<Dropdown
name="guests"
placeholder="Select scene to copy configuration form"
fluid
onChange={this.setCopyFrom}
options={this.state.scenes}
value={this.state.copyFrom}
/>
</Form.Field>
)}
2020-05-05 13:53:07 +00:00
{this.type === "modify" ? (
<Form.Field>
<Checkbox
checked={this.state.guestAccessEnabled}
toggle
onChange={(e, val) =>
this.setGuestAccessEnabled(val.checked)
}
/>
</Form.Field>
) : null}
2020-04-16 13:15:33 +00:00
</Form>
{this.type === "modify" ? (
<Button
icon
labelPosition="left"
inverted
color="red"
onClick={this.deleteScene}
>
<Icon name="trash alternate" />
Delete Scene
</Button>
) : null}
</Modal.Content>
<Modal.Actions>
<Button color="red" onClick={this.closeModal}>
<Icon name="remove" />{" "}
{this.type === "new" ? "Cancel" : "Discard changes"}
</Button>
<Button
color="green"
onClick={
this.type === "new" ? this.addSceneModal : this.modifySceneModal
}
>
<Icon name="checkmark" />{" "}
{this.type === "new" ? "Add scene" : "Save changes"}
</Button>
</Modal.Actions>
</Modal>
</div>
);
}
}
const setActiveScene = (activeScene) => {
return (dispatch) => dispatch(appActions.setActiveScene(activeScene));
};
const mapStateToProps = (state, ownProps) => ({
scene: ownProps.id ? state.scenes[ownProps.id] : null,
2020-05-04 14:09:56 +00:00
scenes: Object.values(state.scenes),
});
const SceneModalContainer = connect(
mapStateToProps,
{ ...RemoteService, setActiveScene },
null,
{ forwardRef: true }
)(SceneModal);
export default SceneModalContainer;