import api from '../../utils/api' import { allBySlug, repackBySort } from './arrangements' const state = { all: [], loaded: false, singleObject: null, } const getters = { allObjects: state => state.all, allObjectsBySlug: state => allBySlug(state), allObjectsLoaded: state => state.loaded, } const actions = { getAllObjects({ commit }, { sortType, params }) { commit('CLEAR_OBJECTS') commit('OBJECTS_LOADED', false) const storeFetch = objects => { let repacked = repackBySort(objects, sortType) commit('STORE_FETCHED_OBJECTS', { objects: repacked }) commit('OBJECTS_LOADED', true) } return api.getByType({ type: 'object', sort: sortType, params, cb: storeFetch, }) }, getMoreObjects({ commit }, { sortType, params }) { const storeFetch = objects => { let repacked = objects commit('ADD_TO_FETCHED_OBJECTS', { objects: repacked }) commit('OBJECTS_LOADED', true) } return api.getByType({ type: 'object', sort: sortType, params, cb: storeFetch, }) }, getSingleObject({ commit }, id) { commit('CLEAR_SINGLE_OBJECTS') commit('OBJECTS_LOADED', false) return api.getSingleType('object', id, object => { commit('STORE_FETCHED_SINGLE_OBJECT', object) commit('OBJECTS_LOADED', true) }) }, } const mutations = { ADD_TO_FETCHED_OBJECTS(state, { objects }) { state.all = [...state.all, ...objects] }, STORE_FETCHED_OBJECTS(state, { objects }) { state.all = objects }, STORE_FETCHED_SINGLE_OBJECT(state, object) { state.singleObject = object }, CLEAR_OBJECTS(state) { state.all = [] }, CLEAR_SINGLE_OBJECTS(state) { state.singleObject = null }, OBJECTS_LOADED(state, val) { state.loaded = val }, } export default { state, getters, actions, mutations }