import React, { Component } from "react"; import { Button, Header, Modal, Icon, Responsive, Form, Input, Dropdown, } from "semantic-ui-react"; 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); this.updateIcon = this.updateIcon.bind(this); 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 { name: this.type === "new" ? "New Scene" : this.props.scene.name, openModal: false, selectedIcon: "home", scenes: this.scenes, copyFrom: null, }; } 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) => { let data = { name: this.state.name, icon: this.state.selectedIcon, }; this.props .saveScene(data, null, this.state.copyFrom) .then(() => { this.setInitialState(); this.closeModal(); }) .catch((err) => console.error("error in creating room", err)); }; modifySceneModal = (e) => { let data = { name: this.state.name, icon: this.state.selectedIcon, }; this.props .saveScene(data, this.props.id) .then(() => { this.setInitialState(); this.closeModal(); }) .catch((err) => console.error("error in updating room", err)); }; deleteScene = (e) => { this.props .deleteScene(this.props.id) .then(() => this.closeModal()) .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 }); }; updateIcon(e) { this.setState({ selectedIcon: e }); } setCopyFrom(_, copyFrom) { this.setState({ ...this.state, copyFrom: copyFrom.value }); } render() { const spaceDiv = { background: "#f4f4f4", padding: "10px 10px", margin: "10px 0px", }; return (
{!this.props.nicolaStop ? (
{this.type === "new" ? ( ) : ( )} {this.type === "new" ? ( ) : ( )}
) : null}
{this.type === "new" ? "Add new scene" : "Modify scene"}

Insert the name of the scene:

{this.type === "new" && ( )}
{this.type === "modify" ? ( ) : null}
); } } const setActiveScene = (activeScene) => { return (dispatch) => dispatch(appActions.setActiveScene(activeScene)); }; const mapStateToProps = (state, ownProps) => ({ scene: ownProps.id ? state.scenes[ownProps.id] : null, scenes: Object.values(state.scenes), }); const SceneModalContainer = connect( mapStateToProps, { ...RemoteService, setActiveScene }, null, { forwardRef: true } )(SceneModal); export default SceneModalContainer;