From e786288fa8610cc880cc060eb9e44bf14fe65d84 Mon Sep 17 00:00:00 2001 From: britea Date: Thu, 19 Mar 2020 11:52:13 +0100 Subject: [PATCH] fix all warnings and cleaned code --- smart-hut/src/App.js | 13 +- smart-hut/src/client_server.js | 84 +----------- smart-hut/src/components/HeaderController.js | 10 +- smart-hut/src/components/HomeNavbar.js | 1 - smart-hut/src/components/SelectIcons.js | 3 +- .../src/components/dashboard/DevicePanel.js | 121 ++++++++++-------- .../dashboard/devices/DeviceTypeController.js | 10 +- .../components/dashboard/devices/NewDevice.js | 13 +- .../dashboard/devices/SmartPlugStyle.js | 1 - smart-hut/src/components/modalform.js | 28 ++-- smart-hut/src/views/ConfirmForgotPassword.js | 7 - smart-hut/src/views/ConfirmRegistration.js | 7 - smart-hut/src/views/Dashboard.js | 91 +++++++++---- smart-hut/src/views/Forgot-pass-reset.js | 8 +- smart-hut/src/views/Home.js | 2 - smart-hut/src/views/Navbar.js | 12 +- smart-hut/src/views/NavbarTest.js | 12 -- smart-hut/src/views/TestHeaderController.js | 11 +- 18 files changed, 192 insertions(+), 242 deletions(-) delete mode 100644 smart-hut/src/views/NavbarTest.js diff --git a/smart-hut/src/App.js b/smart-hut/src/App.js index 0054987..1126e85 100644 --- a/smart-hut/src/App.js +++ b/smart-hut/src/App.js @@ -44,18 +44,22 @@ class App extends Component { } componentDidMount() { - if (this.props.location) { - const values = queryString.parse(this.props.location.search); + if (window.location) { + const values = queryString.parse(window.location.search); + console.log(values); this.setState({ query : values }); + } else { + this.setState({ + query : "ciao" + }); } } auth(data) { return call.login(data.params) .then(res => { - console.log(res); if (res.data && res.status === 200) { localStorage.setItem("token", res.data.jwttoken); this.setState( @@ -80,7 +84,6 @@ class App extends Component { }; logout() { - console.log("logout") this.setState({ loggedIn : false, }); @@ -109,7 +112,7 @@ class App extends Component { - + diff --git a/smart-hut/src/client_server.js b/smart-hut/src/client_server.js index 3821dcb..8477ad4 100644 --- a/smart-hut/src/client_server.js +++ b/smart-hut/src/client_server.js @@ -17,132 +17,56 @@ const tkn = localStorage.getItem("token"); export var call = { login: function(data, headers) { return axios.post(config +'auth/login', data) - .then(res => { - return res; - }).catch(err => { - return {status : "Errore"}; - }); }, register: function(data, headers) { return axios.post(config + 'register', data) - .then(res => { - return res; - }).catch(err => { - //console.error(err); - return {status : "Errore"}; - }); }, initResetPassword: function(data, headers) { return axios.post(config + 'register/init-reset-password', data) - .then(res => { - return res; - }).catch(err => { - return {status : "Errore"}; - }); }, resetPassword: function(data, headers) { return axios.put(config + 'register/reset-password', data) - .then(res => { - return res; - }).catch(err => { - return {status : "Errore"}; - }); }, getAllRooms: function(token) { if (!token){ token = tkn; } return axios.get(config + 'room', { headers: { Authorization : "Bearer " + token } }) - .then(res => { - return res; - }).catch(err => { - return err; - }); }, getAllDevices: function(token) { if (!token){ token = tkn; } - return axios.get(config + 'device', { headers: { Authorization : "Bearer " + token } }) - .then(res => { - return res; - }).catch(err => { - return err; - }); + return axios.get(config + 'device', { headers: { Authorization : "Bearer " + token } }); }, getAllDevicesByRoom: function(id, token) { if (!token){ token = tkn; } - return axios.get(config + 'room/' + id + '/devices' , { headers: { Authorization : "Bearer " + token } }) - .then(res => { - return res; - }).catch(err => { - return err; - }); + return axios.get(config + 'room/' + id + '/devices' , { headers: { Authorization : "Bearer " + token } }); }, createRoom: function(data, headers) { return axios.post(config + 'room', data, { headers: { Authorization : "Bearer " + tkn } }) - .then(res => { - return res; - }).catch(err => { - return err; - }); }, updateRoom: function(data, headers) { return axios.put(config + 'room/'+data.id) - .then(res => { - return res; - }).catch(err => { - return err; - }); }, deleteRoom: function(data, headers) { - return axios.delete(config + 'room/'+data.id) - .then(res => { - return res; - }).catch(err => { - return err; - }); + return axios.delete(config + 'room/'+data.id, { headers: { Authorization : "Bearer " + tkn } }); }, devicePost: function(data, headers) { - return axios.post(config + data.device, data.params) - .then(res => { - return res; - }).catch(err => { - return err; - }); + return axios.post(config + data.device, data.params, { headers: { Authorization : "Bearer " + tkn } }) }, deviceUpdate: function(data, headers) { return axios.put(config + data.device, data.params) - .then(res => { - return res; - }).catch(err => { - return err; - }); }, deviceDelete: function(data, headers) { return axios.delete(config + data.device + '/' + data.id, data.params) - .then(res => { - return res; - }).catch(err => { - return err; - }); }, deviceGetById: function(data, headers) { return axios.get(config + data.device + '/' + data.id) - .then(res => { - return res; - }).catch(err => { - return err; - }); }, deviceGetAll: function(data, headers) { return axios.get(config + data.device) - .then(res => { - return res; - }).catch(err => { - return err; - }); } }; diff --git a/smart-hut/src/components/HeaderController.js b/smart-hut/src/components/HeaderController.js index 5bf9c9f..afb59ef 100644 --- a/smart-hut/src/components/HeaderController.js +++ b/smart-hut/src/components/HeaderController.js @@ -1,6 +1,5 @@ -import React, { Component } from "react"; -import { render } from "react-dom"; -import { Button, Menu, Header, Dropdown, Icon, Grid, Divider} from 'semantic-ui-react' +import React from "react"; +import { Dropdown, Icon, Grid, Divider} from 'semantic-ui-react' import { Segment, Image } from 'semantic-ui-react' const ImageExampleWrapped = () => ( @@ -23,10 +22,6 @@ const ImageExampleWrapped3 = () => ( ) -const HeaderExampleIconProp = () => ( -
-) - const GridExampleInverted = (props) => ( @@ -67,7 +62,6 @@ const GridExampleInverted = (props) => ( export default class MyHeader extends React.Component { render() { - console.log(this.props) return (
diff --git a/smart-hut/src/components/HomeNavbar.js b/smart-hut/src/components/HomeNavbar.js index 5bcafb5..651e1a0 100644 --- a/smart-hut/src/components/HomeNavbar.js +++ b/smart-hut/src/components/HomeNavbar.js @@ -1,6 +1,5 @@ import _ from "lodash"; import React, { Component } from "react"; -import { render } from "react-dom"; import { Container, Icon, diff --git a/smart-hut/src/components/SelectIcons.js b/smart-hut/src/components/SelectIcons.js index e9d23f6..f1a7994 100644 --- a/smart-hut/src/components/SelectIcons.js +++ b/smart-hut/src/components/SelectIcons.js @@ -1,5 +1,5 @@ import React, { Component } from 'react'; -import { Button, Header, Image, Modal, Checkbox, Form, Grid, Input, Icon } from 'semantic-ui-react' +import { Button, Grid } from 'semantic-ui-react' export default class SelectIcons extends Component { @@ -29,7 +29,6 @@ export default class SelectIcons extends Component { ['trophy', 'wrench', 'image', 'book', 'university', 'medkit'], ['paw', 'tree', 'utensils', 'male', 'female', 'life ring outline']]; - var colums = 0; return ( { diff --git a/smart-hut/src/components/dashboard/DevicePanel.js b/smart-hut/src/components/dashboard/DevicePanel.js index cb32a63..a5b4eeb 100644 --- a/smart-hut/src/components/dashboard/DevicePanel.js +++ b/smart-hut/src/components/dashboard/DevicePanel.js @@ -1,6 +1,6 @@ import React, {Component} from 'react'; import { - Grid, + Grid, } from "semantic-ui-react"; import {editButtonStyle, panelStyle} from "./devices/styleComponents"; import {checkMaxLength, DEVICE_NAME_MAX_LENGTH} from "./devices/constants"; @@ -9,67 +9,76 @@ import NewDevice from "./devices/NewDevice"; export default class DevicePanel extends Component { - constructor(props) { - super(props); - this.state = { - editMode : false, - devices : this.props.devices, - }; - } + constructor(props) { + super(props); + this.state = { + editMode : false, + }; + + this.addDevice = this.addDevice.bind(this); + } - editModeController = (e) => this.setState((prevState) => ({ editMode: !prevState.editMode })); + editModeController = (e) => this.setState((prevState) => ({ editMode: !prevState.editMode })); - changeDeviceData = (deviceId, newSettings) => { + changeDeviceData = (deviceId, newSettings) => { - console.log(newSettings.name, " <-- new name --> ", deviceId ); - this.state.devices.map(device => { - if(device.id === deviceId){ - for(let prop in newSettings){ - if(device.hasOwnProperty(prop)){ - if(prop==="name"){ - if(checkMaxLength(newSettings[prop])){ - device[prop] = newSettings[prop]; - } - else{ - alert("Name must be less than " + DEVICE_NAME_MAX_LENGTH + " characters."); - } - }else{ - device[prop] = newSettings[prop]; + console.log(newSettings.name, " <-- new name --> ", deviceId ); + this.props.devices.map(device => { + if(device.id === deviceId){ + for(let prop in newSettings){ + if(device.hasOwnProperty(prop)){ + if(prop==="name"){ + if(checkMaxLength(newSettings[prop])){ + device[prop] = newSettings[prop]; + } + else{ + alert("Name must be less than " + DEVICE_NAME_MAX_LENGTH + " characters."); + } + }else{ + device[prop] = newSettings[prop]; + } + + } + } } + return null; + }); + this.forceUpdate(); + }; - } - } - } - return null; - }); - this.forceUpdate(); - }; + addDevice(data) { + const ds = this.props.addDevice(data); + this.setState({ + devices: ds + }); + } - render() { - return ( -
- - 0 ? this.state.devices.length : 1} divided="vertically"> - { - this.state.devices ? - this.state.devices.map((e, i) => { - return ( - - - - ) - }) - : - null - } - - - - -
- ) - } + render() { + const ds = this.state.devices ? this.state.devices : this.props.devices; + return ( +
+ + 0 ? ds.length : 1} divided="vertically"> + { + ds ? + ds.map((e, i) => { + return ( + + + + ) + }) + : + null + } + + + + +
+ ) + } -} +} diff --git a/smart-hut/src/components/dashboard/devices/DeviceTypeController.js b/smart-hut/src/components/dashboard/devices/DeviceTypeController.js index 2b1cc08..bb8de4d 100644 --- a/smart-hut/src/components/dashboard/devices/DeviceTypeController.js +++ b/smart-hut/src/components/dashboard/devices/DeviceTypeController.js @@ -1,4 +1,4 @@ -import React, { Component } from 'react'; +import React from 'react'; import Light from "./Light"; import SmartPlug from "./SmartPlug"; import Sensor from "./Sensor"; @@ -9,8 +9,8 @@ import Switcher from "./Switch"; const DeviceType = (props) => { switch(props.type) { - case "light": - return + case "regular-light": + return () case "sensor": return case "dimmer": @@ -19,6 +19,10 @@ const DeviceType = (props) => { return case "switch": return + case "light": + return + default: + return "" } } diff --git a/smart-hut/src/components/dashboard/devices/NewDevice.js b/smart-hut/src/components/dashboard/devices/NewDevice.js index 122ee96..77aca2e 100644 --- a/smart-hut/src/components/dashboard/devices/NewDevice.js +++ b/smart-hut/src/components/dashboard/devices/NewDevice.js @@ -25,6 +25,7 @@ export default class NewDevice extends Component { openModal : false }; this.baseState = this.state + this.createDevice = this.createDevice.bind(this); } handleOpen = () => {this.setState({openModal : true})}; @@ -59,8 +60,16 @@ export default class NewDevice extends Component { this.setState({lightsAttached : d.value}); }; - createDevice = () => { + createDevice() { // Connect to the backend and create device here. + const data = { + params: { + "name": this.state.deviceName, + }, + device: this.state.typeOfDevice + + } + this.props.addDevice(data); this.resetState(); }; @@ -70,7 +79,7 @@ export default class NewDevice extends Component { { key: 'light', text: 'Normal Light', - value: 'light', + value: 'regularLight', image: {avatar: true, src: '/img/lightOn.svg'}, }, { diff --git a/smart-hut/src/components/dashboard/devices/SmartPlugStyle.js b/smart-hut/src/components/dashboard/devices/SmartPlugStyle.js index ea74700..26b4b9e 100644 --- a/smart-hut/src/components/dashboard/devices/SmartPlugStyle.js +++ b/smart-hut/src/components/dashboard/devices/SmartPlugStyle.js @@ -1,4 +1,3 @@ -import {iconStyle} from "./styleComponents"; export const energyConsumedStyle = { color : "black", diff --git a/smart-hut/src/components/modalform.js b/smart-hut/src/components/modalform.js index 79ca7da..682bd12 100644 --- a/smart-hut/src/components/modalform.js +++ b/smart-hut/src/components/modalform.js @@ -1,5 +1,5 @@ import React, { Component } from 'react'; -import { Button, Header, Image, Modal, Checkbox, Form, Grid, Input, Icon } from 'semantic-ui-react' +import { Button, Header, Modal, Form, Input, Icon } from 'semantic-ui-react' import SelectIcons from "./SelectIcons"; export default class ModalWindow extends Component { @@ -39,10 +39,7 @@ export default class ModalWindow extends Component { } deleteRoom = (e) => { - let data = { - "id" : this.state.id - } - this.props.deleteRoom(data); + this.props.deleteRoom(); this.closeModal(); } @@ -77,40 +74,39 @@ export default class ModalWindow extends Component { return (
- {this.props.type == "new" ? + {this.props.type === "new" ? : - + null } -
{this.props.type == "new" ? "Add new room" : "Modify room" }
+
{this.props.type === "new" ? "Add new room" : "Modify room" }

Insert the name of the room:

+ value={this.props.type === "new" ? null : this.props.idRoom.name }/>

Insert an image of the room:

+ value={this.props.type === "new" ? null : this.props.idRoom.images }/>

Select an icon:

- {this.props.type == "new" ? "home" : this.props.idRoom.icon } - +
- {this.props.type == "modify" ? + {this.props.type === "modify" ? - diff --git a/smart-hut/src/views/ConfirmForgotPassword.js b/smart-hut/src/views/ConfirmForgotPassword.js index e364f0d..7de5e16 100644 --- a/smart-hut/src/views/ConfirmForgotPassword.js +++ b/smart-hut/src/views/ConfirmForgotPassword.js @@ -1,14 +1,7 @@ import React, { Component } from "react"; -import { render } from "react-dom"; import HomeNavbar from "./../components/HomeNavbar"; import { - Container, - Icon, Image, - Menu, - Sidebar, - Responsive, - Header, Divider, Message, Grid diff --git a/smart-hut/src/views/ConfirmRegistration.js b/smart-hut/src/views/ConfirmRegistration.js index 9bec496..b1c5652 100644 --- a/smart-hut/src/views/ConfirmRegistration.js +++ b/smart-hut/src/views/ConfirmRegistration.js @@ -1,14 +1,7 @@ import React, { Component } from "react"; -import { render } from "react-dom"; import HomeNavbar from "./../components/HomeNavbar"; import { - Container, - Icon, Image, - Menu, - Sidebar, - Responsive, - Header, Divider, Message, Grid diff --git a/smart-hut/src/views/Dashboard.js b/smart-hut/src/views/Dashboard.js index e9d4253..957c204 100644 --- a/smart-hut/src/views/Dashboard.js +++ b/smart-hut/src/views/Dashboard.js @@ -25,62 +25,101 @@ export default class Dashboard extends Component{ }; this.addRoom = this.addRoom.bind(this); + this.deleteRoom = this.deleteRoom.bind(this); + this.addDevice = this.addDevice.bind(this); this.handleItemClick = this.handleItemClick.bind(this); } componentDidMount() { call.getAllRooms(this.props.tkn) .then(res => { - this.setState({ - rooms: res.data - }); + if (res.status === 200){ + this.setState({ + rooms: res.data + }); + } }).catch(err => { - console.log(err); - }); + console.log(err); + }); call.getAllDevices(this.props.tkn) .then(res => { - this.setState({ - devices: res.data - }); + if (res.status === 200) { + this.setState({ + devices: res.data + }); + } }).catch(err => { - console.log(err); - }); + console.log(err); + }); } addRoom(data) { call.createRoom(data) .then(res => { - console.log(res); if (res.status === 200 && res.data) { this.setState(state => ({ rooms: state.rooms.concat([res.data]) })); } }).catch(err => { - console.log(err); - }); + console.log(err); + }); }; - deleteRoom(id) { - call.deleteRoom(id) + deleteRoom() { + let data = { + id : this.state.activeItem + } + call.deleteRoom(data) .then(res => { //remove room in state.rooms }).catch(err => { - console.log(err); - }); + console.log(err); + }); } handleItemClick(id) { // el -> obj of name and id //da fare richiesta get della room e settare activeItem - call.getAllDevicesByRoom(id, this.props.tkn) - .then(res => { - this.setState({ - devices: res.data - }); - }).catch(err => { - console.log(err); + this.setState({ + activeItem: id }); + if ( id === -1) { + call.getAllDevices(this.props.tkn) + .then(res => { + if ( res.status === 200) { + this.setState({ + devices: res.data + }); + } + }).catch(err => { + console.log(err); + }); + } else { + call.getAllDevicesByRoom(id, this.props.tkn) + .then(res => { + if (res.status === 200) { + this.setState({ + devices: res.data + }); + } + }).catch(err => { + console.log(err); + }); + } + } + + addDevice(data) { + data.params["roomId"] = this.state.activeItem; + call.devicePost(data, this.props.tkn) + .then(res => { + this.setState(state => ({ + devices: state.devices.concat([res.data]) + })); + }).catch(err => { + + }); + return this.state.devices; } render () { @@ -94,11 +133,11 @@ export default class Dashboard extends Component{ - + - + diff --git a/smart-hut/src/views/Forgot-pass-reset.js b/smart-hut/src/views/Forgot-pass-reset.js index 170bf1d..0989b91 100644 --- a/smart-hut/src/views/Forgot-pass-reset.js +++ b/smart-hut/src/views/Forgot-pass-reset.js @@ -1,6 +1,7 @@ import React, {Component} from 'react'; import {Button, Form, Grid, Header, Image, Icon, Message} from 'semantic-ui-react'; import { call } from '../client_server'; +import { Redirect } from "react-router-dom"; export default class ChangePass extends Component { @@ -13,6 +14,7 @@ export default class ChangePass extends Component { message: "", } } + this.handleChangePassword = this.handleChangePassword.bind(this); } onChangeHandler = (event) => { @@ -32,14 +34,16 @@ export default class ChangePass extends Component { handleChangePassword = (e) => { const params = { - "confirmationToken" : this.props.query.confirmationToken , + "confirmationToken" : this.props.query.token , "password" : this.state.password } call.resetPassword(params) .then(res => { - if (res.status === "Errore") { + if (res.status !== 200){ this.setState({ error: { state: true, message: "Errore"}}); + } else { + return } // else set a message that an email has been sent }).catch(err => { diff --git a/smart-hut/src/views/Home.js b/smart-hut/src/views/Home.js index 4937938..2cb4cff 100644 --- a/smart-hut/src/views/Home.js +++ b/smart-hut/src/views/Home.js @@ -4,7 +4,6 @@ import HomeNavbar from './../components/HomeNavbar'; import { Button, Container, - Divider, Grid, Header, Icon, @@ -70,7 +69,6 @@ class DesktopContainer extends Component { render() { const { children } = this.props - const { fixed } = this.state return ( diff --git a/smart-hut/src/views/Navbar.js b/smart-hut/src/views/Navbar.js index 121d4b7..de3f183 100644 --- a/smart-hut/src/views/Navbar.js +++ b/smart-hut/src/views/Navbar.js @@ -1,5 +1,5 @@ import React, { Component } from 'react'; -import { Menu, Grid, Icon, Button, Header, Image, Modal, Responsive, Segment } from "semantic-ui-react"; +import { Menu, Grid, Icon, Responsive, Segment } from "semantic-ui-react"; import {editButtonStyle} from "../components/dashboard/devices/styleComponents"; import ModalWindow from "../components/modalform"; @@ -19,7 +19,14 @@ class Navbar extends Component { handleClick = (e, {id}) => { console.log(id); + let obj = undefined; + this.props.rooms.forEach((e) => { + if (e.id === id) { + obj = e; + } + }); this.setState({ activeItem: id, + activeRoom: obj }); this.props.handleItemClick(id) } @@ -38,6 +45,7 @@ class Navbar extends Component { - //dropdown menu +
diff --git a/smart-hut/src/views/NavbarTest.js b/smart-hut/src/views/NavbarTest.js deleted file mode 100644 index c108c20..0000000 --- a/smart-hut/src/views/NavbarTest.js +++ /dev/null @@ -1,12 +0,0 @@ -import React, {Component} from 'react'; -import {Button } from 'semantic-ui-react'; - - -export default class NavbarTest extends Component { - - render() { - return ( -

Ciao

- ) - } -} \ No newline at end of file diff --git a/smart-hut/src/views/TestHeaderController.js b/smart-hut/src/views/TestHeaderController.js index 6293908..b0afedb 100644 --- a/smart-hut/src/views/TestHeaderController.js +++ b/smart-hut/src/views/TestHeaderController.js @@ -1,15 +1,6 @@ import _ from "lodash"; -import React, { Component } from "react"; +import React from "react"; import HeaderController from "./../components/HeaderController"; -import { render } from "react-dom"; -import { - Container, - Icon, - Image, - Menu, - Sidebar, - Responsive -} from "semantic-ui-react"; export default class TestHeaderController extends React.Component { render () {