-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
153 lines (136 loc) · 3.99 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { createStore, applyMiddleware } from 'redux'
import thunkMiddleware from 'redux-thunk'
import axios from 'axios'
import { client, generateUUID } from './lib/moltin'
const initialState = {
cartId: null,
products: [],
cartItems: [],
cart: {},
// `taxState` is simply the (geographic) state value so we can maintain taxe prices
// when the user navigates different pages after selecting an address during checkout
taxState: null,
isBillingSelectDisabled: false,
}
export const actionTypes = {
LOAD_PRODUCTS: 'LOAD_PRODUCTS',
SET_PRODUCTS: 'SET_PRODUCTS',
SET_CART_ITEMS: 'SET_CART_ITEMS',
SET_CART: 'SET_CART',
SET_TAX_STATE: 'SET_TAX_STATE',
SET_IS_BILLING_SELECT_DISABLED: 'SET_IS_BILLING_SELECT_DISABLED',
}
export const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.SET_PRODUCTS:
return {
...state,
products: action.products,
}
case actionTypes.SET_CART_ITEMS:
return {
...state,
cartItems: action.items,
}
case actionTypes.SET_CART:
return {
...state,
cart: action.cart,
}
case actionTypes.SET_TAX_STATE:
return {
...state,
taxState: action.taxState,
}
case actionTypes.SET_IS_BILLING_SELECT_DISABLED:
return {
...state,
isBillingSelectDisabled: action.isBillingSelectDisabled,
}
default:
return state
}
}
// Quick helpers to set some state
export const setProducts = products => ({ type: actionTypes.SET_PRODUCTS, products })
export const setCart = cart => ({ type: actionTypes.SET_CART, cart })
export const setCartItems = items => ({ type: actionTypes.SET_CART_ITEMS, items })
export const updateTaxState = taxState => ({ type: actionTypes.SET_TAX_STATE, taxState })
export const updateIsBillingSelectDisabled = disabled => ({
type: actionTypes.SET_IS_BILLING_SELECT_DISABLED,
disabled,
})
export const loadProducts = () => async (dispatch) => {
const products = await client.get('products?include=main_images')
const data = products.data.map((product) => {
const mainImage = products.included.main_images.find(image => (
image.id === product.relationships.main_image.data.id
))
return {
...product,
main_image: mainImage,
}
})
dispatch(setProducts(data))
}
export const loadCart = cartId => async (dispatch) => {
const { data } = await client.get(`carts/${cartId}`)
dispatch(setCart(data))
}
export const addToCart = (cartId, productId, taxCode) => async (dispatch) => {
const { data } = await client.post(`carts/${cartId}/items`, {
id: productId,
type: 'cart_item',
quantity: 1,
tax_code: taxCode,
})
dispatch(setCartItems(data))
loadCart(cartId)
}
export const removeFromCart = (cartId, itemId) => async (dispatch) => {
const { data } = await client.delete(`carts/${cartId}/items/${itemId}`)
dispatch(setCartItems(data))
loadCart(cartId)
}
export const loadCartItems = cartId => async (dispatch) => {
const { data } = await client.get(`carts/${cartId}/items`)
dispatch(setCartItems(data))
loadCart(cartId)
}
export const updateTaxes = (
city, jurisdiction, country, zip, nexusAddresses, cartId, cartItems,
) => async () => {
// Calculate based on new jurisdiction
const taxJarResponse = await axios.post('/api/calculate', {
city,
jurisdiction,
country,
zip,
nexusAddresses,
cartItems,
}).catch((e) => {
console.log('e', e)
})
// Delete existing taxes since we are updating
await axios.delete('/api/tax', {
data: { cartId },
}).catch((e) => {
console.log('e', e)
})
// Apply new taxes
await axios.post('/api/tax', {
cartId,
cartItems,
taxJarResponse,
}).catch((e) => {
console.log('e', e)
})
}
export function initializeStore(state = initialState) {
const cartId = state.cartId || generateUUID()
const initialStoreState = {
...state,
cartId,
}
return createStore(reducer, initialStoreState, applyMiddleware(thunkMiddleware))
}