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