-
-
Save mtermoul/e5c7cbddc5233ff04e8c185143cf987e to your computer and use it in GitHub Desktop.
col-admin/src/store/modules/cosmic.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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