From 246fdb1de8671395b6081fe380d4d0c00636dcb7 Mon Sep 17 00:00:00 2001 From: britea Date: Tue, 5 May 2020 15:53:07 +0200 Subject: [PATCH 1/2] points 2,3,4 --- smart-hut/src/components/HeaderController.js | 25 ++++++++++++++++++- smart-hut/src/components/SceneModal.js | 22 ++++++++++++++++ .../components/dashboard/devices/Videocam.js | 2 +- smart-hut/src/remote.js | 15 +++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/smart-hut/src/components/HeaderController.js b/smart-hut/src/components/HeaderController.js index 1d84c6b..6c797ab 100644 --- a/smart-hut/src/components/HeaderController.js +++ b/smart-hut/src/components/HeaderController.js @@ -1,5 +1,12 @@ import React from "react"; -import { Grid, Divider, Button, Label, Responsive } from "semantic-ui-react"; +import { + Grid, + Divider, + Button, + Label, + Responsive, + Checkbox, +} from "semantic-ui-react"; import { Segment, Image } from "semantic-ui-react"; import { RemoteService } from "../remote"; import { withRouter } from "react-router-dom"; @@ -35,6 +42,16 @@ export class MyHeader extends React.Component { .catch((err) => console.error("MyHeader fetch user info error", err)); } + setCameraEnabled(val) { + let enabled = { + cameraEnabled: val, + }; + this.props + .userPermissions(enabled) + .then(() => this.getInfo()) + .catch((err) => console.error("Camera enabled", err)); + } + render() { return (
@@ -60,6 +77,11 @@ export class MyHeader extends React.Component { + this.setCameraEnabled(val.checked)} + /> @@ -94,6 +116,7 @@ export class MyHeader extends React.Component { const mapStateToProps = (state, _) => ({ username: state.userInfo && state.userInfo.username ? state.userInfo.username : "", + cameraEnabled: state.userInfo ? state.userInfo.cameraEnabled : false, }); const LoginContainer = connect( mapStateToProps, diff --git a/smart-hut/src/components/SceneModal.js b/smart-hut/src/components/SceneModal.js index be03283..513c2e6 100644 --- a/smart-hut/src/components/SceneModal.js +++ b/smart-hut/src/components/SceneModal.js @@ -8,6 +8,7 @@ import { Form, Input, Dropdown, + Checkbox, } from "semantic-ui-react"; import SelectIcons from "./SelectIcons"; import { connect } from "react-redux"; @@ -23,6 +24,7 @@ class SceneModal extends Component { this.modifySceneModal = this.modifySceneModal.bind(this); this.deleteScene = this.deleteScene.bind(this); this.updateIcon = this.updateIcon.bind(this); + this.setGuestAccessEnabled = this.setGuestAccessEnabled.bind(this); this.setCopyFrom = this.setCopyFrom.bind(this); } @@ -41,6 +43,8 @@ class SceneModal extends Component { selectedIcon: "home", scenes: this.scenes, copyFrom: null, + guestAccessEnabled: + this.type === "new" ? null : this.props.scene.guestAccessEnabled, }; } @@ -79,7 +83,9 @@ class SceneModal extends Component { let data = { name: this.state.name, icon: this.state.selectedIcon, + guestAccessEnabled: this.state.guestAccessEnabled, }; + console.log(data); this.props .saveScene(data, this.props.id) @@ -119,6 +125,11 @@ class SceneModal extends Component { this.setState({ ...this.state, copyFrom: copyFrom.value }); } + setGuestAccessEnabled(val) { + console.log(this.state, val); + this.setState({ guestAccessEnabled: val }); + } + render() { const spaceDiv = { background: "#f4f4f4", @@ -210,6 +221,17 @@ class SceneModal extends Component { /> )} + {this.type === "modify" ? ( + + + this.setGuestAccessEnabled(val.checked) + } + /> + + ) : null} {this.type === "modify" ? ( diff --git a/smart-hut/src/components/dashboard/devices/Videocam.js b/smart-hut/src/components/dashboard/devices/Videocam.js index de77219..cdde004 100644 --- a/smart-hut/src/components/dashboard/devices/Videocam.js +++ b/smart-hut/src/components/dashboard/devices/Videocam.js @@ -31,7 +31,7 @@ class Videocam extends Component { setOnOff(onOff) { const turn = onOff; - if (this.props.tab === "Devices") { + if (this.props.tab === "Devices" || this.props.tab === "Hosts") { this.props .saveDevice({ ...this.props.device, on: turn }) .then((res) => diff --git a/smart-hut/src/remote.js b/smart-hut/src/remote.js index 666910b..4eec55d 100644 --- a/smart-hut/src/remote.js +++ b/smart-hut/src/remote.js @@ -242,6 +242,20 @@ export const RemoteService = { }); }, + /** + * Fetches user information via REST calls, if it is logged in + * @returns {Promise} promise that resolves to void and rejects + * with user-fiendly errors as a RemoteError + */ + userPermissions: (data) => { + return (dispatch) => { + return Endpoint.put("/user/permissions", {}, data).catch((err) => { + console.warn("Fetch user info error", err); + throw new RemoteError(["Network error"]); + }); + }; + }, + /** * Fetches user information via REST calls, if it is logged in * @returns {Promise} promise that resolves to void and rejects @@ -458,6 +472,7 @@ export const RemoteService = { data = { name: data.name, icon: data.icon, + guestAccessEnabled: sceneId ? data.guestAccessEnabled : false, }; return (sceneId From bb16d5c47538e682130f4fa0c5472428ad988741 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Tue, 5 May 2020 17:04:53 +0200 Subject: [PATCH 2/2] Done 1 read scene part --- smart-hut/src/components/HostModal.js | 50 ++++++------- smart-hut/src/components/RoomModal.js | 3 +- .../src/components/dashboard/HostsPanel.js | 71 +++++++++++++------ .../dashboard/devices/DeviceSettingsModal.js | 10 +-- smart-hut/src/remote.js | 13 +++- smart-hut/src/store.js | 10 +++ smart-hut/src/storeActions.js | 5 ++ 7 files changed, 100 insertions(+), 62 deletions(-) diff --git a/smart-hut/src/components/HostModal.js b/smart-hut/src/components/HostModal.js index e262e3f..cd03809 100644 --- a/smart-hut/src/components/HostModal.js +++ b/smart-hut/src/components/HostModal.js @@ -22,8 +22,7 @@ class HostModal extends Component { .then(() => { this.setState({ ...this.state, - guests: this.props.guests - .map((u) => u.id), + guests: this.props.guests.map((u) => u.id), }); }) .catch(console.error); @@ -32,11 +31,13 @@ class HostModal extends Component { .then((users) => this.setState({ ...this.state, - users: users.filter(u => u.id !== this.props.currentUserId).map((u) => ({ - key: u.id, - text: `@${u.username} (${u.name})`, - value: u.id, - })), + users: users + .filter((u) => u.id !== this.props.currentUserId) + .map((u) => ({ + key: u.id, + text: `@${u.username} (${u.name})`, + value: u.id, + })), }) ) .catch(console.error); @@ -86,25 +87,20 @@ class HostModal extends Component {
Select guests
- -

Spaghetti!

-
- -
- - - - -
-
+
+ + + + +
+
+ + + ))} + +
+ Devices +
+ + {this.props.hostDeviceIds.map((id) => { + return ( + + ); + })} + + ); } } @@ -44,6 +72,7 @@ class HostsPanel extends Component { const mapStateToProps = (state, _) => ({ isActiveDefaultHost: state.active.activeHost === -1, activeHost: state.active.activeHost, + hostScenes: state.hostScenes[state.active.activeHost] || [], hostDevices: state.hostDevices, hostDeviceIds: Object.keys(state.hostDevices[state.active.activeHost] || {}), }); diff --git a/smart-hut/src/components/dashboard/devices/DeviceSettingsModal.js b/smart-hut/src/components/dashboard/devices/DeviceSettingsModal.js index 83773c8..c6b9408 100644 --- a/smart-hut/src/components/dashboard/devices/DeviceSettingsModal.js +++ b/smart-hut/src/components/dashboard/devices/DeviceSettingsModal.js @@ -1,13 +1,5 @@ import React, { Component, useState } from "react"; -import { - Button, - Form, - Icon, - Header, - Modal, - Input, - Checkbox, -} from "semantic-ui-react"; +import { Button, Form, Icon, Header, Modal, Input } from "semantic-ui-react"; import { connect } from "react-redux"; import { RemoteService } from "../../../remote"; diff --git a/smart-hut/src/remote.js b/smart-hut/src/remote.js index 4eec55d..f0cfcea 100644 --- a/smart-hut/src/remote.js +++ b/smart-hut/src/remote.js @@ -304,10 +304,17 @@ export const RemoteService = { * @returns {Promise} promise that resolves to void and rejects * with user-fiendly errors as a RemoteError */ - fetchAllScenes: () => { + fetchAllScenes: (hostId = null) => { return (dispatch) => { - return Endpoint.get("/scene") - .then((res) => void dispatch(actions.scenesUpdate(res.data))) + return Endpoint.get("/scene", hostId ? { hostId } : {}) + .then( + (res) => + void dispatch( + !hostId + ? actions.scenesUpdate(res.data) + : actions.hostScenesUpdate(hostId, res.data) + ) + ) .catch((err) => { console.error("Fetch all scenes error", err); throw new RemoteError(["Network error"]); diff --git a/smart-hut/src/store.js b/smart-hut/src/store.js index 1ade73b..f256839 100644 --- a/smart-hut/src/store.js +++ b/smart-hut/src/store.js @@ -120,6 +120,15 @@ function reducer(previousState, action) { createOrUpdateScene(scene); } break; + case "HOST_SCENES_UPDATE": + change = { + hostScenes: { + [action.hostId]: { $set: action.scenes }, // stored as array + }, + }; + + newState = update(previousState, change); + break; case "STATES_UPDATE": //console.log(action.sceneStates); change = null; @@ -576,6 +585,7 @@ const initState = { rooms: {}, /** @type {[integer]Scene} */ scenes: {}, + hostScenes: {}, /** @type {[integer]Automation} */ automations: {}, /** @type {[integer]Device} */ diff --git a/smart-hut/src/storeActions.js b/smart-hut/src/storeActions.js index 64c2a52..218f4b9 100644 --- a/smart-hut/src/storeActions.js +++ b/smart-hut/src/storeActions.js @@ -102,6 +102,11 @@ const actions = { type: "SCENES_UPDATE", scenes, }), + hostScenesUpdate: (hostId, scenes) => ({ + type: "HOST_SCENES_UPDATE", + hostId, + scenes, + }), deviceDelete: (deviceId) => ({ type: "DEVICE_DELETE", deviceId,