import React, { Component } from "react"; import { Button, Header, Modal, Form, Input, Icon, Responsive, Image, } 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"; const NO_IMAGE = "https://react.semantic-ui.com/images/wireframe/image.png"; class RoomModal extends Component { constructor(props) { super(props); this.state = this.initialState; this.setInitialState(); this.fileInputRef = React.createRef(); this.addRoomModal = this.addRoomModal.bind(this); this.updateIcon = this.updateIcon.bind(this); this.removeImage = this.removeImage.bind(this); } get initialState() { return { selectedIcon: this.type === "new" ? "home" : this.props.room.icon, name: this.type === "new" ? "New Room" : this.props.room.name, img: this.type === "new" ? null : this.props.room.image, openModal: false, }; } removeImage(e) { e.preventDefault(); this.setState( update(this.state, { image: { $set: null }, }) ); } setInitialState() { this.setState(this.initialState); } get type() { return !this.props.id ? "new" : "modify"; } addRoomModal = (e) => { let data = { icon: this.state.selectedIcon, name: this.state.name, image: this.state.img, }; this.props .saveRoom(data, null) .then(() => { this.setInitialState(); this.closeModal(); }) .catch((err) => console.error("error in creating room", err)); }; modifyRoomModal = (e) => { let data = { icon: this.state.selectedIcon, name: this.state.name, image: this.state.img, }; console.log("data", data); this.props .saveRoom(data, this.props.id) .then(() => { this.setInitialState(); this.closeModal(); }) .catch((err) => console.error("error in updating room", err)); }; deleteRoom = (e) => { this.props .deleteRoom(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 }); } getBase64(file, callback) { let reader = new FileReader(); reader.readAsDataURL(file.target.files[0]); reader.onload = () => { this.setState(Object.assign(this.state, { img: reader.result })); }; reader.onerror = console.error; } 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 room" : "Modify room"}

Insert the name of the room:

Insert an image of the room:

this.fileInputRef.current.click()} /> {this.state.img ? ( ) : null}

Select an icon:

{this.type === "modify" ? ( ) : null}
); } } const setActiveRoom = (activeRoom) => { return (dispatch) => dispatch(appActions.setActiveRoom(activeRoom)); }; const mapStateToProps = (state, ownProps) => ({ room: ownProps.id ? state.rooms[ownProps.id] : null, }); const RoomModalContainer = connect( mapStateToProps, { ...RemoteService, setActiveRoom }, null, { forwardRef: true } )(RoomModal); export default RoomModalContainer;