-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrontend.js
153 lines (136 loc) · 5.42 KB
/
frontend.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
function createElement(tagName, attrs) {
let elem = document.createElement(tagName);
Object.assign(elem, attrs);
if ('class' in attrs) {
elem.setAttribute('class', attrs['class']);
}
return elem
}
function updateTotal(rateId) {
function doUpdate() {
// TODO: Update this to make the request to add the sku to the order
// Also need to remove other rates if one is already selected
// i.e. ecommerceUpdateCartItem -> ecommerceAddToCart -> ecommerceRecalcEstimations
let rates = document.getElementsByClassName('shipping-method-radio');
let newRate = document.getElementById(rateId);
const requests = [];
for (const rate of rates) {
requests.push({
operationName: 'CheckoutAddShipping',
query: `
mutation CheckoutAddShipping($sku: String!, $count: Int!) {
ecommerceUpdateCartItem(sku: $sku, count: $count) {
ok
__typename
}
}
`,
variables: {sku: rate.sku, count: 0}
})
}
requests.push({
operationName: 'CheckoutAddShipping',
query: `
mutation CheckoutAddShipping($sku: String!, $count: Int!) {
ecommerceAddToCart(sku: $sku, count: $count) {
ok
__typename
}
}
`,
variables: {sku: newRate.sku, count: 1}
});
requests.push({
operationName: 'CheckoutAddShipping',
query: `
mutation CheckoutAddShipping {
ecommerceRecalcEstimations() {
ok
__typename
}
}
`,
variables: {}
});
let request = new XMLHttpRequest();
request.open('POST', `${window.location.origin}/.wf_graphql/apollo`);
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('X-Wf-CSRF', getCookie()['wf-csrf']);
request.send(JSON.stringify(requests));
// let subTotalPrice = document.getElementById('subtotal-price');
// let ratePrice = document.getElementById('rate-price');
// let totalPrice = document.getElementById('total-price');
// ratePrice.innerText = `$ ${cost}`;
// let subTotal = parseFloat(subTotalPrice.innerText.trim().replace('$', ''));
// totalPrice.innerText = `$ ${parseFloat(cost) + subTotal}`;
}
return doUpdate
}
function getCookie() {
return document.cookie.split(';').reduce((cookies, cookie) => {
const [ name, value ] = cookie.split('=').map(c => c.trim());
cookies[name] = value;
return cookies;
}, {});
}
document.getElementById('calculate-shipping').onclick = function() {
// text boxes
let name = document.getElementById('name');
let street1 = document.getElementById('street1');
let street2 = document.getElementById('street2');
let city = document.getElementById('city');
let state = document.getElementById('state');
let zip = document.getElementById('zip');
// arrays?
let shippingMethods = document.getElementById('shipping-methods');
let request = new XMLHttpRequest();
request.open('POST', 'https://us-central1-nots-backend-dev.cloudfunctions.net/widgets/shipping/estimate');
request.setRequestHeader('Content-Type', 'application/json');
request.onload = function() {
if (request.status === 200) {
let data = JSON.parse(request.response)
// Set the updated address
street1.value = data.address.addressLine1;
street2.value = data.address.addressLine2;
city.value = data.address.city;
state.value = data.address.state;
zip.value = data.address.zipCode;
// Set the shipping rates
shippingMethods.innerHTML = '';
data.rates.map(rate => {
let wrapper = createElement('div', {class: 'shipping-method-container'});
let input = createElement(
'input',
{
type: 'radio',
id: rate.rateId,
name: 'shippingMethod',
class: 'shipping-method-radio',
sku: rate.sku,
cost: rate.cost
}
);
input.onclick = updateTotal(rate.rateId);
wrapper.appendChild(input);
let label = createElement('label', {for: rate.rateId});
label.innerText = `${rate.provider} | $ ${rate.cost} | ~${rate.estimatedDays} days`;
wrapper.appendChild(label);
shippingMethods.appendChild(wrapper);
});
} else {
console.log(`${request.status} - ${request.response}`);
}
}
// TODO: how do we add weight?
request.send(JSON.stringify({
weight: 1,
shipTo: {
name: name.value || name.innerText,
addressLine1: street1.value || street1.innerText,
addressLine2: street2.value || street2.innerText,
city: city.value || city.innerText,
state: state.value || state.innerText,
zipCode: zip.value || zip.innerText,
},
}));
}