From b60a050a7012c61455f5e7336a754980fa9a6f50 Mon Sep 17 00:00:00 2001 From: britea Date: Sat, 11 Apr 2020 18:29:32 +0200 Subject: [PATCH] Refactor redux almost complete --- smart-hut/src/components/HeaderController.js | 9 +- smart-hut/src/components/RoomModal.js | 51 ++++++------ .../dashboard/devices/DeviceSettingsModal.js | 3 +- .../components/dashboard/devices/Dimmer.js | 29 ++++--- .../src/components/dashboard/devices/Light.js | 18 ++-- .../components/dashboard/devices/NewDevice.js | 83 +++++++++---------- smart-hut/src/remote.js | 73 +++++++++++++--- smart-hut/src/store.js | 52 ++++++++++-- smart-hut/src/views/Navbar.js | 47 ++++++++--- 9 files changed, 232 insertions(+), 133 deletions(-) diff --git a/smart-hut/src/components/HeaderController.js b/smart-hut/src/components/HeaderController.js index 08dc3af..d6ef4c5 100644 --- a/smart-hut/src/components/HeaderController.js +++ b/smart-hut/src/components/HeaderController.js @@ -20,9 +20,6 @@ const TitleImage = () => ; export class MyHeader extends React.Component { constructor(props) { super(props); - this.state = { - username: "", - }; this.getInfo(); this.logout = this.logout.bind(this); @@ -57,7 +54,7 @@ export class MyHeader extends React.Component { @@ -79,10 +76,10 @@ export class MyHeader extends React.Component { - + diff --git a/smart-hut/src/components/RoomModal.js b/smart-hut/src/components/RoomModal.js index 350edb1..eabdb89 100644 --- a/smart-hut/src/components/RoomModal.js +++ b/smart-hut/src/components/RoomModal.js @@ -32,7 +32,7 @@ class RoomModal extends Component { get initialState() { return { - selectedIcon: "home", + 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, @@ -41,9 +41,11 @@ class RoomModal extends Component { removeImage(e) { e.preventDefault(); - this.setState(update(this.state, { - image: {$set: NO_IMAGE} - })); + this.setState( + update(this.state, { + image: { $set: null }, + }) + ); } setInitialState() { @@ -61,36 +63,38 @@ class RoomModal extends Component { image: this.state.img, }; - this.props.saveRoom(data, null) + this.props + .saveRoom(data, null) .then(() => { this.setInitialState(); this.closeModal(); }) - .catch((err) => console.error('error in creating room', err)); + .catch((err) => console.error("error in creating room", err)); }; modifyRoomModal = (e) => { let data = { - icon: - this.state.selectedIcon === "" - ? this.props.room.icon - : this.state.selectedIcon, - name: this.state.name === "" ? this.props.room.name : this.state.name, + icon: this.state.selectedIcon, + name: this.state.name, image: this.state.img, }; - this.props.saveRoom(data, this.props.id) + 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)); + .catch((err) => console.error("error in updating room", err)); }; deleteRoom = (e) => { - // 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)); + this.props + .deleteRoom(this.props.id) + .then(() => this.closeModal()) + .catch((err) => console.error("error in deleting room", err)); }; changeSomething = (event) => { @@ -101,7 +105,6 @@ class RoomModal extends Component { closeModal = (e) => { this.setState({ openModal: false }); - this.updateIcon("home"); }; openModal = (e) => { @@ -172,7 +175,7 @@ class RoomModal extends Component { ) : null} - +
{this.type === "new" ? "Add new room" : "Modify room"}
@@ -192,7 +195,7 @@ class RoomModal extends Component {

Insert an image of the room:

this.fileInputRef.current.click()} /> @@ -207,9 +210,9 @@ class RoomModal extends Component { onChange={this.getBase64.bind(this)} /> - {this.state.img ? + {this.state.img ? ( - : null } + ) : null}
@@ -244,9 +247,7 @@ class RoomModal extends Component { @@ -188,7 +204,14 @@ const setActiveRoom = (activeRoom) => { return (dispatch) => dispatch(appActions.setActiveRoom(activeRoom)); }; -const mapStateToProps = (state, _) => ({ rooms: state.rooms }); +const mapStateToProps = (state, _) => ({ + rooms: state.rooms, + activeRoom: state.active.activeRoom, + roomModalRefs: Object.keys(state.rooms).reduce( + (acc, key) => ({ ...acc, [key]: React.createRef() }), + {} + ), +}); const NavbarContainer = connect(mapStateToProps, { ...RemoteService, setActiveRoom,