1
0

Cart.vue 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <template>
  2. <div class="cart">
  3. <h2>Your Cart</h2>
  4. <p v-show="!cart.added.length"><i>Please add some products to cart.</i></p>
  5. <ul>
  6. <li v-for="p in products">
  7. {{ p.title }} - {{ p.price | currency }} x {{ p.quantity }}
  8. </li>
  9. </ul>
  10. <p>Total: {{ total | currency }}</p>
  11. <p><button :disabled="!cart.added.length" @click="checkout">Checkout</button></p>
  12. <p v-show="cart.lastCheckout">Checkout {{ cart.lastCheckout }}.</p>
  13. </div>
  14. </template>
  15. <script>
  16. import vuex from '../vuex'
  17. const { checkout } = vuex.actions
  18. export default {
  19. data () {
  20. return {
  21. cart: vuex.get('cart')
  22. }
  23. },
  24. computed: {
  25. products () {
  26. return this.cart.added.map(({ id, quantity }) => {
  27. const product = vuex.state.products.find(p => p.id === id)
  28. return {
  29. title: product.title,
  30. price: product.price,
  31. quantity
  32. }
  33. })
  34. },
  35. total () {
  36. return this.products.reduce((total, p) => {
  37. return total + p.price * p.quantity
  38. }, 0)
  39. }
  40. },
  41. methods: {
  42. checkout
  43. }
  44. }
  45. </script>