Merge branch '48-add-update-lights' into 'dev'

added update on lights

Closes #48

See merge request sa4-2020/the-sanmarinoes/frontend!49
This commit is contained in:
Matteo Omenetti 2020-03-23 10:21:35 +01:00
commit 52b1e12d73
7 changed files with 29 additions and 11 deletions

View file

@ -31,8 +31,12 @@ class App extends Component {
let userJsonString = localStorage.getItem("token"); let userJsonString = localStorage.getItem("token");
let exp = localStorage.getItem("exp"); let exp = localStorage.getItem("exp");
let date = new Date().getTime(); let date = new Date().getTime();
console.log(exp > date, date)
if (userJsonString && exp && date < exp) { if (userJsonString && exp && date < exp) {
loggedIn = true; loggedIn = true;
} else {
localStorage.removeItem("token");
localStorage.removeItem("exp");
} }
}catch(exception) { }catch(exception) {
@ -64,8 +68,9 @@ class App extends Component {
return call.login(data.params) return call.login(data.params)
.then(res => { .then(res => {
if (res.data && res.status === 200) { if (res.data && res.status === 200) {
let expire = new Date().getTime()+60*60*5*1000;
localStorage.setItem("token", res.data.jwttoken); localStorage.setItem("token", res.data.jwttoken);
localStorage.setItem("exp", new Date().getTime() + (60*60*5)) localStorage.setItem("exp", expire)
call.setToken(res.data.jwttoken); call.setToken(res.data.jwttoken);
this.setState( this.setState(
{ {
@ -94,6 +99,7 @@ class App extends Component {
}); });
localStorage.removeItem("token"); localStorage.removeItem("token");
localStorage.removeItem("exp");
}; };
render() { render() {

View file

@ -94,6 +94,7 @@ export default class DevicePanel extends Component {
console.log(roomId) console.log(roomId)
data["id"] = this.state.settingsDeviceId; data["id"] = this.state.settingsDeviceId;
data["roomId"] = roomId; data["roomId"] = roomId;
console.log(data);
call.deviceUpdate(data) call.deviceUpdate(data)
.then(res => { .then(res => {
if (res.status === 200) { if (res.status === 200) {

View file

@ -8,7 +8,7 @@ import Switcher from "./Switch";
const DeviceType = (props) => { const DeviceType = (props) => {
switch(props.type) { switch(props.type) {
case "regular-light": case "regularLight":
return (<Light onChangeData={props.changeDeviceData} device={props.device} edit={props.edit} />) return (<Light onChangeData={props.changeDeviceData} device={props.device} edit={props.edit} />)
case "sensor": case "sensor":
return <Sensor onChangeData={props.changeDeviceData} device={props.device} edit={props.edit} /> return <Sensor onChangeData={props.changeDeviceData} device={props.device} edit={props.edit} />
@ -18,7 +18,7 @@ const DeviceType = (props) => {
return <SmartPlug onChangeData={props.changeDeviceData} device={props.device} edit={props.edit} /> return <SmartPlug onChangeData={props.changeDeviceData} device={props.device} edit={props.edit} />
case "switch": case "switch":
return <Switcher onChangeData={props.changeDeviceData} device={props.device} edit={props.edit} /> return <Switcher onChangeData={props.changeDeviceData} device={props.device} edit={props.edit} />
case "light": case "dimmableLight":
return (<Light onChangeData={props.changeDeviceData} device={props.device} edit={props.edit} />) return (<Light onChangeData={props.changeDeviceData} device={props.device} edit={props.edit} />)
default: default:
return "" return ""

View file

@ -25,11 +25,17 @@ export default class Light extends Component {
} }
onClickDevice = () => { onClickDevice = () => {
this.setState((prevState) => ({turnedOn: !prevState.turnedOn})); this.props.device.on = !this.state.turnedOn;
call.deviceUpdate(this.props.device, 'regularLight')
.then(res => {
if (res.status === 200 ){
this.setState((prevState) => ({turnedOn: !prevState.turnedOn}));
}
})
}; };
setIntensity = (newValue) => { setIntensity = (newValue) => {
this.props.device.intensity = Math.round(newValue) <= 1 ? 1 : Math.round(newValue ); this.props.device.intensity = Math.round(newValue * 100) <= 1 ? 1 : Math.round(newValue * 100);
console.log(this.props.device.intensity) console.log(this.props.device.intensity)
call.deviceUpdate(this.props.device, 'dimmableLight') call.deviceUpdate(this.props.device, 'dimmableLight')
.then(res => { .then(res => {
@ -52,6 +58,9 @@ export default class Light extends Component {
intensity: this.props.device.intensity intensity: this.props.device.intensity
}); });
} }
this.setState({
turnedOn: this.props.device.on
});
// Get the state and update it // Get the state and update it
} }
@ -60,7 +69,7 @@ export default class Light extends Component {
render() { render() {
const intensityLightView = ( const intensityLightView = (
<CircularInput <CircularInput
value={this.props.device.intensity} value={Math.round(this.props.device.intensity/100)}
onChange={this.setIntensity} onChange={this.setIntensity}
style={style} style={style}
> >

View file

@ -108,12 +108,12 @@ export default class ModalWindow extends Component {
<p>Insert the name of the room:</p> <p>Insert the name of the room:</p>
<Form.Field> <Form.Field>
<Input label='Room name' placeholder='Room Name' name="name" type='text' onChange={this.changeSomething} <Input label='Room name' placeholder='Room Name' name="name" type='text' onChange={this.changeSomething}
value={this.props.type === "new" && this.props.idRoom !== -1 ? null : this.props.idRoom.name }/> value={this.props.type === "new" && this.props.idRoom !== -1 ? "" : "" }/>
</Form.Field> </Form.Field>
<p>Insert an image of the room:</p> <p>Insert an image of the room:</p>
<Form.Field> <Form.Field>
<Input label='Room image' type='file' name="img" onChange={this.changeSomething} <Input label='Room image' type='file' name="img" onChange={this.changeSomething}
value={this.props.type === "new" && this.props.idRoom !== -1 ? null : this.props.idRoom.images }/> value={this.props.type === "new" && this.props.idRoom !== -1 ? "" : this.props.idRoom.images }/>
</Form.Field> </Form.Field>
</Form> </Form>
@ -126,7 +126,7 @@ export default class ModalWindow extends Component {
<Button icon labelPosition='left' inverted color='red' onClick={this.deleteRoom}> <Button icon labelPosition='left' inverted color='red' onClick={this.deleteRoom}>
<Icon name='trash alternate' /> <Icon name='trash alternate' />
Delete room Delete room
</Button> : null } </Button> : "" }
</Modal.Content> </Modal.Content>
<Modal.Actions> <Modal.Actions>

View file

@ -36,6 +36,7 @@ export default class Dashboard extends Component{
call.getAllDevices() call.getAllDevices()
.then(res => { .then(res => {
if ( res.status === 200) { if ( res.status === 200) {
console.log(res.data)
this.setState({ this.setState({
devices: res.data devices: res.data
}); });

View file

@ -120,6 +120,7 @@ class Navbar extends Component {
<Dropdown.Item <Dropdown.Item
key={-1} key={-1}
id={-1}
name='Home' name='Home'
active={this.state.activeItem === 'Home'} active={this.state.activeItem === 'Home'}
onClick={this.handleClick}> onClick={this.handleClick}>
@ -168,10 +169,10 @@ class Navbar extends Component {
<Grid> <Grid>
<Grid.Row> <Grid.Row>
<Grid.Column width={8}> <Grid.Column width={8}>
<ModalWindow type="new" idRoom={this.state.activeItem === -1? -1 : this.state.activeRoom} updateRoom={this.props.updateRoom} deleteRoom={this.props.deleteRoom}/> <ModalWindow type="new" idRoom={this.state.activeRoom} updateRoom={this.props.updateRoom} deleteRoom={this.props.deleteRoom}/>
</Grid.Column> </Grid.Column>
<Grid.Column width={8}> <Grid.Column width={8}>
<ModalWindow type="modify" idRoom={this.state.activeItem === -1? -1 : this.state.activeRoom} updateRoom={this.props.updateRoom} deleteRoom={this.props.deleteRoom}/> <ModalWindow type="modify" idRoom={this.state.activeRoom} updateRoom={this.props.updateRoom} deleteRoom={this.props.deleteRoom}/>
</Grid.Column> </Grid.Column>
</Grid.Row> </Grid.Row>
</Grid> </Grid>