|
| 1 | +import * as React from "react" |
| 2 | + |
| 3 | +interface GAEvent |
| 4 | +{ |
| 5 | + key: number |
| 6 | + timestamp: string |
| 7 | + name: string |
| 8 | + description: string |
| 9 | + snippet: string |
| 10 | +} |
| 11 | + |
| 12 | +interface Product |
| 13 | +{ |
| 14 | + id: number |
| 15 | + title: string |
| 16 | + brand: string |
| 17 | + category: string |
| 18 | + price: number |
| 19 | +} |
| 20 | + |
| 21 | +interface CartItem |
| 22 | +{ |
| 23 | + id: number |
| 24 | + product: Product |
| 25 | + variantId: string |
| 26 | + quantity: number |
| 27 | +} |
| 28 | + |
| 29 | +interface PostalAddress |
| 30 | +{ |
| 31 | + firstName: string |
| 32 | + lastName: string |
| 33 | + addressLine1: string |
| 34 | + addressLine2: string |
| 35 | + city: string |
| 36 | + provinceState: string |
| 37 | + zipPostalCode: string |
| 38 | + country: string |
| 39 | +} |
| 40 | + |
| 41 | +interface CheckoutState |
| 42 | +{ |
| 43 | + email: string |
| 44 | + shippingAddress: PostalAddress |
| 45 | + billingAddress: PostalAddress |
| 46 | + paymentMethod: string |
| 47 | + shippingMethod: string |
| 48 | + coupon: string |
| 49 | +} |
| 50 | + |
| 51 | +interface StoreContextValues { |
| 52 | + events: GAEvent[] |
| 53 | + cart: CartItem[] |
| 54 | + lastCart: CartItem[] |
| 55 | + isOpen: boolean |
| 56 | + checkoutState: CheckoutState |
| 57 | + onOpen(): void |
| 58 | + onClose(): void |
| 59 | + addEvent(name: string, description: string, snippet: string): void |
| 60 | + addVariantToCart(product: Product, variantId: string, |
| 61 | + quantity: number): void |
| 62 | + removeLineItem(id: number): void |
| 63 | + updateLineItem(id: number, quantity: number): void |
| 64 | + getCartSubtotal(): number |
| 65 | + updateCheckoutState(name: string, value: string|number): void |
| 66 | + updateShippingAddress(name: string, value: string|number): void |
| 67 | + updateBillingAddress(name: string, value: string|number): void |
| 68 | + emptyCart(): CartItem[] |
| 69 | +} |
| 70 | + |
| 71 | +const defaultValues: StoreContextValues = { |
| 72 | + cart: [], |
| 73 | + lastCart: [], |
| 74 | + events: [], |
| 75 | + isOpen: false, |
| 76 | + checkoutState: { |
| 77 | + email: '', |
| 78 | + shippingAddress: { |
| 79 | + firstName: '', |
| 80 | + lastName: '', |
| 81 | + addressLine1: '', |
| 82 | + addressLine2: '', |
| 83 | + city: '', |
| 84 | + provinceState: '', |
| 85 | + country: '', |
| 86 | + zipPostalCode: '' |
| 87 | + }, |
| 88 | + shippingMethod: '', |
| 89 | + billingAddress: { |
| 90 | + firstName: '', |
| 91 | + lastName: '', |
| 92 | + addressLine1: '', |
| 93 | + addressLine2: '', |
| 94 | + city: '', |
| 95 | + provinceState: '', |
| 96 | + country: '', |
| 97 | + zipPostalCode: '' |
| 98 | + }, |
| 99 | + coupon: '', |
| 100 | + paymentMethod: '' |
| 101 | + }, |
| 102 | + onOpen: () => { |
| 103 | + }, |
| 104 | + onClose: () => { |
| 105 | + }, |
| 106 | + addEvent: () => { |
| 107 | + }, |
| 108 | + addVariantToCart: () => { |
| 109 | + }, |
| 110 | + removeLineItem: () => { |
| 111 | + }, |
| 112 | + updateLineItem: () => { |
| 113 | + }, |
| 114 | + getCartSubtotal: () => { |
| 115 | + return 0 |
| 116 | + }, |
| 117 | + updateCheckoutState: () => { |
| 118 | + }, |
| 119 | + updateShippingAddress: () => { |
| 120 | + }, |
| 121 | + updateBillingAddress: () => { |
| 122 | + }, |
| 123 | + emptyCart: () => { |
| 124 | + return [] |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +export const StoreContext = React.createContext(defaultValues) |
| 130 | + |
| 131 | +export const StoreProvider = ({children}) => { |
| 132 | + const [cart, setCart] = React.useState(defaultValues.cart) |
| 133 | + const [lastCart, setLastCart] = React.useState(defaultValues.lastCart) |
| 134 | + |
| 135 | + const [checkoutState, setCheckoutState] = React.useState(defaultValues.checkoutState) |
| 136 | + const [events, setEvents] = React.useState(defaultValues.events) |
| 137 | + |
| 138 | + const addEvent = (name, description, snippet) => { |
| 139 | + const key = events.length |
| 140 | + const timestamp = new Intl.DateTimeFormat('default', { |
| 141 | + hour: 'numeric', |
| 142 | + minute: 'numeric', |
| 143 | + second: 'numeric', |
| 144 | + hour12: false, |
| 145 | + }).format(Date.now()) |
| 146 | + |
| 147 | + const newEvents = [{ |
| 148 | + key, |
| 149 | + timestamp, |
| 150 | + name, |
| 151 | + description, |
| 152 | + snippet |
| 153 | + }].concat(events); |
| 154 | + setEvents(newEvents); |
| 155 | + } |
| 156 | + const addVariantToCart = (product, variantId, quantity) => { |
| 157 | + const id = cart.length; |
| 158 | + const cartItems = [ |
| 159 | + { |
| 160 | + id, |
| 161 | + product, |
| 162 | + variantId, |
| 163 | + quantity: parseInt(quantity, 10), |
| 164 | + }, |
| 165 | + ] |
| 166 | + const newCart = cart.concat(cartItems) |
| 167 | + setCart(newCart) |
| 168 | + } |
| 169 | + |
| 170 | + const removeLineItem = (id) => { |
| 171 | + const newCart = cart.filter(item => item.id !== id); |
| 172 | + setCart(newCart) |
| 173 | + } |
| 174 | + |
| 175 | + const updateLineItem = (id, quantity) => { |
| 176 | + const newCart = [ ...cart] |
| 177 | + const item = newCart.find(item => item.id === id); |
| 178 | + if( item ) |
| 179 | + { |
| 180 | + item.quantity = parseInt(quantity, 10); |
| 181 | + } |
| 182 | + setCart(newCart); |
| 183 | + } |
| 184 | + |
| 185 | + const getCartSubtotal = () => { |
| 186 | + return cart.reduce((sum,x) => sum + Number(x.product.price) * x.quantity, 0); |
| 187 | + } |
| 188 | + |
| 189 | + const updateCheckoutState = (name, value) => |
| 190 | + { |
| 191 | + const newCheckoutState = { ...checkoutState }; |
| 192 | + newCheckoutState[name] = value; |
| 193 | + setCheckoutState(newCheckoutState); |
| 194 | + } |
| 195 | + |
| 196 | + const updateShippingAddress = (name, value) => |
| 197 | + { |
| 198 | + const newCheckoutState = { ...checkoutState }; |
| 199 | + newCheckoutState.shippingAddress[name] = value; |
| 200 | + setCheckoutState(newCheckoutState); |
| 201 | + } |
| 202 | + |
| 203 | + const updateBillingAddress = (name, value) => |
| 204 | + { |
| 205 | + const newCheckoutState = { ...checkoutState }; |
| 206 | + newCheckoutState.billingAddress[name] = value; |
| 207 | + setCheckoutState(newCheckoutState); |
| 208 | + } |
| 209 | + |
| 210 | + const emptyCart = () => |
| 211 | + { |
| 212 | + const cartSnapshot = [ ...cart ]; |
| 213 | + setCart([]); |
| 214 | + setLastCart(cart) |
| 215 | + return cartSnapshot; |
| 216 | + } |
| 217 | + |
| 218 | + return ( |
| 219 | + <StoreContext.Provider |
| 220 | + value={{ |
| 221 | + ...defaultValues, |
| 222 | + addEvent, |
| 223 | + addVariantToCart, |
| 224 | + removeLineItem, |
| 225 | + updateLineItem, |
| 226 | + getCartSubtotal, |
| 227 | + updateCheckoutState, |
| 228 | + updateShippingAddress, |
| 229 | + updateBillingAddress, |
| 230 | + emptyCart, |
| 231 | + cart, |
| 232 | + checkoutState, |
| 233 | + lastCart, |
| 234 | + events |
| 235 | + }} |
| 236 | + > |
| 237 | + {children} |
| 238 | + </StoreContext.Provider> |
| 239 | + ) |
| 240 | +} |
0 commit comments