cart.js 867 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as types from '../mutation-types'
  2. // initial state
  3. // shape: [{ id, quantity }]
  4. const state = {
  5. added: [],
  6. checkoutStatus: null
  7. }
  8. // mutations
  9. const mutations = {
  10. [types.ADD_TO_CART] (state, { id }) {
  11. state.lastCheckout = null
  12. const record = state.added.find(p => p.id === id)
  13. if (!record) {
  14. state.added.push({
  15. id,
  16. quantity: 1
  17. })
  18. } else {
  19. record.quantity++
  20. }
  21. },
  22. [types.CHECKOUT_REQUEST] (state) {
  23. // clear cart
  24. state.added = []
  25. state.checkoutStatus = null
  26. },
  27. [types.CHECKOUT_SUCCESS] (state) {
  28. state.checkoutStatus = 'successful'
  29. },
  30. [types.CHECKOUT_FAILURE] (state, { savedCartItems }) {
  31. // rollback to the cart saved before sending the request
  32. state.added = savedCartItems
  33. state.checkoutStatus = 'failed'
  34. }
  35. }
  36. export default {
  37. state,
  38. mutations
  39. }