This commit is contained in:
Claudio Maggioni (maggicl) 2020-04-28 11:56:34 +02:00
parent 835ac99a8e
commit b0b281263e
2 changed files with 534 additions and 558 deletions

View file

@ -65,9 +65,7 @@ const CreateTrigger = (props) => {
const onChange = (e, val) => {
props.inputChange(val);
if (
props.devices
.filter((d) => d.id === val.value)[0]
.hasOwnProperty("on")
props.devices.filter((d) => d.id === val.value)[0].hasOwnProperty("on")
) {
setActiveOperand(false);
} else {
@ -94,9 +92,7 @@ const CreateTrigger = (props) => {
<React.Fragment>
<Form.Field inline width={2}>
<Dropdown
onChange={(e, val) =>
props.inputChange(val)
}
onChange={(e, val) => props.inputChange(val)}
name="operand"
compact
selection
@ -105,9 +101,7 @@ const CreateTrigger = (props) => {
</Form.Field>
<Form.Field inline width={7}>
<Input
onChange={(e, val) =>
props.inputChange(val)
}
onChange={(e, val) => props.inputChange(val)}
name="value"
type="number"
placeholder="Value"
@ -117,9 +111,7 @@ const CreateTrigger = (props) => {
) : (
<Form.Field inline width={7}>
<Dropdown
onChange={(e, val) =>
props.inputChange(val)
}
onChange={(e, val) => props.inputChange(val)}
placeholder="State"
name="value"
compact
@ -146,10 +138,7 @@ const SceneItem = (props) => {
<Checkbox
toggle
onChange={(e, val) =>
props.orderScenes(
props.scene.id,
val.checked
)
props.orderScenes(props.scene.id, val.checked)
}
checked={position + 1 > 0}
/>
@ -158,9 +147,7 @@ const SceneItem = (props) => {
<h3>{props.scene.name}</h3>
</Grid.Column>
<Grid.Column width={4}>
<h3>
{position !== -1 ? "# " + (position + 1) : ""}
</h3>
<h3>{position !== -1 ? "# " + (position + 1) : ""}</h3>
</Grid.Column>
</Grid.Row>
</Grid>
@ -207,9 +194,7 @@ export const CreateAutomation = (props) => {
}, [props]);
const _checkNewTrigger = (trigger) => {
const auxDevice = props.devices.filter(
(d) => d.id === trigger.device
)[0];
const auxDevice = props.devices.filter((d) => d.id === trigger.device)[0];
if (auxDevice && auxDevice.hasOwnProperty("on")) {
if (!trigger.device || !trigger.value == null) {
return {
@ -425,7 +410,6 @@ const Automation = ({automation, devices, scenes, removeAutomation}) => {
const getOperator = (operand) =>
operands.filter((o) => o.key == operand)[0].text;
return (
<React.Fragment>
<Header style={{ display: "inline", marginRight: "1rem" }}>
@ -462,9 +446,7 @@ const Automation = ({automation, devices, scenes, removeAutomation}) => {
<Menu.Item as="span">
{device.name}{" "}
{trigger.operator
? getOperator(
trigger.operator
) +
? getOperator(trigger.operator) +
" " +
trigger.range
: trigger.value
@ -486,9 +468,7 @@ const Automation = ({automation, devices, scenes, removeAutomation}) => {
)[0];
return (
<Menu key={sceneData.id} compact>
<Menu.Item as="span">
{sceneData.name}
</Menu.Item>
<Menu.Item as="span">{sceneData.name}</Menu.Item>
</Menu>
);
})}
@ -504,7 +484,7 @@ const Automation = ({automation, devices, scenes, removeAutomation}) => {
class AutomationsPanel extends Component {
constructor(props) {
super(props);
this.state = {openModal: false}
this.state = { openModal: false };
this.getDevices();
this.getScenes();
this.getAutomations();
@ -526,20 +506,18 @@ class AutomationsPanel extends Component {
.catch((err) => console.error(`error fetching automations:`, err));
}
removeAutomation = (id) => {
this.props
.deleteAutomation(id)
.catch((err) => console.error(`error removing automation ${id}:`, err));
}
};
render() {
return (
<Grid style={{ marginTop: "3rem" }}>
<Grid.Row>
<Grid.Column textAlign="center" width={16}>
{!this.state.openModal ?
(
{!this.state.openModal ? (
<List>
<CreateAutomation
save={this.props.saveAutomation}
@ -547,21 +525,16 @@ class AutomationsPanel extends Component {
devices={this.props.devices}
/>
</List>
) :
(
) : (
<Button color="green">CREATE AUTOMATION</Button>
)}
</Grid.Column>
</Grid.Row>
<Grid.Row>
{this.props.automations.map((automation, i) => {
console.log(23, automation, i, this.props.automations);
return (
<Grid.Column
key={i}
width={8}
style={{margin: "2rem 0"}}
>
<Grid.Column key={i} width={8} style={{ margin: "2rem 0" }}>
<Automation
removeAutomation={this.removeAutomation}
scenes={this.props.scenes}
@ -587,6 +560,7 @@ const mapStateToProps = (state, _) => ({
return Object.values(state.devices);
},
get automations() {
console.log(state.automations);
return Object.values(state.automations);
},
});

View file

@ -256,8 +256,13 @@ function reducer(previousState, action) {
case "AUTOMATION_UPDATE":
newState = previousState;
const automations = {};
for (const automation of action.automations) {
automations[automation.id] = automation;
}
change = {
automations : {$set: action.automations}
automations: { $set: automations },
};
newState = update(previousState, change);
break;
@ -297,15 +302,13 @@ function reducer(previousState, action) {
case "AUTOMATION_SAVE":
console.log("ID: ", action.automation.id);
change = {
automations : {[action.automation.id] : {$set : action.automation}}
automations: { [action.automation.id]: { $set: action.automation } },
};
newState = update(previousState, change);
break;
case "STATE_SAVE":
change = {
sceneStates: { [action.sceneState.id]: { $set: action.sceneState } },
@ -355,15 +358,14 @@ function reducer(previousState, action) {
break;
case "AUTOMATION_DELETE":
change = {
automations: { $unset: [action.id] },
};
console.log("CHANGE ", change)
console.log("CHANGE ", change);
console.log("Action id: ", action.id);
newState = update(previousState, change);
console.log("NEW STATE ", newState)
console.log("NEW STATE ", newState);
break;
case "SCENE_DELETE":
console.log("SCENE", action.sceneId);