export default{
state: {
posts: [],
perPage: 15,
page: 1,
total: 0,
loading: false,
},
getters: {
numPages: state => Math.ceil( state.total / state.perPage ),
},
mutations: {
updateLoading: ( state, loading ) => state.loading = loading,
updatePosts: ( state, { posts, total, page } ) => Object.assign( state, { posts, total, page } ),
},
actions: {
async fetchPosts( { commit }, page ) {
commit('updateLoading', true)
const params = router.currentRoute
const start = ( page - 1 ) * state.perPage;
console.log( start )
const new__url = `/api/photos?${ params.name }=true&limit=15`
try {
const response = await fetch( new__url )
const posts = await response.json()
const total = posts.totalItems
commit( 'updatePosts', { posts, total, page })
} catch (e) {
console.error(e)
}
commit( 'updateLoading', false )
},
}
}