summaryrefslogblamecommitdiff
path: root/iridescence/src/store/index.js
blob: 48cae11e32b5ffad32847ccac531e97c7a509d03 (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: [],
    cart: {},
    busy: false,
    compare: a => (a.featured ? -1 : 1),
    productDetailId: 0
  },
  getters: {
    products(state) {
      return state.products
        .filter(item => {
          return JSON.stringify(item)
            .toLowerCase()
            .includes(state.searchTerm.toLowerCase());
        })
        .sort(state.compare);
    }
  },
  mutations: {
    toggleBusy(state) {
      state.busy = !state.busy;
    },
    productDetailId(state, id) {
      state.productDetailId = id;
    },
    cartItem(state, { id, by }) {
      if (state.cart[id]) {
        let newCount = (state.cart[id] += by);
        if (newCount <= 0) {
          // remove from cart entirely
          delete state.cart[id];
          return;
        }
        state.cart = {
          ...state.cart,
          [id]: newCount
        };
      } else {
        state.cart = {
          ...state.cart,
          [id]: 1
        };
      }
    },
    compare(state, compare) {
      state.compare = compare;
    },
    searchTerm(state, term) {
      state.searchTerm = term;
    },
    setProducts(state, products) {
      if (products) {
        state.products = products;
      }
    }
  },
  actions: {
    async refreshProducts({ commit }) {
      commit("toggleBusy");
      const products = await dichroism.getProducts();
      commit("setProducts", products);
      commit("toggleBusy");
    },
    async updateProduct({ commit, dispatch }, product) {
      commit("toggleBusy");
      await dichroism.updateProduct(product);
      dispatch("refreshProducts");
      commit("toggleBusy");
    },
    async createProduct({ commit, dispatch }, product) {
      commit("toggleBusy");
      await dichroism.createProduct(product);
      dispatch("refreshProducts");
      commit("toggleBusy");
    },
    async createPhotoSet({ commit }, file) {
      commit("toggleBusy");
      const photoSet = await dichroism.createPhoto(file);
      commit("toggleBusy");
      return photoSet;
    }
  }
});