commerce-vuex
Version:
Commerce vuex module
157 lines (145 loc) • 5.32 kB
JavaScript
import { snakeCase } from 'lodash'
import humps from 'lodash-humps'
import createHumps from 'lodash-humps/lib/createHumps'
import { getField, updateField } from 'vuex-map-fields'
import brandProductsService from './brand_products.service'
import { setSizesRangeGroups } from './common.service'
const { doPostOne, doPatchOne, doFetchOne, doDeleteSelected } = brandProductsService
const snakes = createHumps(snakeCase)
export default {
namespaced: true,
state: {
one: null,
inProgress: true,
OrgList: [],
sizesRangeGroup:[]
},
getters: {
getField,
},
mutations: {
updateField,
setOne(state, one) {
state.one = one;
state.sizesRangeGroup=setSizesRangeGroups(state.one.sizeRange,state.one.sizes)
},
inProgress(state, yesOrNo) {
state.inProgress = yesOrNo
},
setList(state, list) {
state.OrgList = list;
},
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
}
},
},
actions: {
new({ commit },{appId, brandId,brandCatalogId}) {
commit('inProgress', false);
commit('clearSizesRangeGroup');
commit('setOne', {
"appId":appId,
"brandId":brandId,
"brandCatalogId":brandCatalogId,
"name":"",
"sku":"",
"imageUrls":"",
"sizes":[],
"colors": [],
"priceWholesale":"",
"sizeCategory":null,
"sizeRange":null,
"priceRetail": "",
"priceWholesaleUsd":null,
"priceWholesaleEur":null,
"priceWholesaleGbp":null,
"priceWholesaleAed":null,
"priceRetailUsd":null,
"priceRetailEur":null,
"priceRetailGbp":null,
"priceRetailAed":null,
"barcodeMap": null,
"skuMap": null
});
},
fetchOne: async ({ commit }, { brandProductId, appId }) => {
commit('inProgress', true);
try {
let brand = await doFetchOne({brandProductId,appId});
if (brand) {
let { barcode_map, sku_map } = brand
brand = humps(brand)
brand.barcodeMap = barcode_map
brand.skuMap = sku_map
commit('setOne', brand);
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
save: async ({ commit, state }) => {
commit('inProgress', true);
try {
commit('setSizesRange')
if (undefined === state.one.id) {
let { barcodeMap, skuMap } = state.one
let brandProduct = snakes(Object.assign({}, state.one))
brandProduct.barcode_map = barcodeMap
brandProduct.sku_map = skuMap
let brand = await doPostOne({brandProduct});
commit('inProgress', false);
let { barcode_map, sku_map } = brand
brand = humps(brand)
brand.barcodeMap = barcode_map
brand.skuMap = sku_map
return ({ new: true, brand: (brand) });
} else {
let { barcodeMap, skuMap } = state.one
let brandProduct = snakes(state.one)
brandProduct.barcode_map = barcodeMap
brandProduct.sku_map = skuMap
let brand = await doPatchOne(brandProduct);
commit('inProgress', false);
let { barcode_map, sku_map } = brand
brand = humps(brand)
brand.barcodeMap = barcode_map
brand.skuMap = sku_map
return ({ update: true, brand: (brand) });
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
clearSizeRange: async ({ commit }) => {
commit('clearSizesRangeGroup');
},
deleteBrandStyle: async ({ commit }, { brandStyleId }) => {
commit('inProgress', true);
try {
await doDeleteSelected({ brandStyleId });
return ({ deleteSelected: true })
} catch (err) {
console.log('error', err)
}
finally {
commit('inProgress', false);
}
},
},
}