[Vuejs]-Get DRF paginated endpoint from axios

0👍

Based on this answer, I made a factory that takes a url and returns a working function:

// returns a promise once it gets ALL pages from API
function paginated_factory(url) {
    return function paginatedFetcher(next, obj_list = []) {
        return session.get(next ? next : url)
            .then(({data}) => {
                obj_list.push(...data.results)
                if (!data.next) return obj_list
                return paginatedFetcher(data.next, obj_list)
            })
    }
}

Then, my API module function:

bars: paginated_factory('/bars/')

And my vuex action:

barsRefresh(context) {
    api.bars().then((data) => context.commit('setBars', data))
},

Leave a comment