Skip to content

Commit

Permalink
Resolve nullish vm/vm_pool API object to empty list
Browse files Browse the repository at this point in the history
  • Loading branch information
rszwajko committed Mar 23, 2022
1 parent c32dfe8 commit f07ffe7
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/sagas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,27 @@ export function* fetchVms ({ payload: { count, page, shallowFetch = true } }) {
const additional = shallowFetch ? VM_FETCH_ADDITIONAL_SHALLOW : VM_FETCH_ADDITIONAL_DEEP
const apiVms = yield callExternalAction(Api.getVms, { payload: { count, page, additional } })

let internalVms = null
if (apiVms?.vm) {
internalVms = []
for (const apiVm of apiVms.vm) {
const internalVm = yield transformAndPermitVm(apiVm)
internalVms.push(internalVm)
}
if (!apiVms || apiVms.error) {
return { internalVms: null }
}

if (!apiVms.vm) {
// no VMs
return { internalVms: [] }
}

// NOTE: No need to fetch the current=true cdrom info at this point. The cdrom info
// is needed on the VM details page and `fetchSingleVm` is called upon entry
// to the details page. The `fetchSingleVm` fetch includes loading the
// appropriate cdrom info based on the VM's state. See `fetchSingleVm` for more
// details.
const internalVms = []
for (const apiVm of apiVms.vm) {
const internalVm = yield transformAndPermitVm(apiVm)
internalVms.push(internalVm)
}

// NOTE: No need to fetch the current=true cdrom info at this point. The cdrom info
// is needed on the VM details page and `fetchSingleVm` is called upon entry
// to the details page. The `fetchSingleVm` fetch includes loading the
// appropriate cdrom info based on the VM's state. See `fetchSingleVm` for more
// details.

return { internalVms }
}

Expand Down Expand Up @@ -240,11 +246,16 @@ export function* fetchAndPutSingleVm (action) {
export function* fetchPools (action) {
const apiPools = yield callExternalAction(Api.getPools, action)

const internalPools = apiPools?.vm_pool
? apiPools.vm_pool.map(pool => Transforms.Pool.toInternal({ pool }))
: null
if (!apiPools || apiPools.error) {
return { internalPools: null }
}

if (!apiPools.vm_pool) {
// no pools
return { internalPools: [] }
}

return { internalPools }
return { internalPools: apiPools.vm_pool.map(pool => Transforms.Pool.toInternal({ pool })) }
}

function* fetchAndPutPools (action) {
Expand Down

0 comments on commit f07ffe7

Please sign in to comment.