summaryrefslogblamecommitdiff
path: root/iridescence/src/store/index.js
blob: 5a5b5f77363b48d6178b54d4b9b67136564d0c95 (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 dichroismApi = new Dichroism();

export default new Vuex.Store({
  state: {
    searchTerm: "",
    products: []
  },
  getters: {
    products(state) {
      const searchTerm = state.searchTerm.toLowerCase();

      return state.products.filter(item => {
        return (
          item.name.toLowerCase().indexOf(searchTerm) != -1 ||
          item.description.toLowerCase().indexOf(searchTerm) != -1
        );
      });
    }
  },
  mutations: {
    searchTerm(state, term) {
      state.searchTerm = term;
    },
    setProducts(state, products) {
      state.products = products;
    }
  },
  actions: {
    refreshProducts(context) {
      context.commit("setProducts", []);
      context.commit("setProducts", dichroismApi.getProducts());
    },
    updateProduct(context, product) {
      dichroismApi.updateProduct(product);
      context.dispatch("refreshProducts");
    }
  },
  modules: {}
});