diff options
Diffstat (limited to 'iridescence/src/store/index.js')
-rw-r--r-- | iridescence/src/store/index.js | 41 |
1 files changed, 33 insertions, 8 deletions
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: {} |