|
1 | 1 | import shop from '../../api/shop'
|
| 2 | +import nested from './nested' |
2 | 3 |
|
3 | 4 | // initial state
|
4 | 5 | // shape: [{ id, quantity }]
|
@@ -29,20 +30,20 @@ const getters = {
|
29 | 30 |
|
30 | 31 | // actions
|
31 | 32 | const actions = {
|
32 |
| - checkout ({ commit, state }, products) { |
| 33 | + async checkout ({ commit, state }, products) { |
33 | 34 | const savedCartItems = [...state.items]
|
34 | 35 | commit('setCheckoutStatus', null)
|
35 | 36 | // empty cart
|
36 | 37 | commit('setCartItems', { items: [] })
|
37 |
| - shop.buyProducts( |
38 |
| - products, |
39 |
| - () => commit('setCheckoutStatus', 'successful'), |
40 |
| - () => { |
41 |
| - commit('setCheckoutStatus', 'failed') |
42 |
| - // rollback to the cart saved before sending the request |
43 |
| - commit('setCartItems', { items: savedCartItems }) |
44 |
| - } |
45 |
| - ) |
| 38 | + try { |
| 39 | + await shop.buyProducts(products) |
| 40 | + commit('setCheckoutStatus', 'successful') |
| 41 | + } catch (e) { |
| 42 | + console.error(e) |
| 43 | + commit('setCheckoutStatus', 'failed') |
| 44 | + // rollback to the cart saved before sending the request |
| 45 | + commit('setCartItems', { items: savedCartItems }) |
| 46 | + } |
46 | 47 | },
|
47 | 48 |
|
48 | 49 | addProductToCart ({ state, commit }, product) {
|
@@ -88,5 +89,8 @@ export default {
|
88 | 89 | state,
|
89 | 90 | getters,
|
90 | 91 | actions,
|
91 |
| - mutations |
| 92 | + mutations, |
| 93 | + modules: { |
| 94 | + nested |
| 95 | + } |
92 | 96 | }
|
0 commit comments