Skip to content

Instantly share code, notes, and snippets.

@mtermoul

mtermoul/cosmic.js Secret

Created Apr 3, 2019
Embed
What would you like to do?
col-admin/src/store/modules/cosmic.js
import Cosmic from '../../api/cosmic' // used for Rest API
const actions = {
async fetchStudents ({commit, dispatch}) {
const recordLimit = 25
let skipPos = 0
let fetchMore = true
while (fetchMore) {
try {
const params = {
type: 'students',
limit: recordLimit,
skip: skipPos
}
let res = await Cosmic.getObjects(params)
if (res.objects && res.objects.length) {
let data = res.objects.map((item) => {
return {...item.metadata, id: item.metadata.sid,
firstName: item.metadata.firstname,
lastName: item.metadata.lastname }
})
commit('ADD_STUDENTS', data)
commit('SET_IS_DATA_READY', true)
// if fetched recodrs lenght is less than 25 then we have end of list
if (res.objects.length < recordLimit) fetchMore = false
} else {
fetchMore = false
}
skipPos += recordLimit
}
catch (error) {
console.log(error)
fetchMore = false
}
}
dispatch('fetchStates')
}
}
export default {
actions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.