UNPKG

commerce-vuex

Version:
69 lines (68 loc) 2.6 kB
import humps from 'lodash-humps'; import retailerCollection from './retailer_collection.service' const { doFetchList } = retailerCollection export default { namespaced: true, state: { list: [], recordsCount: 0, listInProgress: true, brandList: [] }, mutations: { inProgress(state, yesOrNo) { state.inProgress = yesOrNo }, setList(state, list) { state.list = list; }, setListCount(state, number) { state.recordsCount = number; }, setBrandList(state, brands) { state.brandList = brands; }, }, actions: { fetchList: async ({ commit, dispatch}, { appId, search, retailerId,brandId,limit,pageNumber}) => { commit('inProgress', true); try { let offset = (pageNumber - 1) * limit; const response = await doFetchList({ appId, search, retailerId,brandId,limit,offset}); if (response.range != null) { let recordsCount = response.range.split("/"); commit('setListCount', Number(recordsCount[1])); const list = await response.data; if (list) { let brandIds = [] list.filter((item) => { if(item.brand_id) brandIds.push(item.brand_id) }) const unqBrandIds = [...new Set(brandIds)] if (unqBrandIds) { const ids = unqBrandIds.join() let brandList = await dispatch('brandList/fetchBrandListByIds', { appId:appId,brandIds:ids,nameLike:'',pageNumber:1, limit:unqBrandIds.length }, { root: true }) commit('setBrandList', humps((brandList))); commit('inProgress', false) } commit('setList', humps((list))); commit('inProgress', false); return humps(list) } else commit('setList', []); commit('inProgress', false); } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } } } }