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

109 lines
2.5 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'
/*
rooms -> actual rooms
activeItem -> the current room in view
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);
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-18 11:46:28 +00:00
this.setState({
rooms: res.data
});
2020-03-11 16:38:04 +00:00
}).catch(err => {
console.log(err);
});
2020-03-17 15:20:43 +00:00
call.getAllDevices(this.props.tkn)
.then(res => {
2020-03-18 12:20:01 +00:00
this.setState({
devices: res.data
});
2020-03-17 15:20:43 +00:00
}).catch(err => {
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 => {
console.log(res);
if (res.status === 200 && res.data) {
this.setState(state => ({
rooms: state.rooms.concat([res.data])
}));
}
}).catch(err => {
console.log(err);
});
};
2020-03-14 14:48:33 +00:00
deleteRoom(id) {
2020-03-17 15:20:43 +00:00
call.deleteRoom(id)
.then(res => {
//remove room in state.rooms
}).catch(err => {
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-18 11:46:28 +00:00
call.getAllDevicesByRoom(id, this.props.tkn)
2020-03-17 15:20:43 +00:00
.then(res => {
2020-03-18 11:46:28 +00:00
this.setState({
devices: res.data
});
2020-03-17 15:20:43 +00:00
}).catch(err => {
console.log(err);
});
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-14 14:48:33 +00:00
<Grid.Row color='black'>
<Grid.Column>
2020-03-18 11:46:28 +00:00
<MyHeader logout={this.props.logout} />
2020-03-14 14:48:33 +00:00
</Grid.Column>
</Grid.Row>
2020-03-13 14:12:28 +00:00
<Grid.Row color='black'>
2020-03-11 16:38:04 +00:00
<Grid.Column width={3}>
2020-03-14 14:48:33 +00:00
<Navbar addRoom={this.addRoom} rooms={this.state.rooms} handleItemClick={this.handleItemClick}/>
2020-03-11 16:38:04 +00:00
</Grid.Column>
<Grid.Column width={13}>
2020-03-17 15:20:43 +00:00
<DevicePanel 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>
)
}
}