From 82a8d8f953854c0fee4748116b540a2f56d7ba45 Mon Sep 17 00:00:00 2001 From: Seth Ladygo Date: Fri, 3 May 2019 17:45:01 -0700 Subject: [PATCH] front end handle saving catalog --- cateditor/src/pages/editor/store/index.js | 46 ++++++++++++++++++++--- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/cateditor/src/pages/editor/store/index.js b/cateditor/src/pages/editor/store/index.js index 690499d..a745c19 100644 --- a/cateditor/src/pages/editor/store/index.js +++ b/cateditor/src/pages/editor/store/index.js @@ -79,6 +79,7 @@ export const store = new Vuex.Store({ selectedModelID: null, selectedMaterial: null, loadingCatalog: true, + savingCatalog: false, }, getters: { // @@ -130,6 +131,10 @@ export const store = new Vuex.Store({ loadingCatalog(state) { return state.loadingCatalog }, + + savingCatalog(state) { + return state.savingCatalog + }, }, mutations: { // SET_CAT_SECTIONS: (state, payload) => { @@ -167,6 +172,10 @@ export const store = new Vuex.Store({ state.loadingCatalog = value }, + setSavingCatalog(state, value) { + state.savingCatalog = value + }, + setCatalogProperty(state, { key, value }) { console.log('mutation set prop', key, value) if (state.catalog) { @@ -199,28 +208,55 @@ export const store = new Vuex.Store({ // context.commit('ADD_TODO',payload) // }, + setCatalogProperty(context, data) { + context.commit('setCatalogProperty', data) + }, + async loadCatalog({ commit }, id) { try { commit('setLoadingCatalog', true) const response = await axios.get('/api/v1/catalogs/id/' + id) - if ('catalog' in response.data) { - console.log('recieved catalog:', response.data.catalog) - commit('setCatalog', response.data.catalog) + if (response.data) { + console.log('recieved catalog:', response.data) + commit('setCatalog', response.data) + commit('setCatalogID', id) } else { console.log('no catalog') commit('setCatalog', null) + commit('setCatalogID', null) } commit('setLoadingCatalog', false) } catch (error) { // TODO set loading error property console.error('error loading catalog: ', error) commit('setCatalog', null) + commit('setCatalogID', null) commit('setLoadingCatalog', false) } }, - setCatalogProperty(context, data) { - context.commit('setCatalogProperty', data) + async saveCatalog({ commit, state }) { + commit('setSavingCatalog', true) + + axios.post('/api/v1/catalogs/save', state.catalog) + .then(function(response) { + console.log('save response', response) + if (response.data) { + console.log('recieved catalog:', response.data) + commit('setCatalog', response.data) + commit('setCatalogID', response.data.id) + } else { + console.error('no catalog returned from save!') + commit('setCatalog', null) + commit('setCatalogID', null) + } + commit('setSavingCatalog', false) + }) + .catch(function(error) { + // TODO set saving error property + console.error('error saving catalog: ', error) + commit('setSavingCatalog', false) + }) }, }