diff --git a/smart-hut/src/components/AutomationModal.js b/smart-hut/src/components/AutomationModal.js index 626a6c5..0cf1674 100644 --- a/smart-hut/src/components/AutomationModal.js +++ b/smart-hut/src/components/AutomationModal.js @@ -84,6 +84,7 @@ class AutomationModal extends Component { render() { return (
+ {/* {!this.props.nicolaStop ? (
@@ -167,7 +168,7 @@ class AutomationModal extends Component { {this.type === "new" ? "Add automation" : "Save changes"} - + */}
); } diff --git a/smart-hut/src/components/dashboard/AutomationsPanel.js b/smart-hut/src/components/dashboard/AutomationsPanel.js index 4e8ca37..c730332 100644 --- a/smart-hut/src/components/dashboard/AutomationsPanel.js +++ b/smart-hut/src/components/dashboard/AutomationsPanel.js @@ -16,7 +16,7 @@ import { Divider, Checkbox, Menu, - Label + Label, } from "semantic-ui-react"; const operands = [ @@ -154,7 +154,7 @@ const SceneItem = (props) => { checked={position + 1 > 0} /> - +

{props.scene.name}

@@ -180,11 +180,13 @@ const Trigger = ({deviceName, trigger, onRemove, index}) => { {deviceName} {operand ? {symbol} : ""} - {value ? "on" : "off"} + + {operand ? value : value ? "on" : "off"} + onRemove(device, index)} + onClick={() => onRemove(index)} className="remove-icon" name="remove" /> @@ -192,7 +194,7 @@ const Trigger = ({deviceName, trigger, onRemove, index}) => { ); }; -const CreateAutomation = (props) => { +export const CreateAutomation = (props) => { const [triggerList, setTrigger] = useState([]); const [order, setOrder] = useState([]); const [stateScenes, setScenes] = useState(props.scenes); @@ -250,8 +252,8 @@ const CreateAutomation = (props) => { } }; - const removeTrigger = (trigger) => { - // TODO When this is connected to the backend each trigger will have an id which can be used to remove it + const removeTrigger = (index) => { + setTrigger((prevList) => prevList.filter((t, i) => i !== index)); }; // This gets triggered when the devices dropdown changes the value. @@ -271,7 +273,6 @@ const CreateAutomation = (props) => { if (value.length > 0) { setScenes((prevScenes) => { return stateScenes.filter((e) => { - console.log(e.name.includes(value)); return e.name.includes(value); }); }); @@ -301,17 +302,16 @@ const CreateAutomation = (props) => { return false; } return true; - } + }; const saveAutomation = () => { - console.log("trigger list: ", triggerList); //if(checkBeforeSave()){ const automation = { - name: automationName - } + name: automationName, + }; props.save({automation, triggerList, order}); //} - } + }; return ( @@ -409,10 +409,9 @@ const CreateAutomation = (props) => { - - - - + @@ -420,9 +419,12 @@ const CreateAutomation = (props) => { ); }; -const Automation = ({automation, devices}) => { - const {triggers, scenes} = automation; - const getOperator = (operand) => operands.filter(o => o.key == operand)[0].text; +const Automation = ({automation, devices, scenes, removeAutomation}) => { + const {triggers} = automation; + const scenePriorities = automation.scenes; + const getOperator = (operand) => + operands.filter((o) => o.key == operand)[0].text; + return ( @@ -435,6 +437,14 @@ const Automation = ({automation, devices}) => { size="small" icon={"edit"} /> + + )} + - {this.props.automations.map((automation) => { + {this.props.automations.map((automation, i) => { return ( - - + + ); })} @@ -527,9 +587,8 @@ const mapStateToProps = (state, _) => ({ return Object.values(state.devices); }, get automations() { - console.log(Object.values(state.automations)); return Object.values(state.automations); - } + }, }); const AutomationsPanelContainer = connect( mapStateToProps, diff --git a/smart-hut/src/remote.js b/smart-hut/src/remote.js index 6891b88..a3c6b8c 100644 --- a/smart-hut/src/remote.js +++ b/smart-hut/src/remote.js @@ -348,7 +348,6 @@ export const RemoteService = { if ((index + 1) === length) { return void dispatch(actions.automationsUpdate(automations)); } - console.log("automation after trigger: ", automation); }) }) @@ -479,6 +478,7 @@ export const RemoteService = { */ saveAutomation: (data) => { const {automation, triggerList, order} = data; + console.log("automation: ", automation, triggerList, order); automation.triggers = []; automation.scenes = []; return (dispatch) => { @@ -525,8 +525,9 @@ export const RemoteService = { let resScenes = await Endpoint["post"](urlScenePriority, {}, scenePriority) automation.scenes.push(resScenes.data); } - console.log("This is an automtion: ", automation); + automation.id = id; dispatch(actions.automationSave(automation)); + }); } @@ -721,6 +722,19 @@ export const RemoteService = { }; }, + deleteAutomation: (id) => { + console.log("ID OF AUTO ", id); + return (dispatch) => { + return Endpoint.delete(`/automation/${id}`) + .then((_) => dispatch(actions.automationDelete(id))) + .catch((err) => { + console.warn("Automation deletion error", err); + throw new RemoteError(["Network error"]); + }); + }; + + }, + /** * Deletes a scene * @param {Number} sceneId the id of the scene to delete diff --git a/smart-hut/src/store.js b/smart-hut/src/store.js index 2e0e20d..7c0507f 100644 --- a/smart-hut/src/store.js +++ b/smart-hut/src/store.js @@ -295,15 +295,18 @@ function reducer(previousState, action) { break; case "AUTOMATION_SAVE": + console.log("ID: ", action.automation.id); change = { automations : {[action.automation.id] : {$set : action.automation}} }; + + newState = update(previousState, change); + break; case "STATE_SAVE": - console.log("Store", action.sceneState); change = { sceneStates: { [action.sceneState.id]: { $set: action.sceneState } }, }; @@ -350,6 +353,18 @@ function reducer(previousState, action) { newState = update(previousState, change); break; + + case "AUTOMATION_DELETE": + + change = { + automations: { $unset: [action.id] }, + }; + + console.log("CHANGE ", change) + console.log("Action id: ", action.id); + newState = update(previousState, change); + console.log("NEW STATE ", newState) + break; case "SCENE_DELETE": console.log("SCENE", action.sceneId); if (!(action.sceneId in previousState.scenes)) { diff --git a/smart-hut/src/storeActions.js b/smart-hut/src/storeActions.js index e02bbd8..d1ec599 100644 --- a/smart-hut/src/storeActions.js +++ b/smart-hut/src/storeActions.js @@ -71,6 +71,10 @@ const actions = { type: "ROOM_DELETE", roomId, }), + automationDelete: (id) => ({ + type: "AUTOMATION_DELETE", + id, + }), sceneDelete: (sceneId) => ({ type: "SCENE_DELETE", sceneId,