From c5280144de096c274f185fade287ccd63b954ceb Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sun, 8 Nov 2020 21:26:34 -0500 Subject: fixed up product models and api calls, working on new product form --- iridescence/src/store/index.js | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) (limited to 'iridescence/src/store') diff --git a/iridescence/src/store/index.js b/iridescence/src/store/index.js index 2b24816..fc77f79 100644 --- a/iridescence/src/store/index.js +++ b/iridescence/src/store/index.js @@ -10,12 +10,16 @@ export default new Vuex.Store({ state: { searchTerm: "", products: [], - busy: false + busy: false, + progress: 100 }, getters: { busy(state) { return state.busy; }, + progress(state) { + return state.progress; + }, products(state) { return state.products.filter(item => { return JSON.stringify(item) @@ -26,19 +30,40 @@ export default new Vuex.Store({ }, mutations: { searchTerm(state, term) { - state.searchTerm = term; + if (term) { + state.searchTerm = term; + } }, setProducts(state, products) { - state.products = products; + if (products) { + state.products = products; + } + }, + replaceProduct(state, product) { + let index = state.products.findIndex(p => p.id == product.id); + + if (product && index >= 0) { + state.products[index] = product; + } + }, + addProduct(state, product) { + if (product) { + state.products.push(product); + } } }, actions: { - refreshProducts(context) { - context.commit("setProducts", dichroism.getProducts()); + async refreshProducts(context) { + const products = await dichroism.getProducts(); + context.commit("setProducts", products); + }, + async updateProduct(context, product) { + const updatedProduct = await dichroism.updateProduct(product); + context.dispatch("replaceProduct", updatedProduct); }, - updateProduct(context, product) { - dichroism.updateProduct(product); - context.dispatch("refreshProducts"); + async createProduct(context, product) { + const newProduct = await dichroism.createProduct(product); + context.dispatch("addProduct", newProduct); } }, modules: {} -- cgit v1.2.3