commerce-vuex
Version:
Commerce vuex module
109 lines (95 loc) • 3.13 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 brandCatalogService from './brand_catalog.service'
const { doPostOne, doPatchOne, doFetchOne } = brandCatalogService
const snakes = createHumps(snakeCase)
export default {
namespaced: true,
state: {
one: null,
inProgress: true,
OrgList: [],
},
getters: {
getField,
},
mutations: {
updateField,
setOne(state, one) {
state.one = one;
},
inProgress(state, yesOrNo) {
state.inProgress = yesOrNo
},
setList(state, list) {
state.OrgList = list;
},
},
actions: {
new({ commit },{appId, brandId}) {
commit('inProgress', false);
commit('setOne', {
"name": "",
"appId":appId,
"brandId":brandId,
"metadata":{ ashtaSeason : ""}
});
},
fetchOne: async ({ commit }, { brandCatalogId, appId }) => {
commit('inProgress', true);
try {
const brand = await doFetchOne({brandCatalogId,appId});
if (brand) {
commit('setOne', humps(brand));
return humps(brand);
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
console.log(err)
//throw err
}
finally {
commit('inProgress', false)
}
},
save: async ({ commit, state }) => {
commit('inProgress', true);
try {
if (undefined === state.one.id) {
let brand = await doPostOne({brandCatalog:snakes(Object.assign({}, state.one))});
commit('inProgress', false);
return ({ new: true, brand: humps(brand) });
} else {
let brand = await doPatchOne(snakes(state.one));
commit('inProgress', false);
return ({ update: true, brand: humps(brand) });
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
cloneLineSheetOne: async ({ commit, state },{newObj}) => {
commit('inProgress', true);
try {
let lineSheet = await doPostOne(({brandCatalog:snakes(newObj)}));
commit('inProgress', false);
return ({ new: true, lineSheet: humps(lineSheet) });
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
},
}