-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user presence action in scene (#999)
* backend: user presence * Allow the house key in action model * First frontend version of the user presence action box in scene * Add english translation * Add more tests * Add message in UI in dashboard box about setting user presence
- Loading branch information
1 parent
6f92880
commit da901e8
Showing
15 changed files
with
337 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
front/src/routes/scene/edit-scene/actions/UserPresence.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import Select from 'react-select'; | ||
import { Component } from 'preact'; | ||
import { connect } from 'unistore/preact'; | ||
import { Text } from 'preact-i18n'; | ||
|
||
import { ACTIONS } from '../../../../../../server/utils/constants'; | ||
|
||
@connect('httpClient', {}) | ||
class UserSeenAtHome extends Component { | ||
getOptions = async () => { | ||
try { | ||
const [users, houses] = await Promise.all([ | ||
this.props.httpClient.get('/api/v1/user'), | ||
this.props.httpClient.get('/api/v1/house') | ||
]); | ||
const userOptions = []; | ||
users.forEach(user => { | ||
userOptions.push({ | ||
label: user.firstname, | ||
value: user.selector | ||
}); | ||
}); | ||
const houseOptions = []; | ||
houses.forEach(house => { | ||
houseOptions.push({ | ||
label: house.name, | ||
value: house.selector | ||
}); | ||
}); | ||
await this.setState({ userOptions, houseOptions }); | ||
this.refreshSelectedOptions(this.props); | ||
return userOptions; | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}; | ||
handleChange = selectedOption => { | ||
if (selectedOption && selectedOption.value) { | ||
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'user', selectedOption.value); | ||
} else { | ||
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'user', null); | ||
} | ||
}; | ||
handleHouseChange = selectedOption => { | ||
if (selectedOption && selectedOption.value) { | ||
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'house', selectedOption.value); | ||
} else { | ||
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'house', null); | ||
} | ||
}; | ||
refreshSelectedOptions = nextProps => { | ||
let selectedOption = ''; | ||
let selectedHouseOption = ''; | ||
if (nextProps.action.user && this.state.userOptions) { | ||
const userOption = this.state.userOptions.find(option => option.value === nextProps.action.user); | ||
|
||
if (userOption) { | ||
selectedOption = userOption; | ||
} | ||
} | ||
if (nextProps.action.house && this.state.houseOptions) { | ||
const houseOption = this.state.houseOptions.find(option => option.value === nextProps.action.house); | ||
|
||
if (houseOption) { | ||
selectedHouseOption = houseOption; | ||
} | ||
} | ||
this.setState({ selectedOption, selectedHouseOption }); | ||
}; | ||
constructor(props) { | ||
super(props); | ||
this.props = props; | ||
this.state = { | ||
selectedOption: '', | ||
selectedHouseOption: '' | ||
}; | ||
} | ||
componentDidMount() { | ||
this.getOptions(); | ||
} | ||
componentWillReceiveProps(nextProps) { | ||
this.refreshSelectedOptions(nextProps); | ||
} | ||
render(props, { selectedOption, userOptions, houseOptions, selectedHouseOption }) { | ||
return ( | ||
<div> | ||
<p> | ||
{props.action.type === ACTIONS.USER.SET_SEEN_AT_HOME && ( | ||
<Text id="editScene.actionsCard.userPresence.userSeenDescription" /> | ||
)} | ||
{props.action.type === ACTIONS.USER.SET_OUT_OF_HOME && ( | ||
<Text id="editScene.actionsCard.userPresence.userLeftHomeDescription" /> | ||
)} | ||
</p> | ||
<div class="form-group"> | ||
<label class="form-label"> | ||
<Text id="editScene.actionsCard.userPresence.userLabel" /> | ||
<span class="form-required"> | ||
<Text id="global.requiredField" /> | ||
</span> | ||
</label> | ||
<Select options={userOptions} value={selectedOption} onChange={this.handleChange} /> | ||
</div> | ||
<div class="form-group"> | ||
<label class="form-label"> | ||
<Text id="editScene.actionsCard.userPresence.houseLabel" /> | ||
<span class="form-required"> | ||
<Text id="global.requiredField" /> | ||
</span> | ||
</label> | ||
<Select options={houseOptions} value={selectedHouseOption} onChange={this.handleHouseChange} /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default UserSeenAtHome; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const db = require('../../models'); | ||
const { NotFoundError } = require('../../utils/coreErrors'); | ||
const logger = require('../../utils/logger'); | ||
const { EVENTS, WEBSOCKET_MESSAGE_TYPES } = require('../../utils/constants'); | ||
|
||
/** | ||
* @description User left the house. | ||
* @param {string} houseSelector - The selector of the house. | ||
* @param {string} userSelector - The selector of the user. | ||
* @example | ||
* gladys.house.userLeft('main-house', 'john'); | ||
*/ | ||
async function userLeft(houseSelector, userSelector) { | ||
const house = await db.House.findOne({ | ||
where: { | ||
selector: houseSelector, | ||
}, | ||
}); | ||
|
||
if (house === null) { | ||
throw new NotFoundError('House not found'); | ||
} | ||
|
||
const user = await db.User.findOne({ | ||
attributes: ['id', 'firstname', 'lastname', 'selector', 'email', 'current_house_id', 'last_house_changed'], | ||
where: { | ||
selector: userSelector, | ||
}, | ||
}); | ||
|
||
if (user === null) { | ||
throw new NotFoundError('User not found'); | ||
} | ||
|
||
let userFinal = user; | ||
|
||
// user was in the house before | ||
if (userFinal.get({ plain: true }).current_house_id === house.id) { | ||
logger.debug(`User ${userSelector} left home ${houseSelector}`); | ||
userFinal = await user.update({ current_house_id: null, last_house_changed: new Date() }); | ||
// so we emit left home event | ||
this.event.emit(EVENTS.USER_PRESENCE.LEFT_HOME, userFinal.get({ plain: true })); | ||
// and we emit websocket event so that the change is sent to UI | ||
this.event.emit(EVENTS.WEBSOCKET.SEND_ALL, { | ||
type: WEBSOCKET_MESSAGE_TYPES.USER_PRESENCE.LEFT_HOME, | ||
payload: userFinal.get({ plain: true }), | ||
}); | ||
} | ||
|
||
return userFinal.get({ plain: true }); | ||
} | ||
|
||
module.exports = { | ||
userLeft, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.