product.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. const state = {
  2. products: [],
  3. loadingProducts: true,
  4. filteredProducts: [],
  5. productWithVariant: null
  6. }
  7. const getters = {
  8. /**
  9. *
  10. * @param {*} state
  11. */
  12. products(state) {
  13. return state.products
  14. },
  15. /**
  16. *
  17. * @param {*} state
  18. */
  19. visibleProducts(state) {
  20. return state.filteredProducts.length === 0 ? state.products : state.filteredProducts
  21. },
  22. /**
  23. *
  24. * @param {*} state
  25. */
  26. productWithVariant(state) {
  27. return state.productWithVariant
  28. },
  29. /**
  30. *
  31. * @param {*} state
  32. */
  33. showProductForm(state) {
  34. return !!state.productWithVariant
  35. },
  36. /**
  37. *
  38. * @param {*} state
  39. */
  40. loadingProducts(state) {
  41. return state.loadingProducts
  42. }
  43. }
  44. const mutations = {
  45. /**
  46. *
  47. * @param {*} state
  48. * @param {*} payload
  49. */
  50. setProducts(state, payload) {
  51. state.products = [...payload]
  52. },
  53. /**
  54. *
  55. * @param {*} state
  56. * @param {*} payload
  57. */
  58. setProductWithVariant(state, payload) {
  59. state.productWithVariant = payload
  60. },
  61. /**
  62. *
  63. * @param {*} state
  64. * @param {*} payload
  65. */
  66. setFilteredProducts(state, payload) {
  67. state.filteredProducts = [...payload]
  68. },
  69. /**
  70. *
  71. * @param {*} state
  72. * @param {*} payload
  73. */
  74. setLoadingProducts(state, payload) {
  75. state.loadingProducts = !!payload
  76. }
  77. }
  78. const actions = {
  79. /**
  80. *
  81. * @param {*} param0
  82. * @param {*} payload
  83. */
  84. initProducts({ commit }, payload) {
  85. commit('setProducts', payload)
  86. commit('setLoadingProducts', false)
  87. },
  88. /**
  89. *
  90. * @param {*} param0
  91. * @param {*} payload
  92. */
  93. selectProduct({ commit }, payload) {
  94. if (payload.variantCount > 1) {
  95. commit('setProductWithVariant', payload)
  96. return
  97. }
  98. commit('pushToCart', payload.variants[0])
  99. },
  100. /**
  101. *
  102. * @param {*} param0
  103. * @param {*} payload
  104. */
  105. filterProducts({ commit }, payload) {
  106. commit('setFilteredProducts', payload)
  107. },
  108. /**
  109. *
  110. * @param {*} param0
  111. * @param {*} payload
  112. */
  113. selectProductVariant({ commit, dispatch }, payload) {
  114. commit('setProductVariant', null)
  115. if (!payload) return
  116. dispatch('addToCart', payload)
  117. },
  118. /**
  119. *
  120. * @param {*} param0
  121. * @param {*} payload
  122. */
  123. showProductForm({ commit }) {
  124. commit('setShowProductForm', true)
  125. },
  126. /**
  127. *
  128. * @param {*} param0
  129. * @param {*} payload
  130. */
  131. hideProductForm({ commit }) {
  132. commit('setShowProductForm', false)
  133. },
  134. /**
  135. *
  136. * @param {*} param0
  137. * @param {*} payload
  138. */
  139. submitProduct({ commit, dispatch }, payload) {
  140. if (payload) {
  141. if (!payload.name || !payload.price) {
  142. dispatch('notify', 'Complete los campos para guardar')
  143. return
  144. }
  145. commit('setLoadingProducts', true)
  146. dispatch('createProduct', payload)
  147. }
  148. dispatch('hideProductForm')
  149. },
  150. /**
  151. *
  152. * @param {*} param0
  153. * @param {*} payload
  154. */
  155. addProduct({ commit }, payload) {
  156. commit('addProduct', payload)
  157. commit('setLoadingProducts', false)
  158. },
  159. /**
  160. *
  161. * @param {*} param0
  162. */
  163. resetProduct({ commit }) {
  164. commit('setLoadingProducts', true)
  165. commit('setProducts', [])
  166. commit('setFilteredProducts', [])
  167. }
  168. }
  169. export default {
  170. state,
  171. getters,
  172. mutations,
  173. actions
  174. }