From 56da195894992f3ea411d78cc77f62c03ffd829f Mon Sep 17 00:00:00 2001 From: britea Date: Thu, 16 Apr 2020 15:15:33 +0200 Subject: [PATCH] Fixed registration bug --- smart-hut/src/components/RoomModal.js | 11 ++- smart-hut/src/components/SceneModal.js | 73 ++++++++++++++-- .../components/dashboard/NewSceneDevice.js | 87 +++++++++++++++++++ .../src/components/dashboard/ScenesPanel.js | 23 ++++- smart-hut/src/remote.js | 14 +-- smart-hut/src/views/Login.js | 2 +- 6 files changed, 193 insertions(+), 17 deletions(-) create mode 100644 smart-hut/src/components/dashboard/NewSceneDevice.js diff --git a/smart-hut/src/components/RoomModal.js b/smart-hut/src/components/RoomModal.js index eabdb89..36496b2 100644 --- a/smart-hut/src/components/RoomModal.js +++ b/smart-hut/src/components/RoomModal.js @@ -49,7 +49,16 @@ class RoomModal extends Component { } setInitialState() { - this.setState(this.initialState); + for (let key in this.initialState) { + if (this.initialState.hasOwnProperty(key)) { + //console.log(key + " -> " + this.initialState[key]); + this.setState({ + key: this.initialState[key], + }); + } + } + console.log(this.initialState); + //this.setState(state); } get type() { diff --git a/smart-hut/src/components/SceneModal.js b/smart-hut/src/components/SceneModal.js index af97d89..cff8630 100644 --- a/smart-hut/src/components/SceneModal.js +++ b/smart-hut/src/components/SceneModal.js @@ -1,5 +1,14 @@ import React, { Component } from "react"; -import { Button, Header, Modal, Icon, Responsive } from "semantic-ui-react"; +import { + Button, + Header, + Modal, + Icon, + Responsive, + Form, + Input, + Dropdown, +} from "semantic-ui-react"; import { connect } from "react-redux"; import { RemoteService } from "../remote"; import { appActions } from "../storeActions"; @@ -10,6 +19,7 @@ class SceneModal extends Component { super(props); this.state = this.initialState; this.setInitialState(); + this.getDevices(); this.addSceneModal = this.addSceneModal.bind(this); this.modifySceneModal = this.modifySceneModal.bind(this); @@ -18,7 +28,9 @@ class SceneModal extends Component { get initialState() { return { - //INITIAL STATE HERE + name: this.type === "new" ? "New Scene" : this.props.room.name, + sceneDevices: this.type === "new" ? [{}] : [this.props.scene.devices], + openModal: false, }; } @@ -30,6 +42,12 @@ class SceneModal extends Component { return !this.props.id ? "new" : "modify"; } + getDevices() { + this.props + .fetchDevices() + .catch((err) => console.error(`error fetching devices:`, err)); + } + addSceneModal = (e) => { /*let data = { // DATA HERE @@ -81,7 +99,21 @@ class SceneModal extends Component { this.setState({ openModal: true }); }; + setSceneDevice(e, d) { + this.setState({ sceneDevices: d.value }); + } + render() { + const availableDevices = []; + this.props.devices.forEach((e) => { + if (!this.state.sceneDevices.includes(e)) { + availableDevices.push({ + key: e.id, + text: e.name, + id: e.id, + }); + } + }); return (
{!this.props.nicolaStop ? ( @@ -132,9 +164,30 @@ class SceneModal extends Component { {this.type === "new" ? "Add new scene" : "Modify scene"} - { - //TODO FORM TO ADD OR MODIFY SCENE - } +
+

Insert the name of the scene:

+ + + + + + + +
{this.type === "modify" ? ( + + + ); + } +} + +const mapStateToProps = (state, _) => ({ + devices: Object.values(state.devices), + activeRoom: state.active.activeRoom, +}); +const NewSceneDeviceContainer = connect( + mapStateToProps, + RemoteService +)(NewSceneDevice); +export default NewSceneDeviceContainer; diff --git a/smart-hut/src/components/dashboard/ScenesPanel.js b/smart-hut/src/components/dashboard/ScenesPanel.js index 1a6db3e..ca96e0b 100644 --- a/smart-hut/src/components/dashboard/ScenesPanel.js +++ b/smart-hut/src/components/dashboard/ScenesPanel.js @@ -1,21 +1,36 @@ import React, { Component } from "react"; import { connect } from "react-redux"; import { RemoteService } from "../../remote"; +import NewSceneDevice from "./NewSceneDevice"; +import { Grid } from "semantic-ui-react"; class ScenesPanel extends Component { constructor(props) { super(props); - console.log(this.props.activeRoom, this.props.activeTab); + console.log(this.props.activeScene); } render() { - return

SCENES

; + return ( + + { + //TODO DISPLAY DEVICES IN SCENE + } + {this.props.isActiveDefaultScene ? ( + + + + ) : null} + + ); } } const mapStateToProps = (state, _) => ({ - activeRoom: state.active.activeRoom, - activeTab: state.active.activeTab, + get isActiveDefaultScene() { + return state.active.activeScene === -1; + }, + activeScene: state.active.activeScene, }); const ScenesPanelContainer = connect( mapStateToProps, diff --git a/smart-hut/src/remote.js b/smart-hut/src/remote.js index 3100417..825ba1d 100644 --- a/smart-hut/src/remote.js +++ b/smart-hut/src/remote.js @@ -39,8 +39,8 @@ const Endpoint = { * @param {[String]String} query query ('?') parameters (no params by default) * @param {any} body the JSON request body */ - send: (method, route, query = {}, body = null) => { - if (!Endpoint.token) { + send: (method, route, query = {}, body = null, registration) => { + if (!registration && !Endpoint.token) { throw new Error("No token while performing authenticated request"); } @@ -48,9 +48,11 @@ const Endpoint = { method: method, params: query, data: ["put", "post"].indexOf(method) !== -1 ? body : null, - headers: { - Authorization: `Bearer ${Endpoint.token}`, - }, + headers: !registration + ? { + Authorization: `Bearer ${Endpoint.token}`, + } + : {}, }).then((res) => { if (!res.data && method !== "delete") { console.error("Response body is empty"); @@ -109,7 +111,7 @@ const Endpoint = { * @returns {Promise<*, *>} The Axios-generated promise */ post(route, query, body) { - return this.send("post", route, query, body); + return this.send("post", route, query, body, true); }, /** diff --git a/smart-hut/src/views/Login.js b/smart-hut/src/views/Login.js index 375d1a5..3544e7a 100644 --- a/smart-hut/src/views/Login.js +++ b/smart-hut/src/views/Login.js @@ -31,7 +31,7 @@ class Login extends Component { .login(this.state.user, this.state.password) .then(() => this.props.history.push("/dashboard")) .catch((err) => { - console.log(err); + console.log("CIAO", err); this.setState({ error: { state: true, message: err.messages.join(" - ") }, });