diff --git a/smart-hut/src/components/modalform.js b/smart-hut/src/components/RoomModal.js
similarity index 66%
rename from smart-hut/src/components/modalform.js
rename to smart-hut/src/components/RoomModal.js
index 87835a3..350edb1 100644
--- a/smart-hut/src/components/modalform.js
+++ b/smart-hut/src/components/RoomModal.js
@@ -10,31 +10,48 @@ import {
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";
-export default class ModalWindow extends Component {
+class RoomModal extends Component {
constructor(props) {
super(props);
-
- if (typeof this.props.idRoom === "function") {
- this.idRoom = this.props.idRoom();
- } else {
- this.idRoom = this.props.idRoom;
- }
-
- this.state = {
- id: "",
- selectedIcon: "",
- name: this.props.type === "new" ? "New Room" : this.idRoom.name,
- img: this.props.type === "new" ? null : this.idRoom.image,
- openModal: false,
- };
+ 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: "home",
+ 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: NO_IMAGE}
+ }));
+ }
+
+ setInitialState() {
+ this.setState(this.initialState);
+ }
+
+ get type() {
+ return !this.props.id ? "new" : "modify";
}
addRoomModal = (e) => {
@@ -43,29 +60,37 @@ export default class ModalWindow extends Component {
name: this.state.name,
image: this.state.img,
};
- this.props.addRoom(data);
- this.setState({
- name: "Device",
- });
- this.closeModal();
+
+ 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 === ""
- ? this.idRoom.icon
+ ? this.props.room.icon
: this.state.selectedIcon,
- name: this.state.name === "" ? this.idRoom.name : this.state.name,
+ name: this.state.name === "" ? this.props.room.name : this.state.name,
image: this.state.img,
};
- this.props.updateRoom(data);
- this.closeModal();
+
+ 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.closeModal();
+ // no need to close modal since this room modal instance will be deleted
+ this.props.deleteRoom(this.props.id)
+ .catch((err) => console.error('error in deleting room', err));
};
changeSomething = (event) => {
@@ -107,7 +132,7 @@ export default class ModalWindow extends Component {
{!this.props.nicolaStop ? (
- {this.props.type === "new" ? (
+ {this.type === "new" ? (