summaryrefslogtreecommitdiff
path: root/iridescence/src/store/index.js
blob: b324bd1b976d03b0312771722301a9491baf11be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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 => {
          let haystack = [
            item.id,
            item.name,
            item.description,
            item.category
          ].join("");
          return haystack
            .toLowerCase()
            .includes(state.searchTerm.toLowerCase());
        })
        .sort(state.compare);
    },
    cartTotal(state) {
      let cents = state.products
        .filter(p => state.cart[p.id])
        .map(p => p.cents * state.cart[p.id])
        .reduce((acc, cur) => acc + cur, 0);
      return "$ " + (cents / 100).toFixed(2);
    }
  },
  mutations: {
    toggleBusy(state) {
      state.busy = !state.busy;
    },
    productDetailId(state, id) {
      state.productDetailId = id;
    },
    cartItem(state, { id, by }) {
      let newCount = 1;
      if (state.cart[id]) {
        newCount = state.cart[id] += by;
        //if (newCount <= 0) {
        //  // remove from cart entirely
        //  delete state.cart[id];
        //  return;
        //}
        //state.cart = {
        //  ...state.cart,
        //  [id]: newCount
      }

      let cart = {
        ...state.cart
      };

      if (newCount) {
        cart[id] = newCount;
      } else {
        // remove entirely
        delete cart[id];
      }
      state.cart = cart;
    },
    removeItemFromCart(state, id) {
      if (state.cart[id]) {
        let cart = {
          ...state.cart
        };
        delete cart[id];
        state.cart = cart;
      }
    },
    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;
    }
  }
});