frontend/smart-hut/src/views/Dashboard.js

150 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-03-11 16:38:04 +00:00
import React, {Component} from 'react';
import DevicePanel from "../components/dashboard/DevicePanel";
2020-03-14 14:48:33 +00:00
import Navbar from './Navbar'
2020-03-13 14:12:28 +00:00
import MyHeader from '../components/HeaderController'
2020-03-11 16:38:04 +00:00
import { call } from '../client_server';
2020-03-17 15:20:43 +00:00
import { Grid } from 'semantic-ui-react'
2020-03-18 14:30:02 +00:00
/*
2020-03-17 15:20:43 +00:00
rooms -> actual rooms
2020-03-18 14:30:02 +00:00
activeItem -> the current room in view
2020-03-17 15:20:43 +00:00
devices -> current device in current room view
2020-03-13 14:12:28 +00:00
2020-03-11 16:38:04 +00:00
2020-03-17 15:20:43 +00:00
id of Home is -1
*/
2020-03-11 16:38:04 +00:00
export default class Dashboard extends Component{
constructor(props) {
super(props);
this.state = {
rooms: [],
2020-03-17 15:20:43 +00:00
activeItem: -1,
devices: [],
tkn: this.props.tkn,
2020-03-11 16:38:04 +00:00
};
this.addRoom = this.addRoom.bind(this);
2020-03-19 10:52:13 +00:00
this.deleteRoom = this.deleteRoom.bind(this);
this.addDevice = this.addDevice.bind(this);
2020-03-11 16:38:04 +00:00
this.handleItemClick = this.handleItemClick.bind(this);
}
2020-03-13 14:12:28 +00:00
componentDidMount() {
2020-03-14 14:48:33 +00:00
call.getAllRooms(this.props.tkn)
2020-03-11 16:38:04 +00:00
.then(res => {
2020-03-19 10:52:13 +00:00
if (res.status === 200){
this.setState({
rooms: res.data
});
}
2020-03-11 16:38:04 +00:00
}).catch(err => {
2020-03-19 10:52:13 +00:00
console.log(err);
});
2020-03-18 14:30:02 +00:00
call.getAllDevices(this.props.tkn)
2020-03-17 15:20:43 +00:00
.then(res => {
2020-03-19 10:52:13 +00:00
if (res.status === 200) {
this.setState({
devices: res.data
});
}
2020-03-11 16:38:04 +00:00
}).catch(err => {
2020-03-19 10:52:13 +00:00
console.log(err);
});
2020-03-11 16:38:04 +00:00
}
2020-03-14 14:48:33 +00:00
addRoom(data) {
call.createRoom(data)
2020-03-11 16:38:04 +00:00
.then(res => {
if (res.status === 200 && res.data) {
this.setState(state => ({
rooms: state.rooms.concat([res.data])
}));
}
}).catch(err => {
2020-03-19 10:52:13 +00:00
console.log(err);
});
2020-03-11 16:38:04 +00:00
};
2020-03-19 10:52:13 +00:00
deleteRoom() {
let data = {
id : this.state.activeItem
}
call.deleteRoom(data)
2020-03-17 15:20:43 +00:00
.then(res => {
//remove room in state.rooms
}).catch(err => {
2020-03-19 10:52:13 +00:00
console.log(err);
});
2020-03-14 14:48:33 +00:00
}
2020-03-18 11:46:28 +00:00
handleItemClick(id) {
2020-03-17 15:20:43 +00:00
// el -> obj of name and id
2020-03-11 16:38:04 +00:00
//da fare richiesta get della room e settare activeItem
2020-03-19 10:52:13 +00:00
this.setState({
activeItem: id
2020-03-18 14:30:02 +00:00
});
2020-03-19 10:52:13 +00:00
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) {
2020-03-20 17:42:38 +00:00
console.log(res.data);
2020-03-19 10:52:13 +00:00
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])
}));
2020-03-20 17:42:38 +00:00
return this.state.devices;
2020-03-19 10:52:13 +00:00
}).catch(err => {
});
2020-03-11 16:38:04 +00:00
}
2020-03-17 16:38:03 +00:00
2020-03-20 17:42:38 +00:00
2020-03-11 16:38:04 +00:00
render () {
return(
2020-03-13 14:12:28 +00:00
<div style={{height : "110vh", background: '#1b1c1d'}}>
<Grid >
2020-03-18 13:18:37 +00:00
<Grid.Row color='black'>
2020-03-18 14:30:02 +00:00
<Grid.Column>
<MyHeader logout={this.props.logout} />
</Grid.Column>
</Grid.Row>
<Grid.Row color='black'>
2020-03-11 16:38:04 +00:00
<Grid.Column width={3}>
2020-03-19 10:52:13 +00:00
<Navbar addRoom={this.addRoom} deleteRoom={this.deleteRoom} rooms={this.state.rooms} handleItemClick={this.handleItemClick}/>
2020-03-11 16:38:04 +00:00
</Grid.Column>
2020-03-17 16:38:03 +00:00
2020-03-11 16:38:04 +00:00
<Grid.Column width={13}>
2020-03-20 17:42:38 +00:00
<DevicePanel tkn={this.props.tkn} activeItem={this.state.activeItem} addDevice={this.addDevice} devices={this.state.devices} />
2020-03-13 14:12:28 +00:00
</Grid.Column>
2020-03-11 16:38:04 +00:00
</Grid.Row>
</Grid>
</div>
)
}
}