UNPKG

commerce-vuex

Version:
94 lines (88 loc) 3.04 kB
import { snakeCase } from 'lodash' import humps from 'lodash-humps' import createHumps from 'lodash-humps/lib/createHumps' import { getField, updateField } from 'vuex-map-fields' import connectionService from './connection.service' const { doFetchList,doFetchBusinessConnectionLookUp } = connectionService const snakes = createHumps(snakeCase) export default { namespaced: true, state: { list: [], inProgress: true, recordsCount: 0, businessConnectionLookUpList:[] }, getters: { getField, }, mutations: { updateField, setList(state, list) { state.list = list; }, inProgress(state, yesOrNo) { state.inProgress = yesOrNo }, setListCount(state, number) { state.recordsCount = number; }, setBusinessConnectionLookUpList(state, list) { state.businessConnectionLookUpList = list; }, }, actions: { fetchList: async ({ commit }, { nameLike, ownerId, pageNumber, limit,sortByDate}) => { commit('inProgress', true); try { let offset = (pageNumber - 1) * limit; const response = await doFetchList({ nameLike, ownerId, limit, offset,sortByDate }); if (response.range != null) { let recordsCount = response.range.split("/"); commit('setListCount', Number(recordsCount[1])); const list = await response.data; if (list) { commit('setList', humps((list))); commit('inProgress', false); return humps((list)); } else{ commit('setList', []); commit('inProgress', false); return [] } } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, fetchBusinessConnectionLookUpList: async ({ commit }, {filters}) => { commit('inProgress', true); try { const list = await doFetchBusinessConnectionLookUp({filters }); if (list) { commit('setBusinessConnectionLookUpList', humps((list))); commit('setList', humps((list ? list : []))); commit('setListCount', Number(list? list.length : 0)); commit('inProgress', false); return humps((list)); } else{ commit('setBusinessConnectionLookUpList', []); commit('inProgress', false); return [] } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, }, }