Skip to content

Instantly share code, notes, and snippets.

@mtermoul
Created April 3, 2019 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtermoul/e5c7cbddc5233ff04e8c185143cf987e to your computer and use it in GitHub Desktop.
Save mtermoul/e5c7cbddc5233ff04e8c185143cf987e to your computer and use it in GitHub Desktop.
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