Skip to content

Commit

Permalink
Added promises
Browse files Browse the repository at this point in the history
  • Loading branch information
jlpereira committed Sep 18, 2020
1 parent 11bc88c commit 4150b70
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
Expand Up @@ -2,18 +2,23 @@ import { GetOtuAssertedDistribution } from '../../request/resources'
import { MutationNames } from '../mutations/mutations'

export default ({ state, commit }, otuId) => {
GetOtuAssertedDistribution({ otu_id: otuId, geo_json: true }).then(response => {
state.loadState.assertedDistribution = false
commit(MutationNames.SetAssertedDistributions, state.assertedDistributions.concat(response.body).sort((a, b) => {
const compareA = a.geographic_area.name
const compareB = b.geographic_area.name
if (compareA < compareB) {
return -1
} else if (compareA > compareB) {
return 1
} else {
return 0
}
}))
return new Promise((resolve, reject) => {
GetOtuAssertedDistribution({ otu_id: otuId, geo_json: true }).then(response => {
state.loadState.assertedDistribution = false
commit(MutationNames.SetAssertedDistributions, state.assertedDistributions.concat(response.body).sort((a, b) => {
const compareA = a.geographic_area.name
const compareB = b.geographic_area.name
if (compareA < compareB) {
return -1
} else if (compareA > compareB) {
return 1
} else {
return 0
}
}))
resolve(response)
}, error => {
reject(error)
})
})
}
Expand Up @@ -2,14 +2,21 @@ import { GetCollectingEvents, GetGeoreferences } from '../../request/resources'
import { MutationNames } from '../mutations/mutations'

export default ({ state, commit }, otusId) => {
GetCollectingEvents(otusId).then(response => {
const CEs = response.body
return new Promise((resolve, reject) => {
GetCollectingEvents(otusId).then(response => {
const CEs = response.body

commit(MutationNames.SetCollectingEvents, state.collectingEvents.concat(CEs))
if (CEs.length) {
GetGeoreferences(CEs.map(ce => ce.id)).then(response => {
commit(MutationNames.SetGeoreferences, state.georeferences.concat(response.body))
})
}
commit(MutationNames.SetCollectingEvents, state.collectingEvents.concat(CEs))
if (CEs.length) {
GetGeoreferences(CEs.map(ce => ce.id)).then(response => {
commit(MutationNames.SetGeoreferences, state.georeferences.concat(response.body))
resolve(CEs)
})
} else {
resolve(CEs)
}
}, error => {
reject(error)
})
})
}
@@ -1,13 +1,23 @@
import ActionNames from './actionNames'

export default ({ dispatch }, otus) => {
otus.forEach(otu => {
dispatch(ActionNames.LoadTaxonName, otu.taxon_name_id)
dispatch(ActionNames.LoadCollectionObjects, otu.id).then(() => {
dispatch(ActionNames.LoadCollectingEvents, [otu.id])
function loadOtuInformation (otu) {
const promises = []
return new Promise((resolve, reject) => {
promises.push(dispatch(ActionNames.LoadCollectionObjects, otu.id).then(() => {
dispatch(ActionNames.LoadCollectingEvents, [otu.id])
}))
promises.push(dispatch(ActionNames.LoadAssertedDistributions, otu.id))
Promise.all(promises).then(() => {
resolve()
})
})
dispatch(ActionNames.LoadDescendants, otu)
dispatch(ActionNames.LoadAssertedDistributions, otu.id)
})
}
dispatch(ActionNames.LoadTaxonName, otus[0].taxon_name_id)
dispatch(ActionNames.LoadDescendants, otus[0])
dispatch(ActionNames.LoadPreferences)

otus.forEach(async otu => {
await loadOtuInformation(otu)
})
}

0 comments on commit 4150b70

Please sign in to comment.