commerce-vuex
Version:
Commerce vuex module
288 lines (281 loc) • 12.7 kB
JavaScript
import humps from 'lodash-humps';
import service from './service.service';
const { doFetchList, doFetchDistributorList,doFetchServiceTeamsLookUpList,doFetchBusinessEntityWithServiceLookUpList,doFetchServiceLookUpList } = service
export default {
namespaced: true,
state: {
list: [],
selected: [],
recordsCount: 0,
inProgress: false,
error: null,
distributorRecordsCount: 0,
externalSalesAgenciesListCount:0,
selectedDistributor: [],
distributorList: [],
externalSalesAgenciesList:[],
serviceTeamsLookUpList:[],
distributorsForBrands:[],
businessEntityWithServiceLookUpList:[],
serviceLookUpList:[]
},
mutations: {
inProgress(state, yesOrNo) {
state.inProgress = yesOrNo
},
setList(state, list) {
state.selected = [];
for (let i in state.list) {
state.selected.push(false);
state.selected[i] = false;
}
state.list = list;
},
setServiceTeamsLookUpList(state, list) {
state.serviceTeamsLookUpList = list;
},
setDistributorList(state, list) {
state.selectedDistributor = [];
/*for (let i in state.distributorList) {
state.selectedDistributor.push(false);
state.selectedDistributor[i] = false;
}*/
state.distributorList = list;
},
setListCount(state, number) {
state.recordsCount = number;
},
setDistributorListCount(state, number) {
state.distributorRecordsCount = number;
},
setExternalSalesAgenciesList(state, list) {
state.externalSalesAgenciesList = list;
},
setExternalSalesAgenciesListCount(state, number) {
state.externalSalesAgenciesListCount = number;
},
setBusinessEntityWithServiceLookUpList(state,list){
state.businessEntityWithServiceLookUpList = list;
},
setServiceLookUpList(state,list){
state.serviceLookUpList = list;
},
},
actions: {
fetchList: async ({ commit }, { appId, businessEntityId, countryIso3,serviceId, nameLike, pageNumber, limit, brandId }) => {
commit('inProgress', true);
try {
let offset = (pageNumber - 1) * limit;
const response = await doFetchList({ appId, serviceId, businessEntityId, countryIso3,nameLike, limit, offset,brandId });
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)));
return humps(list)
} else
commit('setList', []);
/*if (recursive && state.list[0].appId != rootState.appOne.one.id) {
dispatch('appOne/fetchOne', state.list[0].appId, { root: true });
}*/
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
fetchDistributorList: async ({ commit, dispatch }, { appId, brandIds,businessEntityId,naicsCode, nameLike, pageNumber, limit }) => {
commit('inProgress', true);
try {
let offset = (pageNumber - 1) * limit;
const response = await doFetchDistributorList({ appId, brandIds, businessEntityId,naicsCode,nameLike, limit, offset });
if (response.range != null) {
let recordsCount = response.range.split("/");
commit('setDistributorListCount', Number(recordsCount[1]));
const list = await response.data;
if (list && list.length > 0) {
let ditributorList = humps(list);
let brandIds = []
ditributorList.filter((item)=>{
if(item.brands&&item.brands.length){
item.brands.map((brand)=>brandIds.push(brand));
}
});
let brandList = await dispatch("brandList/fetchBrandListByIds", {
appId, nameLike, limit:1000, offset:0, brandIds
}, {root: true});
let newList = ditributorList.filter((item)=>{
let brandsInfo = '' //[]
if(item.brands&&item.brands.length){
item.brands.filter((brand)=>{
for(let i=0; i<brandList.length; i++){
if(brandList[i].id === brand){
brandsInfo = brandList[i].name+', '+brandsInfo
// brandsInfo.push({
// name: brandList[i].name,
// id: brandList[i].id,
// })
break;
}
}
});
}
item.brandsInfo = brandsInfo
return item;
});
commit('setDistributorList', newList);
} else
commit('setDistributorList', []);
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
fetchExternalSalesAgenciesList: async ({ commit, dispatch }, { appId, brandIds,businessEntityId,naicsCode, nameLike, pageNumber, limit }) => {
commit('inProgress', true);
try {
let offset = (pageNumber - 1) * limit;
const response = await doFetchDistributorList({ appId, brandIds, businessEntityId,naicsCode,nameLike, limit, offset });
if (response.range != null) {
let recordsCount = response.range.split("/");
commit('setExternalSalesAgenciesListCount', Number(recordsCount[1]));
const list = await response.data;
if (list && list.length > 0) {
let ditributorList = humps(list);
let brandIds = []
ditributorList.filter((item)=>{
if(item.brands&&item.brands.length){
item.brands.map((brand)=>brandIds.push(brand));
}
});
let brandList = await dispatch("brandList/fetchBrandListByIds", {
appId, nameLike, limit:1000, offset:0, brandIds
}, {root: true});
let newList = ditributorList.filter((item)=>{
let brandsInfo = '' //[]
if(item.brands&&item.brands.length){
item.brands.filter((brand)=>{
for(let i=0; i<brandList.length; i++){
if(brandList[i].id === brand){
brandsInfo = brandList[i].name+', '+brandsInfo
// brandsInfo.push({
// name: brandList[i].name,
// id: brandList[i].id,
// })
break;
}
}
});
}
item.brandsInfo = brandsInfo
return item;
});
commit('setExternalSalesAgenciesList', newList);
} else
commit('setExternalSalesAgenciesList', []);
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
serviceTeamsLookUpList: async ({ commit}, { serviceTeam }) => {
commit('inProgress', true);
try {
const response = await doFetchServiceTeamsLookUpList({ serviceTeam});
if(response){
commit('setServiceTeamsLookUpList', humps(response));
//commit('setList', humps((response.items ? response.items : [])));
//commit('setListCount', Number(response.items? response.items.length : 0));
commit('inProgress', false);
return humps((response.items))
}else{
commit('setServiceTeamsLookUpList', []);
commit('inProgress', false);
return []
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
businessEntityWithServiceLookUpList: async ({ commit},{salesAgentServiceFilter} ) => {
commit('inProgress', true);
try {
const response = await doFetchBusinessEntityWithServiceLookUpList( {salesAgentServiceFilter});
if(response){
//commit('setBusinessEntityWithServiceLookUpList', humps(response));
commit('setBusinessEntityWithServiceLookUpList', humps((response ? response : [])));
commit('setListCount', Number(response? response.length : 0));
commit('inProgress', false);
return humps((response))
}else{
commit('setBusinessEntityWithServiceLookUpList', []);
commit('inProgress', false);
return []
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
fetchServiceLookUpList: async ({ commit},{filter,appId} ) => {
commit('inProgress', true);
try {
const response = await doFetchServiceLookUpList( {filter,appId});
if(response){
//commit('setBusinessEntityWithServiceLookUpList', humps(response));
commit('setServiceLookUpList', humps((response ? response : [])));
commit('setListCount', Number(response? response.length : 0));
commit('inProgress', false);
return humps((response))
}else{
commit('setServiceLookUpList', []);
commit('inProgress', false);
return []
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
/*deleteSelected: async ({ commit }, brandId) => {
commit('inProgress', true);
try {
await doDeleteSelected(brandId);
return ({ deleteSelected: true })
} catch (err) {
throw 'error'
}
finally {
commit('inProgress', false);
}
},*/
}
}