-
Notifications
You must be signed in to change notification settings - Fork 3
/
cart.js
242 lines (206 loc) · 5.79 KB
/
cart.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import client from './client'
import { checkoutId } from './checkout'
import {
htmlToElements,
formatCurrency,
bannerMessage,
debounce
} from './helpers'
const cartLoading = document.getElementById('ss-cart-loading')
const noItemsMessage = document.getElementById('ss-cart-no-items')
const cartView = document.getElementById('ss-cart-view')
const cartHolder = document.getElementById('ss-cart')
/**
* Set the cart count for the item
*/
const setCartCount = () => {
const countTarget = document.querySelector('[data-ss-cart-count]')
if (countTarget == null) {
return
}
client.checkout
.fetch(checkoutId)
.then(({ lineItems }) => {
let count = 0
lineItems.forEach(item => (count = count + item.quantity))
countTarget.innerHTML = count
})
.catch(err => {
// Handle Errors here.
})
}
/**
* Hides the cart overview.
* Shows the message that nothing is in the basket.
*/
const hideCartOverview = () => {
noItemsMessage.classList.remove('hidden')
cartView.classList.add('hidden')
}
/**
* Shows the cart overview.
* Hides the message that nothing is in the basket.
*/
const showCartOverview = async (lineItems, price, checkoutLink) => {
cartView.classList.remove('hidden')
noItemsMessage.classList.add('hidden')
// Table
const tableBody = document.querySelector('#ss-cart-view table tbody')
// Append line item elements
await lineItems.forEach(({ id, variant, title, quantity }) => {
const price = formatCurrency(variant.price)
const subtotal = formatCurrency(quantity * variant.price)
let html = `<tr data-ss-variant-id="${id}">
<td class="px-6 py-4 whitespace-nowrap" colspan="2">
<div class="flex items-center">
<div class="mr-3">
<picture class="aspect-w-1 aspect-h-1 overflow-hidden block relative w-20 h-20">`
if (variant.image) {
html =
html +
`<img src="${variant.image.src}" class="pin-0 absolute object-cover" />`
}
html =
html +
`</picture>
</div>
<div>
<span class="block font-semibold">${title}</span>
<span>${variant.title}</span>
</div>
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
${price}
</td>
<td class="px-6 py-4 whitespace-nowrap">
<input type="number" name="qty" min="1" class="border w-20 p-1" value="${quantity}"/>
</td>
<td class="px-6 py-4 whitespace-nowrap">
${subtotal}
</td>
<td class="px-6 py-4 whitespace-nowrap">
<a href="#" data-ss-delete class="text-red-600"><svg class="w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" /></svg></a>
</td>
</tr>`
const elements = htmlToElements(html)
elements.forEach(el => {
tableBody.appendChild(el)
})
})
// Init delete/qty inputs we just appended
initCartActions()
// Set subtotal value
setCartSubtotal(price.amount)
// Checkout Link
const checkoutTag = document.getElementById('ss-checkout-link')
checkoutTag.setAttribute('href', checkoutLink)
}
/**
* Set the subtotal amount on the page.
* Giving the user an overview of total basket.
*
* @param amount
*/
const setCartSubtotal = amount => {
const subtotalEl = document.querySelector('[data-ss-subtotal]')
if (subtotalEl != null) {
subtotalEl.innerHTML = formatCurrency(amount)
}
}
/**
* Initialise the cart actions for updating qty
* and deleting a product from the basket.
* These are reloaded on any item change
*/
const initCartActions = () => {
const tableRows = document.querySelectorAll('#ss-cart-view table tbody tr')
tableRows.forEach(row => {
const btnEls = row.querySelector('[data-ss-delete]')
const qtyEls = row.querySelector('input[name=qty]')
btnEls.addEventListener('click', e => {
e.preventDefault()
deleteRowFromStorefront(row)
})
qtyEls.addEventListener('change', e => {
e.preventDefault()
updateQtyInStorefront(row, e.target.value)
})
})
}
/**
* Get ride of a row from the cart page and then
* delete it from storefront.
*
* @param row
*/
const deleteRowFromStorefront = row => {
const id = row.getAttribute('data-ss-variant-id')
const items = []
items.push(id)
client.checkout
.removeLineItems(checkoutId, items)
.then(({ lineItems, subtotalPriceV2 }) => {
setCartCount(lineItems)
setCartSubtotal(subtotalPriceV2.amount)
bannerMessage(htmlToElements('<p>Item removed successfully</p>'))
if (lineItems.length === 0) {
noItemsMessage.classList.remove('hidden')
cartView.classList.add('hidden')
}
row.remove()
})
}
/**
* Update the quantity in Storefront + the cart
*
* @param row
* @param qty
*/
const updateQtyInStorefront = debounce((row, qty) => {
const id = row.getAttribute('data-ss-variant-id')
const items = [
{
id: id,
quantity: parseInt(qty)
}
]
client.checkout
.updateLineItems(checkoutId, items)
.then(({ lineItems, subtotalPriceV2 }) => {
setCartCount(lineItems)
setCartSubtotal(subtotalPriceV2.amount)
})
.catch(err => {})
}, 500)
/**
* Cart initialisation script which groups all the
* functions above.
*/
const cart = () => {
if (cartHolder == null && cartView == null) {
return
}
// Fetch the cart
client.checkout
.fetch(checkoutId)
.then(checkout => {
const { lineItems, subtotalPriceV2, webUrl } = checkout
cartLoading.classList.add('hidden')
if (lineItems.length === 0) {
hideCartOverview()
return
}
// Show Elements
showCartOverview(lineItems, subtotalPriceV2, webUrl)
})
.catch(err => {})
}
export {
showCartOverview,
hideCartOverview,
initCartActions,
setCartSubtotal,
setCartCount
}
export default cart