Skip to content

Commit

Permalink
Fix population recursion error
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwohlbruck committed May 17, 2022
1 parent 512bc67 commit 439f573
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 25 deletions.
6 changes: 0 additions & 6 deletions client/src/services/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@ export const getGroup = async (id: string) => {
export const getMyGroups = async () => {
const { data } = await axios.get<Group[]>('/groups/me')

console.log(JSON.stringify(data))
console.log(data)

data.forEach(group => {
console.log(group)

// Destructure lamps
group.lamps?.forEach(lamp => {
console.log(lamp)
Store.commit('ADD_LAMP', lamp)
})
delete group.lamps
Expand Down
7 changes: 6 additions & 1 deletion client/src/store/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ const mutations = {
const getters = {
group: (state: GroupsState, getters: any, _rootState: RootState, rootGetters: any) => (id: string, populateGroups = false) => {
const g = state.byId[id]
console.log(populateGroups, g)
if (populateGroups) {
g.lamps = rootGetters.lampsByGroup(id)
}
return g
},
groups: (state: GroupsState, getters: any) => (ids: string[], populateGroups: boolean) => ids.map(id => getters.group(id, populateGroups)),

groups: (state: GroupsState, getters: any) => (ids: string[], populateGroups: boolean) => {
return ids.map(id => getters.group(id, populateGroups))
},

myGroups: (state: GroupsState, getters: any, _rootState: RootState, _rootGetters: any) => {
return getters
.groups(state.all, true)
Expand Down
14 changes: 5 additions & 9 deletions client/src/store/lamps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ const lampsGetters = {
const lamp = {...state.byId[id]}

// Denormalize data
if (lamp.groupId) {
lamp.group = getters.group(lamp.groupId)
delete lamp.groupId
}

if (lamp.userId) {
lamp.user = getters.user(lamp.userId)
delete lamp.userId
Expand All @@ -36,6 +31,7 @@ const lampsGetters = {
if (lamp?.user?._id) {
lamp.user = getters.user(lamp.user._id)
}
console.log(lamp)

return lamp
},
Expand Down Expand Up @@ -73,10 +69,10 @@ const mutations = {
},

ADD_LAMP_CONFIG(state: LampsState, config: any) {
Vue.set(state.byId, config.lampId, {
...state.byId[config.lampId],
config,
})
// Vue.set(state.byId, config.lampId, {
// ...state.byId[config.lampId],
// config,
// })
},

REMOVE_LAMP(state: LampsState, lamp: Lamp | { _id: string }) {
Expand Down
6 changes: 0 additions & 6 deletions client/src/views/Lamps.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ export default class Lamps extends Vue {
await getMyGroups()
}
// When navigating reload data
beforeRouteUpdate(_to: any, _from: any, next: any) {
getMyGroups()
next()
}
get myGroups() {
return this.$store.getters.myGroups
}
Expand Down
7 changes: 4 additions & 3 deletions server/routes/lamps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ router.post('/', isAuthenticated, async (req, res) => {
})

router.get('/:id/config', isAuthenticated, async (req, res) => {
const config = await getLampConfig(req.params.id)
// const config = {"deviceId":"4091519b56b8","readingLightColorTemperature":2001,"nightMode":false,"minimumLightLevel":0.8,"wifi":[{"ssid":"asu-visitor","password":""}],"brightness":0.5,"lampId":"626df507d6756882f166942c"}
// const config = await getLampConfig(req.params.id)
const config = {"deviceId":"4091519b56b8","readingLightColorTemperature":2001,"nightMode":false,"minimumLightLevel":0.8,"wifi":[{"ssid":"asu-visitor","password":""}],"brightness":0.5,"lampId":"626df507d6756882f166942c"}
return res.status(200).json(config)
})

router.patch('/:id/config', isAuthenticated, async (req, res) => {
// TODO: Wait for successful response using pEvent
updateLampConfig(req.params.id, req.body)
res.status(200).json({})
const config = await getLampConfig(req.params.id)
res.status(200).json(config)
})

// Move a lamp to another group
Expand Down

0 comments on commit 439f573

Please sign in to comment.