diff --git a/smart-hut/src/App.js b/smart-hut/src/App.js index f27fe21..9810d84 100644 --- a/smart-hut/src/App.js +++ b/smart-hut/src/App.js @@ -29,7 +29,9 @@ class App extends Component { try { let userJsonString = localStorage.getItem("token"); - if (userJsonString) { + let exp = localStorage.getItem("exp"); + let date = new Date().getTime(); + if (userJsonString && exp && date < exp) { loggedIn = true; } }catch(exception) { @@ -63,6 +65,7 @@ class App extends Component { .then(res => { if (res.data && res.status === 200) { localStorage.setItem("token", res.data.jwttoken); + localStorage.setItem("exp", new Date().getTime() + (60*60*5)) call.setToken(res.data.jwttoken); this.setState( { diff --git a/smart-hut/src/client_server.js b/smart-hut/src/client_server.js index 6df5b30..67eb2d0 100644 --- a/smart-hut/src/client_server.js +++ b/smart-hut/src/client_server.js @@ -63,7 +63,7 @@ export var call = { return axios.post(config + 'room', data, { headers: { Authorization : "Bearer " + tkn } }) }, updateRoom: function(data, headers) { - return axios.put(config + 'room/'+data.id) + return axios.put(config + 'room?name='+ data.name, data, { headers: { Authorization : "Bearer " + tkn } }) }, deleteRoom: function(data, headers) { return axios.delete(config + 'room/'+data.id, { headers: { Authorization : "Bearer " + tkn } }); @@ -71,8 +71,12 @@ export var call = { devicePost: function(data, headers) { return axios.post(config + data.device, data.params, { headers: { Authorization : "Bearer " + tkn } }) }, - deviceUpdate: function(data, headers) { - return axios.put(config + 'device', data, { headers: { Authorization : "Bearer " + tkn } }) + deviceUpdate: function(data, typeDevice) { + let url = 'device'; + if (typeDevice) { + url = typeDevice; + } + return axios.put(config + url, data, { headers: { Authorization : "Bearer " + tkn } }) }, deviceDelete: function(data, headers) { return axios.delete(config + data.device + '/' + data.id, {}, { headers: { Authorization : "Bearer " + tkn } }) diff --git a/smart-hut/src/components/dashboard/DevicePanel.js b/smart-hut/src/components/dashboard/DevicePanel.js index 52466d6..b4a0e7f 100644 --- a/smart-hut/src/components/dashboard/DevicePanel.js +++ b/smart-hut/src/components/dashboard/DevicePanel.js @@ -68,7 +68,7 @@ export default class DevicePanel extends Component { console.log(err); }); } else { - call.getAllDevicesByRoom(this.state.settingsDeviceId) + call.getAllDevicesByRoom(this.props.activeItem) .then(res => { if (res.status === 200) { this.setState({ @@ -98,6 +98,7 @@ export default class DevicePanel extends Component { .then(res => { if (res.status === 200) { this.getDevices(); + this.forceUpdate(); } }).catch(err => { diff --git a/smart-hut/src/components/dashboard/devices/Light.js b/smart-hut/src/components/dashboard/devices/Light.js index c307da6..d358ab7 100644 --- a/smart-hut/src/components/dashboard/devices/Light.js +++ b/smart-hut/src/components/dashboard/devices/Light.js @@ -12,6 +12,7 @@ import Settings from "./DeviceSettings"; import {Image} from "semantic-ui-react"; import {CircularInput, CircularProgress, CircularThumb, CircularTrack} from "react-circular-input"; import {valueStyle, intensityLightStyle, style, nameStyle} from "./LightStyle"; +import { call } from '../../../client_server'; export default class Light extends Component { constructor(props) { @@ -28,7 +29,14 @@ export default class Light extends Component { }; setIntensity = (newValue) => { - this.setState({intensity: newValue}); + this.props.device.intensity = Math.round(newValue) <= 1 ? 1 : Math.round(newValue ); + console.log(this.props.device.intensity) + call.deviceUpdate(this.props.device, 'dimmableLight') + .then(res => { + if (res.status === 200 ) { + this.setState({intensity: newValue}); + } + }) }; getIcon = () => { @@ -52,7 +60,7 @@ export default class Light extends Component { render() { const intensityLightView = ( @@ -61,7 +69,7 @@ export default class Light extends Component { - {Math.round(this.state.intensity * 100)}% + {Math.round(this.props.device.intensity)}% {this.props.device.name} @@ -83,7 +91,7 @@ export default class Light extends Component { deviceId={this.props.device.id} edit={this.props.edit} onChangeData={(id, newSettings) => this.props.onChangeData(id, newSettings)}/> - {this.state.intensity ? (intensityLightView) : (normalLightView)} + {this.props.device.intensity ? (intensityLightView) : (normalLightView)} ) } diff --git a/smart-hut/src/components/modalform.js b/smart-hut/src/components/modalform.js index 682bd12..699da5d 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, Modal, Form, Input, Icon } from 'semantic-ui-react' +import { Button, Header, Modal, Form, Input, Icon, Responsive } from 'semantic-ui-react' import SelectIcons from "./SelectIcons"; export default class ModalWindow extends Component { @@ -22,7 +22,7 @@ export default class ModalWindow extends Component { let data = { "icon" : this.state.selectedIcon, "name" : this.state.name, - "images" : this.state.img + "image" : this.state.img } this.props.addRoom(data); this.closeModal(); @@ -30,9 +30,9 @@ export default class ModalWindow extends Component { modifyRoomModal = (e) => { let data = { - "icon" : this.state.selectedIcon, - "name" : this.state.name, - "images" : this.state.img + "icon" : this.state.selectedIcon === "" ? this.props.idRoom.icon : this.state.selectedIcon , + "name" : this.state.name === ""? this.props.idRoom.name : this.state.name, + "image" : this.state.img } this.props.updateRoom(data); this.closeModal(); @@ -44,6 +44,7 @@ export default class ModalWindow extends Component { } changeSomething = (event) => { + let nam = event.target.name; let val = event.target.value; this.setState({[nam]: val}); @@ -73,15 +74,30 @@ export default class ModalWindow extends Component { return (
- - {this.props.type === "new" ? - - : - null - } + + {this.props.type === "new" ? + + : + + } + + + { + this.props.type === "new" ? + + : + + } + Insert the name of the room:

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

Insert an image of the room:

+ value={this.props.type === "new" && this.props.idRoom !== -1 ? null : this.props.idRoom.images }/> diff --git a/smart-hut/src/views/Dashboard.js b/smart-hut/src/views/Dashboard.js index f84c813..e83e1f0 100644 --- a/smart-hut/src/views/Dashboard.js +++ b/smart-hut/src/views/Dashboard.js @@ -4,7 +4,7 @@ import Navbar from './Navbar' import MyHeader from '../components/HeaderController' import { call } from '../client_server'; -import { Grid } from 'semantic-ui-react' +import { Grid, Responsive } from 'semantic-ui-react' /* rooms -> actual rooms activeItem -> the current room in view @@ -26,40 +26,58 @@ export default class Dashboard extends Component{ this.addRoom = this.addRoom.bind(this); this.deleteRoom = this.deleteRoom.bind(this); + this.updateRoom = this.updateRoom.bind(this); this.addDevice = this.addDevice.bind(this); this.handleItemClick = this.handleItemClick.bind(this); } + getDevices() { + if (this.state.activeItem === -1) { + call.getAllDevices() + .then(res => { + if ( res.status === 200) { + this.setState({ + devices: res.data + }); + } + }).catch(err => { + console.log(err); + }); + } else { + call.getAllDevicesByRoom(this.state.activeItem) + .then(res => { + if (res.status === 200) { + this.setState({ + devices: res.data + }); + } + }).catch(err => { + + }); + } +} + +getRooms() { + call.getAllRooms(this.props.tkn) + .then(res => { + this.setState({ + rooms: res.data + }) + }).catch(err => { + + }); +} + componentDidMount() { - call.getAllRooms(this.props.tkn) - .then(res => { - if (res.status === 200){ - this.setState({ - rooms: res.data - }); - } - }).catch(err => { - console.log(err); - }); - call.getAllDevices(this.props.tkn) - .then(res => { - if (res.status === 200) { - this.setState({ - devices: res.data - }); - } - }).catch(err => { - console.log(err); - }); + this.getRooms(); + this.getDevices(); } addRoom(data) { call.createRoom(data) .then(res => { if (res.status === 200 && res.data) { - this.setState(state => ({ - rooms: state.rooms.concat([res.data]) - })); + this.getRooms(); } }).catch(err => { console.log(err); @@ -73,11 +91,25 @@ export default class Dashboard extends Component{ call.deleteRoom(data) .then(res => { //remove room in state.rooms + this.getRooms(); }).catch(err => { console.log(err); }); } + updateRoom(data) { + console.log(data) + call.updateRoom(data) + .then(res => { + if (res.status === 200 && res.data) { + this.getRooms(); + this.forceUpdate(); + } + }).catch(err => { + + }) + } + handleItemClick(id) { // el -> obj of name and id //da fare richiesta get della room e settare activeItem @@ -99,10 +131,10 @@ export default class Dashboard extends Component{ call.getAllDevicesByRoom(id, this.props.tkn) .then(res => { if (res.status === 200) { - console.log(res.data); this.setState({ devices: res.data }); + this.forceUpdate(); } }).catch(err => { console.log(err); @@ -127,22 +159,43 @@ export default class Dashboard extends Component{ render () { return(
- - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + + + + + + + + + + + + + + + + + + + + +
) } diff --git a/smart-hut/src/views/FourOhFour.js b/smart-hut/src/views/FourOhFour.js index 3913981..bc9ea5e 100644 --- a/smart-hut/src/views/FourOhFour.js +++ b/smart-hut/src/views/FourOhFour.js @@ -1,5 +1,5 @@ import React, {Component} from 'react'; -import { Header, Grid, Message, Button, Segment, Responsive, Image} from 'semantic-ui-react'; +import { Grid, Button, Segment, Responsive, Image} from 'semantic-ui-react'; import {Link } from "react-router-dom"; import MyHeader from '../components/HeaderController' diff --git a/smart-hut/src/views/Instruction.js b/smart-hut/src/views/Instruction.js index 6a58d4a..e1ad53f 100644 --- a/smart-hut/src/views/Instruction.js +++ b/smart-hut/src/views/Instruction.js @@ -1,16 +1,9 @@ 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 } from "semantic-ui-react"; diff --git a/smart-hut/src/views/Navbar.js b/smart-hut/src/views/Navbar.js index 41dfcfb..11d48e0 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, Dropdown } from "semantic-ui-react"; +import { Menu, Grid, Icon, Responsive, Segment, Dropdown } from "semantic-ui-react"; import {editButtonStyle} from "../components/dashboard/devices/styleComponents"; import ModalWindow from "../components/modalform"; @@ -7,8 +7,8 @@ class Navbar extends Component { constructor(props) { super(props); this.state = { - activeItem: 'Home', activeItemName: 'Home', + activeItem: -1, edited: "", editMode : false } @@ -24,18 +24,19 @@ class Navbar extends Component { obj = e; } }); - this.setState({ - activeItem: id, - activeItemName: name, - activeRoom: obj - }); + + this.setState({ + activeItem: id, + activeRoom: obj, + activeItemName: name + }); this.props.handleItemClick(id) } render(){ //const { activeItem } = this.state return ( -
+
@@ -48,7 +49,7 @@ class Navbar extends Component { key={-1} id={-1} name='Home' - active={this.state.activeItem === 'Home'} + active={this.state.activeItem === -1} onClick={this.handleClick} > @@ -83,10 +84,10 @@ class Navbar extends Component { {e.name} - {this.state.editMode ? - - : null - } + {this.state.editMode ? + + : null + } @@ -162,23 +163,21 @@ class Navbar extends Component { - - - - - - - - - - - + { + this.state.activeItem !== -1 ? + + + + + + + + + + + : + null + }