summaryrefslogblamecommitdiff
path: root/iridescence/src/store/index.js
blob: f98fd875a1dc2318316eb8cde1b65d0cc6146bef (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: {
    toggleBusy(state) {
      state.busy = !state.busy;
    },
    searchTerm(state, term) {
      if (term) {
        state.searchTerm = term;
      }
    },
    setProducts(state, products) {
      if (products) {
        state.products = products;
      }
    },
    replaceProduct(state, product) {
      if (!product || !product.id) {
        return;
      }

      let index = state.products.findIndex(p => p.id == product.id);

      if (index) {
        state.products[index] = product;
      }
    },
    addProduct(state, product) {
      if (product) {
        state.products.push(product);
      }
    }
  },
  actions: {
    async refreshProducts({ commit }) {
      commit("toggleBusy");
      const products = await dichroism.getProducts();
      commit("setProducts", products);
      commit("toggleBusy");
    },
    async updateProduct({ commit }, product) {
      commit("toggleBusy");
      const updatedProduct = await dichroism.updateProduct(product);
      commit("replaceProduct", updatedProduct);
      commit("toggleBusy");
    },
    async createProduct({ commit }, product) {
      commit("toggleBusy");
      const newProduct = await dichroism.createProduct(product);
      commit("addProduct", newProduct);
      commit("toggleBusy");
    },
    async createPhotoSet({ commit }, file) {
      commit("toggleBusy");
      const photoSet = await dichroism.createPhoto(file);
      commit("toggleBusy");
      return photoSet;
    }
  },
  modules: {}
});