fixed major bug

This commit is contained in:
britea 2020-04-16 17:07:56 +02:00
parent af209221f0
commit f99a5d49ff
3 changed files with 95 additions and 58 deletions

View file

@ -7,7 +7,6 @@ import {
Responsive, Responsive,
Form, Form,
Input, Input,
Dropdown,
} from "semantic-ui-react"; } from "semantic-ui-react";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { RemoteService } from "../remote"; import { RemoteService } from "../remote";
@ -19,7 +18,6 @@ class SceneModal extends Component {
super(props); super(props);
this.state = this.initialState; this.state = this.initialState;
this.setInitialState(); this.setInitialState();
this.getDevices();
this.addSceneModal = this.addSceneModal.bind(this); this.addSceneModal = this.addSceneModal.bind(this);
this.modifySceneModal = this.modifySceneModal.bind(this); this.modifySceneModal = this.modifySceneModal.bind(this);
@ -29,7 +27,6 @@ class SceneModal extends Component {
get initialState() { get initialState() {
return { return {
name: this.type === "new" ? "New Scene" : this.props.room.name, name: this.type === "new" ? "New Scene" : this.props.room.name,
sceneDevices: this.type === "new" ? [{}] : [this.props.scene.devices],
openModal: false, openModal: false,
}; };
} }
@ -42,12 +39,6 @@ class SceneModal extends Component {
return !this.props.id ? "new" : "modify"; return !this.props.id ? "new" : "modify";
} }
getDevices() {
this.props
.fetchDevices()
.catch((err) => console.error(`error fetching devices:`, err));
}
addSceneModal = (e) => { addSceneModal = (e) => {
/*let data = { /*let data = {
// DATA HERE // DATA HERE
@ -99,21 +90,7 @@ class SceneModal extends Component {
this.setState({ openModal: true }); this.setState({ openModal: true });
}; };
setSceneDevice(e, d) {
this.setState({ sceneDevices: d.value });
}
render() { render() {
const availableDevices = [];
this.props.devices.forEach((e) => {
if (!this.state.sceneDevices.includes(e)) {
availableDevices.push({
key: e.id,
text: e.name,
id: e.id,
});
}
});
return ( return (
<div> <div>
{!this.props.nicolaStop ? ( {!this.props.nicolaStop ? (
@ -176,17 +153,6 @@ class SceneModal extends Component {
value={this.state.name} value={this.state.name}
/> />
</Form.Field> </Form.Field>
<Form.Field>
<label>Select devices you want to attach: </label>
<Dropdown
name="scene devices"
placeholder="Select Devices"
fluid
multiple
onChange={this.setSceneDevice}
options={availableDevices}
/>
</Form.Field>
</Form> </Form>
{this.type === "modify" ? ( {this.type === "modify" ? (
@ -229,16 +195,6 @@ const setActiveScene = (activeScene) => {
}; };
const mapStateToProps = (state, ownProps) => ({ const mapStateToProps = (state, ownProps) => ({
get devices() {
if (state.active.activeRoom === -1) {
return Object.values(state.devices);
} else {
const deviceArray = [
...state.rooms[state.active.activeRoom].devices,
].sort();
return deviceArray.map((id) => state.devices[id]);
}
},
scene: ownProps.id ? state.scenes[ownProps.id] : null, scene: ownProps.id ? state.scenes[ownProps.id] : null,
}); });
const SceneModalContainer = connect( const SceneModalContainer = connect(

View file

@ -1,5 +1,5 @@
import React, { Component } from "react"; import React, { Component } from "react";
import { Button, Modal, Icon, Image } from "semantic-ui-react"; import { Button, Modal, Icon, Image, Form, Dropdown } from "semantic-ui-react";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { RemoteService } from "../../remote"; import { RemoteService } from "../../remote";
import styled from "styled-components"; import styled from "styled-components";
@ -30,7 +30,18 @@ class NewSceneDevice extends Component {
this.state = { this.state = {
openModal: false, openModal: false,
sceneDevices: this.props.scene ? this.props.scene.devices : {},
deviceName: "",
}; };
this.getDevices();
this.setSceneDevice = this.setSceneDevice.bind(this);
}
getDevices() {
this.props
.fetchDevices()
.catch((err) => console.error(`error fetching devices:`, err));
} }
handleOpen = () => { handleOpen = () => {
@ -45,7 +56,21 @@ class NewSceneDevice extends Component {
this.handleClose(); this.handleClose();
}; };
setSceneDevice(e, d) {
this.setState({ devicesAttached: d.value });
}
render() { render() {
const availableDevices = [];
this.props.devices.forEach((e) => {
if (!Object.keys(this.state.sceneDevices).find((d) => e.id === d)) {
availableDevices.push({
key: e.id,
text: e.name,
value: e.id,
});
}
});
return ( return (
<Modal <Modal
closeIcon closeIcon
@ -59,7 +84,21 @@ class NewSceneDevice extends Component {
centered={true} centered={true}
> >
<Modal.Header>Add a New Scene Device</Modal.Header> <Modal.Header>Add a New Scene Device</Modal.Header>
<Modal.Content></Modal.Content> <Modal.Content>
<Form>
<Form.Field style={{ marginTop: "1rem" }}>
<label>Select devices you want to attach: </label>
<Dropdown
name="scene devices"
placeholder="Select Devices"
fluid
multiple
onChange={this.setSceneDevice}
options={availableDevices}
/>
</Form.Field>
</Form>
</Modal.Content>
<Modal.Actions> <Modal.Actions>
<Button <Button
onClick={this.createDevice} onClick={this.createDevice}
@ -78,7 +117,7 @@ class NewSceneDevice extends Component {
const mapStateToProps = (state, _) => ({ const mapStateToProps = (state, _) => ({
devices: Object.values(state.devices), devices: Object.values(state.devices),
activeRoom: state.active.activeRoom, activeScene: state.active.activeScene,
}); });
const NewSceneDeviceContainer = connect( const NewSceneDeviceContainer = connect(
mapStateToProps, mapStateToProps,

View file

@ -39,8 +39,8 @@ const Endpoint = {
* @param {[String]String} query query ('?') parameters (no params by default) * @param {[String]String} query query ('?') parameters (no params by default)
* @param {any} body the JSON request body * @param {any} body the JSON request body
*/ */
send: (method, route, query = {}, body = null, registration) => { send: (method, route, query = {}, body = null) => {
if (!registration && !Endpoint.token) { if (!Endpoint.token) {
throw new Error("No token while performing authenticated request"); throw new Error("No token while performing authenticated request");
} }
@ -48,11 +48,9 @@ const Endpoint = {
method: method, method: method,
params: query, params: query,
data: ["put", "post"].indexOf(method) !== -1 ? body : null, data: ["put", "post"].indexOf(method) !== -1 ? body : null,
headers: !registration headers: {
? { Authorization: `Bearer ${Endpoint.token}`,
Authorization: `Bearer ${Endpoint.token}`, },
}
: {},
}).then((res) => { }).then((res) => {
if (!res.data && method !== "delete") { if (!res.data && method !== "delete") {
console.error("Response body is empty"); console.error("Response body is empty");
@ -63,6 +61,28 @@ const Endpoint = {
}); });
}, },
/**
* Performs a non-authenticated post and put request for registration, reset-password
* @param {post} the desired method
* @param {String} route the desired route (e.g. "/rooms/1/devices")
* @param {[String]String} query query ('?') parameters (no params by default)
* @param {any} body the JSON request body
*/
sendNA: (method, route, query = {}, body = null) => {
return Endpoint.axiosInstance(route, {
method: method,
params: query,
data: ["put", "post"].indexOf(method) !== -1 ? body : null,
}).then((res) => {
if (!res.data) {
console.error("Response body is empty");
return null;
} else {
return res;
}
});
},
/** /**
* Performs login * Performs login
* @param {String} usernameOrEmail * @param {String} usernameOrEmail
@ -111,7 +131,18 @@ const Endpoint = {
* @returns {Promise<*, *>} The Axios-generated promise * @returns {Promise<*, *>} The Axios-generated promise
*/ */
post(route, query, body) { post(route, query, body) {
return this.send("post", route, query, body, true); return this.send("post", route, query, body);
},
/**
* Performs a non-authenticated POST request
* @param {String} route the desired route (e.g. "/rooms/1/devices")
* @param {[String]String} query query ('?') parameters (no params by default)
* @param {any} body the JSON request body
* @returns {Promise<*, *>} The Axios-generated promise
*/
postNA(route, query, body) {
return this.sendNA("post", route, query, body);
}, },
/** /**
@ -125,6 +156,17 @@ const Endpoint = {
return this.send("put", route, query, body); return this.send("put", route, query, body);
}, },
/**
* Performs a non-authenticated PUT request
* @param {String} route the desired route (e.g. "/rooms/1/devices")
* @param {[String]String} query query ('?') parameters (no params by default)
* @param {any} body the JSON request body
* @returns {Promise<*, *>} The Axios-generated promise
*/
putNA(route, query = {}, body = {}) {
return this.sendNA("put", route, query, body);
},
/** /**
* Performs an authenticated DELETE request * Performs an authenticated DELETE request
* @param {get|post|put|delete} the desired method * @param {get|post|put|delete} the desired method
@ -489,7 +531,7 @@ export class Forms {
* with validation errors as a String array * with validation errors as a String array
*/ */
static submitRegistration(data) { static submitRegistration(data) {
return Endpoint.post( return Endpoint.postNA(
"/register", "/register",
{}, {},
{ {
@ -512,7 +554,7 @@ export class Forms {
* with validation errors as a String array * with validation errors as a String array
*/ */
static submitInitResetPassword(email) { static submitInitResetPassword(email) {
return Endpoint.post( return Endpoint.postNA(
"/register/init-reset-password", "/register/init-reset-password",
{}, {},
{ {
@ -537,7 +579,7 @@ export class Forms {
* with validation errors as a String array * with validation errors as a String array
*/ */
static submitResetPassword(confirmationToken, password) { static submitResetPassword(confirmationToken, password) {
return Endpoint.post( return Endpoint.putNA(
"/register/reset-password", "/register/reset-password",
{}, {},
{ {