summaryrefslogblamecommitdiff
path: root/iridescence/src/store/index.js
blob: fc77f7959824dacefd3a7dd3869263a21c9b7f25 (plain) (tree)
1
2
3
4
5
6
7
8
9

                        
                                           


              
                                
 
                               
          
                   
                 

                 

            


                        


                            
                     
                                            


                                                    



              
                             


                                

                                  














                                                                    


            






                                                                    
      


                                                                
     
    

             
import Vue from "vue";
import Vuex from "vuex";
import Dichroism from "@/api/dichroism.js";

Vue.use(Vuex);

let dichroism = new Dichroism();

export default new Vuex.Store({
  state: {
    searchTerm: "",
    products: [],
    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)
          .toLowerCase()
          .includes(state.searchTerm.toLowerCase());
      });
    }
  },
  mutations: {
    searchTerm(state, term) {
      if (term) {
        state.searchTerm = term;
      }
    },
    setProducts(state, 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: {
    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);
    },
    async createProduct(context, product) {
      const newProduct = await dichroism.createProduct(product);
      context.dispatch("addProduct", newProduct);
    }
  },
  modules: {}
});