commerce-vuex
Version:
Commerce vuex module
260 lines (242 loc) • 7.86 kB
JavaScript
import { snakeCase } from 'lodash'
import humps from 'lodash-humps'
import { auth } from 'oak-vuex'
import createHumps from 'lodash-humps/lib/createHumps'
import { getField, updateField } from 'vuex-map-fields'
import retailerCollection from './retailer_collection.service'
import { setSizesRangeGroups, setPriceMap, retrievedPriceMap } from './common.service'
const { doPostOne, doPatchOne, doDeleteSelected, doFetchOne, doPostImportedDataOne, doExportData } = retailerCollection
const { getHubAuthToken } = auth
const retainHubName = process.env.VUE_APP_RETAINHUB_NAME
const snakes = createHumps(snakeCase)
export default {
namespaced: true,
state: {
one: null,
inProgress: false,
fetchInProgress: true,
oneItemVariants: {},
sizesRangeGroup: [],
retailPriceList: [{ orderCcy: '', price: '' }],
retailRecommendedPriceList: [{ orderCcy: '', price: '' }],
wholesalePriceList: [{ orderCcy: '', price: '' }],
},
getters: {
getField,
},
mutations: {
updateField,
setOne(state, one) {
state.one = one;
state.sizesRangeGroup = setSizesRangeGroups(state.one.sizeRange, state.one.sizes)
state.retailPriceList = retrievedPriceMap(state.one.priceRetailMap)
state.retailRecommendedPriceList = retrievedPriceMap(state.one.priceRecommendedRetailMap)
state.wholesalePriceList = retrievedPriceMap(state.one.priceWholesaleMap)
},
inProgress(state, yesOrNo) {
state.inProgress = yesOrNo
},
fetchInProgress(state, yesOrNo) {
state.fetchInProgress = yesOrNo
},
setOneItemVariants(state, oneItem) {
state.oneItemVariants = oneItem;
state.sizesRangeGroup = setSizesRangeGroups(state.oneItem.sizeRange, state.oneItem.sizes)
},
clearSizesRangeGroup(state) {
state.sizesRangeGroup = [];
},
setSizesRange(state) {
if (state.sizesRangeGroup.length > 0) {
state.one.sizes = state.sizesRangeGroup.map((item) => item.name)
state.one.sizeRange = state.sizesRangeGroup.find((item) => item.group).group
}
},
//retail price
addRetailPrice(state) {
state.retailPriceList.push({ orderCcy: '', price: '' })
},
deleteRetailPrice(state, index) {
state.retailPriceList.splice(index, 1)
},
//retail recommended price
addRetailRecommendedPrice(state) {
state.retailRecommendedPriceList.push({ orderCcy: '', price: '' })
},
deleteRetailRecommendedPrice(state, index) {
state.retailRecommendedPriceList.splice(index, 1)
},
//wholesale price
addWholesalePrice(state) {
state.wholesalePriceList.push({ orderCcy: '', price: '' })
},
deleteWholesalePrice(state, index) {
state.wholesalePriceList.splice(index, 1)
},
setPriceMap(state) {
state.one.priceRetailMap = setPriceMap(state.retailPriceList)
state.one.priceRecommendedRetailMap = setPriceMap(state.retailRecommendedPriceList)
state.one.priceWholesaleMap = setPriceMap(state.wholesalePriceList)
}
},
actions: {
new({ commit }, { appId, retailerId }) {
commit('clearSizesRangeGroup');
commit('setOne', {
brandId: null,
name: null,
sku: null,
retailerId: retailerId,
appId: appId,
sizes: [],
colors: [],
metadata: null,
priceRetailMap: null,
priceRecommendedRetailMap: null,
priceWholesaleMap: null,
sizeCategory: null,
sizeRange: null,
skuVars: null,
impexMap: null,
barcode: null,
barcodeVars: null
})
},
// newItemVariants({ commit }) {
// commit('clearSizesRangeGroup');
// commit('setOneItemVariants', {
// brandId: '',
// name: '',
// sku: '',
// retailerId: '',
// appId: '',
// sizes: '',
// colors: '',
// metadata: '',
// priceRetailMap: '',
// priceRecommendedRetailMap: '',
// priceWholesaleMap: '',
// sizeCategory: '',
// sizeRange: '',
// skuVars: '',
// impexMap: '',
// barcode: '',
// barcodeVars: ''
// })
// // commit('setItemInformationList');
// },
save: async ({ state, commit }) => {
try {
await commit('setSizesRange')
await commit('setPriceMap')
console.log(state.one)
if (undefined === state.one.id) {
delete state.one.brand;
let postParams = snakes(Object.assign({}, state.one))
let collection = await doPostOne(postParams)
commit('inProgress', false)
return ({ new: true, collection })
} else {
delete state.one.brand;
let collection = await doPatchOne(snakes(state.one))
commit('inProgress', false)
return ({ update: true, collection })
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
fetchOne: async ({ commit }, { collectionId, appId }) => {
commit('inProgress', true);
try {
let collection = await doFetchOne({ collectionId, appId });
if (collection) {
commit('setOne', humps(collection))
commit('inProgress', false)
return { collection: humps(collection) }
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
deleteSelected: async ({ commit }, { collectionId }) => {
commit('inProgress', true);
try {
await doDeleteSelected({ collectionId });
return ({ deleteSelected: true })
} catch (err) {
return ({ deleteSelected: false })
}
finally {
commit('inProgress', false);
}
},
clearSizeRange: async ({ commit }) => {
commit('clearSizesRangeGroup');
},
// retailer price
addRetailPrice: async ({ commit }) => {
commit('addRetailPrice')
},
deleteRetailPrice: async ({ commit }, { index }) => {
commit('deleteRetailPrice', index)
},
// retailer recommended price
addRetailRecommendedPrice: async ({ commit }) => {
commit('addRetailRecommendedPrice')
},
deleteRetailRecommendedPrice: async ({ commit }, { index }) => {
commit('deleteRetailRecommendedPrice', index)
},
// wholesale price
addWholesalePrice: async ({ commit }) => {
commit('addWholesalePrice')
},
deleteWholesalePrice: async ({ commit }, { index }) => {
commit('deleteWholesalePrice', index)
},
// import file data
saveImportedData: async ({ commit }, {data, format}) => {
commit('inProgress', true);
try {
let collection = await doPostImportedDataOne(({collection: snakes(Object.assign({}, data,format))}))
commit('inProgress', false)
return ({ new: true, collection })
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
// Export file data
exportData: async ({ commit }, {retailerId, brandId, type, providerBusinessEntityId, purchaserBusinessEntityId, format }) => {
commit('inProgress', true);
try {
let retainhubToken = getHubAuthToken(retainHubName)
let blob = await doExportData({retailerId, brandId, retainhubToken, type, providerBusinessEntityId, purchaserBusinessEntityId, format })
commit('inProgress', false)
return ({ blob })
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
},
}