diff --git a/smart-hut/src/components/dashboard/DevicePanel.js b/smart-hut/src/components/dashboard/DevicePanel.js index 3e92824..ae4451c 100644 --- a/smart-hut/src/components/dashboard/DevicePanel.js +++ b/smart-hut/src/components/dashboard/DevicePanel.js @@ -49,9 +49,9 @@ const mapStateToProps = (state, _) => ({ if (state.active.activeRoom === -1) { return Object.values(state.devices); } else { - return state.rooms[state.active.activeRoom].devices.map( - (id) => state.devices[id] - ); + const deviceArray = [...state.rooms[state.active.activeRoom].devices]; + console.log(deviceArray); + return deviceArray.map((id) => state.devices[id]); } }, get isActiveRoomHome() { diff --git a/smart-hut/src/store.js b/smart-hut/src/store.js index d649dfe..f988a63 100644 --- a/smart-hut/src/store.js +++ b/smart-hut/src/store.js @@ -21,6 +21,19 @@ function reducer(previousState, action) { }, }); } + + if (newState.pendingJoins.rooms[room.id]) { + newState = update(newState, { + pendingJoins: { rooms: { $unset: [room.id] } }, + rooms: { + [room.id]: { + devices: { + $add: [...newState.pendingJoins.rooms[room.id]], + }, + }, + }, + }); + } }; let change; @@ -87,6 +100,7 @@ function reducer(previousState, action) { change = { devices: {}, rooms: {}, + pendingJoins: { rooms: {} }, }; for (const device of action.devices) { change.devices[device.id] = { $set: device }; @@ -96,12 +110,16 @@ function reducer(previousState, action) { devices.$add = devices.$add || []; devices.$add.push(device.id); } else { - console.warn( - "Cannot join device", - device, - `in room ${device.roomId} since that - room has not been fetched` - ); + // room does not exist yet, so add to the list of pending + // joins + + if (!change.pendingJoins.rooms[device.roomId]) { + change.pendingJoins.rooms[device.roomId] = { + $set: new Set([device.id]), + }; + } else { + change.pendingJoins.rooms[device.roomId].$set.add(device.id); + } } } newState = update(newState, change); @@ -175,6 +193,9 @@ function reducer(previousState, action) { const initState = { errors: {}, + pendingJoins: { + rooms: {}, + }, active: { activeRoom: -1, }, diff --git a/smart-hut/src/views/Navbar.js b/smart-hut/src/views/Navbar.js index 9246cb2..1b4f9ca 100644 --- a/smart-hut/src/views/Navbar.js +++ b/smart-hut/src/views/Navbar.js @@ -185,8 +185,8 @@ class Navbar extends Component { } } -const setActiveRoom = (dispatch) => { - return (activeRoom) => dispatch(appActions.setActiveRoom(activeRoom)); +const setActiveRoom = (activeRoom) => { + return (dispatch) => dispatch(appActions.setActiveRoom(activeRoom)); }; const mapStateToProps = (state, _) => ({ rooms: state.rooms });