import api from '../../utils/api' import { allBySlug, repackBySort } from './arrangements' const state = { all: [], loaded: false, singlePublication: null, } const getters = { allPublications: state => state.all, allPublicationsBySlug: state => allBySlug(state), allPublicationsLoaded: state => state.loaded, } const actions = { getAllPublications({ commit }, { sortType, params }) { commit('CLEAR_PUBLICATIONS') commit('PUBLICATIONS_LOADED', false) const storeFetch = publications => { let repacked = repackBySort(publications, sortType) commit('STORE_FETCHED_PUBLICATIONS', { publications: repacked }) commit('PUBLICATIONS_LOADED', true) } return api.getByType({ type: 'publication', sort: sortType, params, cb: storeFetch, }) }, getMorePublications({ commit }, { sortType, params }) { const storeFetch = publications => { let repacked = publications commit('ADD_TO_FETCHED_PUBLICATIONS', { publications: repacked }) commit('PUBLICATIONS_LOADED', true) } return api.getByType({ type: 'publication', sort: sortType, params, cb: storeFetch, }) }, getSinglePublication({ commit }, id) { commit('CLEAR_SINGLE_PUBLICATIONS') commit('PUBLICATIONS_LOADED', false) return api.getSingleType('publication', id, publication => { commit('STORE_FETCHED_SINGLE_PUBLICATION', publication) commit('PUBLICATIONS_LOADED', true) }) }, } const mutations = { ADD_TO_FETCHED_PUBLICATIONS(state, { publications }) { state.all = [...state.all, ...publications] }, STORE_FETCHED_PUBLICATIONS(state, { publications }) { state.all = publications }, STORE_FETCHED_SINGLE_PUBLICATION(state, publication) { state.singlePublication = publication }, CLEAR_PUBLICATIONS(state) { state.all = [] }, CLEAR_SINGLE_PUBLICATIONS(state) { state.singlePublication = null }, PUBLICATIONS_LOADED(state, val) { state.loaded = val }, } export default { state, getters, actions, mutations }