1import Vue from 'vue'
2import Vuex from 'vuex'
3
4Vue.use(Vuex)
5
6export default new Vuex.Store({
7 // State là Trạng thái => hay còn gọi là Data (dữ liệu) của KHO chứa (store)
8 // Tất cả dữ liệu sẽ được khởi tạo trong thuộc tính `state`
9 state: {
10 products: [
11 {
12 id: 1,
13 name: 'Notebook Lenovo Ideapad 320 Intel® Core i5-7200u 8GB',
14 price: 2259,
15 image: require('@/assets/img/product/ipad4.png'),
16 stars: 5,
17 totalReviews: 230,
18 details: 'Mô tả chi tiết về sản phẩm Notebook Lenovo',
19 totalBuyed: 0
20 },
21 {
22 id: 2,
23 name: 'Notebook Dell Inspiron i15-3567-A30P Intel Core 7ª i5 4GB',
24 price: 2284,
25 image: require('@/assets/img/product/samsung-galaxy-tab.jpg'),
26 stars: 3.4,
27 totalReviews: 20,
28 details: 'Mô tả chi tiết về sản phẩm Notebook Dell Inspiron',
29 totalBuyed: 0
30 },
31 {
32 id: 3,
33 name: 'Notebook Samsung Essentials E21 Intel Celeron Dual Core',
34 price: 1490,
35 image: require('@/assets/img/product/iphone5.jpg'),
36 stars: 1,
37 totalReviews: 1,
38 details: 'Mô tả chi tiết về sản phẩm Notebook Samsung Essentials',
39 totalBuyed: 0
40 },
41 {
42 id: 4,
43 name: 'Notebook Samsung Expert X22 Intel Core 7 i5 8GB',
44 price: 2307,
45 image: require('@/assets/img/product/nokia-asha-311.jpg'),
46 stars: 4.4,
47 totalReviews: 340,
48 details: 'Mô tả chi tiết về sản phẩm Notebook Samsung Expert',
49 totalBuyed: 0
50 }
51 ],
52
53 cartProducts: [],
54 currentProduct: {}
55 },
56
57 // Getters: là các thuộc tính dùng để các component lấy dữ liệu từ KHO chứa (store) về
58 getters: {
59 getAllProducts: state => state.products,
60 getProductsInCart: state => state.cartProducts,
61 getCurrentProduct: state => state.currentProduct
62 },
63
64 // Mutations: là các thuộc tính dùng để thay đổi giá trị bên trong KHO chứa (store)
65 // thay đổi giá trị, nhưng chạy ĐỒNG BỘ (SYNC)
66 mutations: {
67 ADD_PRODUCT: (state, product) => {
68 let o = state.cartProducts.find(o => o.id === product.id) // Tìm sản phẩm trong Giỏ hàng theo ID
69 if (!o) { // Nếu sản phẩm chưa có trong Giỏ hàng => Thêm sản phẩm vào giỏ hàng
70 product.totalBuyed = 1
71 state.cartProducts.push(Object.assign({}, product))
72 } else { // Nếu sản phẩm đã có => cộng đồn số lượng mua lên 1 đơn vị
73 o.totalBuyed += 1
74 }
75 },
76 REMOVE_PRODUCT: (state, id) => {
77 let o = state.cartProducts.find(o => o.id === id) // Tìm sản phẩm trong Giỏ hàng theo ID
78 let index = state.cartProducts.indexOf(o)
79 state.cartProducts.splice(index, 1)
80 },
81 CURRENT_PRODUCT: (state, product) => {
82 state.currentProduct = product
83 }
84 },
85
86 // Actions: là các hàm (methods) dùng để thực hiện các hành động thay đổi giá trị bên trong KHO chứa (store)
87 // tương tự như Mutations, nhưng có thể chạy BẤT ĐỒNG BỘ (ASYNC)
88 actions: {
89 addProduct: (context, product) => {
90 context.commit('ADD_PRODUCT', product)
91 },
92 removeProduct: (context, id) => {
93 context.commit('REMOVE_PRODUCT', id)
94 },
95 currentProduct: (context, product) => {
96 context.commit('CURRENT_PRODUCT', product)
97 }
98 }
99})