cateditor store: ensure a skeleton catalog to simplify code

This commit is contained in:
Seth Ladygo
2019-05-20 00:42:17 -07:00
parent d46623aaf6
commit 073a175b4f

View File

@ -6,9 +6,13 @@ import { paginateModels, sectionTitle, extendModelFromMaterial } from '@/paginat
Vue.use(Vuex)
function skeletonCatalog() {
return { sections: [] }
}
export const store = new Vuex.Store({
state: {
catalog: null,
catalog: skeletonCatalog(),
materials: {},
selectedSectionID: null,
selectedModelID: null,
@ -26,39 +30,23 @@ export const store = new Vuex.Store({
},
sections: state => {
if (state.catalog != null) {
return state.catalog.sections
} else {
return null
}
return state.catalog.sections
},
section: state => id => {
if (state.catalog && id) {
return state.catalog.sections.find(s => s.id === id)
} else {
return null
}
return state.catalog.sections.find(s => s.id === id)
},
nextSectionID: state => {
if (state.catalog != null) {
let id = 0
for (let section of state.catalog.sections) {
id = Math.max(id, section.id)
}
return id + 1
} else {
return -1
let id = 0
for (let section of state.catalog.sections) {
id = Math.max(id, section.id)
}
return id + 1
},
selectedSection: (state, getters) => {
if (state.catalog && state.selectedSectionID) {
return getters.section(state.selectedSectionID)
} else {
return null
}
return getters.section(state.selectedSectionID)
},
selectedModel: (state, getters) => {
@ -104,20 +92,8 @@ export const store = new Vuex.Store({
return getters.material(state.selectedMaterialID)
},
catalogSections: state => {
if (state.catalog != null) {
return state.catalog.sections
} else {
return null
}
},
catalogProperty: state => key => {
if (state.catalog != null) {
return state.catalog[key]
} else {
return null
}
return state.catalog[key]
},
//
@ -212,7 +188,11 @@ export const store = new Vuex.Store({
},
setCatalog(state, cat) {
state.catalog = cat
if (cat !== null) {
state.catalog = cat
} else {
state.catalog = skeletonCatalog()
}
},
setLoadingCatalog(state, value) {
@ -228,10 +208,7 @@ export const store = new Vuex.Store({
},
setCatalogProperty(state, { key, value }) {
console.log('mutation set prop', key, value)
if (state.catalog) {
state.catalog[key] = value
}
state.catalog[key] = value
},
},
@ -350,11 +327,6 @@ export const store = new Vuex.Store({
},
addMaterialToCatalog({ commit, getters, state }, material) {
if (!state.catalog) {
console.log('no catalog to add materials to')
return
}
// ensure material, not just id
if (typeof material === 'string' || typeof material === 'number') {
material = getters.material(material)
@ -432,7 +404,7 @@ export const store = new Vuex.Store({
},
newCatalog({ commit }) {
commit('setCatalog', {})
commit('setCatalog', skeletonCatalog())
},
async fetchProducts({ commit, dispatch }, text) {
@ -478,13 +450,13 @@ export const store = new Vuex.Store({
commit('setCatalog', response.data.catalog)
} else {
console.log('no catalog')
commit('setCatalog', null)
commit('newCatalog')
}
commit('setLoadingCatalog', false)
} catch (error) {
// TODO set loading error property
console.error('error loading catalog: ', error)
commit('setCatalog', null)
commit('newCatalog')
commit('setLoadingCatalog', false)
}
},
@ -500,7 +472,7 @@ export const store = new Vuex.Store({
commit('setCatalog', response.data)
} else {
console.error('no catalog returned from save!')
commit('setCatalog', null)
commit('newCatalog')
}
commit('setSavingCatalog', false)
})