-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwegs.js
216 lines (189 loc) · 5.62 KB
/
wegs.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
/*
Copyright 2018 Connor Dimaio & Kyle Bansavage
*/
//require('./node_modules/chartist-plugin-legend/chartist-plugin-legend.js');
//create some important vars
var chart;
var authKey = "Bearer ";
var subKey = "7a2da79699e74dd0af5b8158f6134fbd";
var BeerVelocities = {}; // Score for each beer type
var StoreNames = []; // The names of stores seen
var BeerSkus = []; // The Skus of beers seen
var StoreNum = [];
/*
325152 //rolling rock
127425 //miller high life
129182//miller lite
90185 //genny cream ale
232673 //keystone light
282162 //labat blue
173308 //pbr
274681 //natty light
166446 //budweiser
*/
$('html').css('cursor','progress');
$( document ).ready(function() {
//get keys for weggies api
var settings = {
"async": true,
"crossDomain": true,
"url": "https://login.microsoftonline.com/1318d57f-757b-45b3-b1b0-9b3c3842774f/oauth2/token",
"method": "POST",
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"Cache-Control": "no-cache"
},
"data": {
"client_id": "24960d97-4fbe-433d-ab8a-efeb89aa524e",
"grant_type": "client_credentials",
"resource": "https://wegmans-es.azure-api.net",
"client_secret": "A8N7VeeCdFD5N4OxeQT1gFaXNStrxieEplYl3SYdxTs="
}
};
$.ajax(settings).done(function (response) {
authKey += response.access_token;
//console.log(authKey);
});
$('html').css('cursor','auto');
});
//on click change btn to grey
$(".logo").click(function() {
$(this).css("filter","grayscale(1)");
//change text here
});
async function run(beerID){
switch (beerID) {
case 112985: //bud
$('#budkey').text(BeerSkus.length+1);
break;
case 90188:
$('#coorskey').text(BeerSkus.length+1);
break;
case 237942:
$('#gennykey').text(BeerSkus.length+1);
break;
case 127425:
$('#millerkey').text(BeerSkus.length+1);
break;
case 325152:
$('#rollingkey').text(BeerSkus.length+1);
break;
case 173308:
$('#pbrkey').text(BeerSkus.length+1);
break;
}
var zoneName = document.getElementById("zoneName").value;
//console.log(zoneName);
//console.log("Store Length" + StoreNum.length);
if(StoreNum.length > 0 && !BeerSkus.includes(beerID)) {
BeerSkus.push(beerID);
for(var num in StoreNum) {
//console.log(StoreNum[num]);
getAvailabilityAtStore(StoreNum[num], beerID);
}
}
else if( StoreNum.length == 0 && !BeerSkus.includes(beerID)) {
BeerSkus.push(beerID);
//console.log("BEER SKUS: " + BeerSkus.length);
await sleep(500);
$.ajax({
url: "https://wegmans-es.azure-api.net/locationpublic/location/stores?zoneName=" + zoneName,
//url: "https://wegmans-es.azure-api.net/productpublic/productavailability/700632/stores",
beforeSend: setHeaders,
type: "GET",
// Request body
data: "{body}"
})
.done(async function(data) {
for(i = 0; i < data.length; i++) {
//console.log(data[i].Name);
if(!StoreNames.includes(data[i].Name)) {StoreNames.push(data[i].Name) }; //adds store names
if(!StoreNum.includes(data[i].StoreNumber)) StoreNum.push(data[i].StoreNumber);
getAvailabilityAtStore(data[i].StoreNumber, beerID);
}
})
.fail(function() {
console.log("error");
});
}
console.log(BeerVelocities);
//console.log("names"); console.log(StoreNames);
console.log(BeerSkus);
console.log(StoreNum);
// graphResults(BeerVelocities);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function getAvailabilityAtStore(storeNum,beerID) {
$.ajax({
url: "https://wegmans-es.azure-api.net/productpublic/productavailability/" + beerID + "/" + storeNum,
//url: "https://wegmans-es.azure-api.net/productpublic/productavailability/700632/stores",
beforeSend: setHeaders,
type: "GET",
// Request body
data: "{body}"
})
.done(async function(availability) {
//console.log("availability");
if(BeerVelocities[beerID] == null) BeerVelocities[beerID] = [];
await sleep(500);
BeerVelocities[beerID].push({key: storeNum, value: availability[0].Velocity});
createChart(); //generates the chart
})
.fail(function() {
console.log("error");
});
}
function setHeaders(xhrObj) {
xhrObj.setRequestHeader("Location-Subscription-Key", subKey);
xhrObj.setRequestHeader("Product-Subscription-Key",subKey);
xhrObj.setRequestHeader("Authorization",authKey);
xhrObj.setRequestHeader("Availability-Subscription-Key", subKey);
}
function zoneChanged() {
BeerVelocities = {};
StoreNames = [];
BeerSkus = [];
StoreNum = [];
//console.log("DATA CLEARED!")
var event = new Event("DOMContentLoaded");
document.dispatchEvent(event);
$(".logo").css("filter","grayscale(0)");
$(".keynum").text("");
}
document.addEventListener('DOMContentLoaded',function(){
createChart();
});
function createChart(){
console.log(BeerVelocities);
velocities = [];
for (key in BeerVelocities) {
v = [];
for (k in BeerVelocities[key]) {
v[StoreNum.indexOf(BeerVelocities[key][k].key)] = BeerVelocities[key][k];
}
velocities.push(v);
}
console.log(velocities);
var data = {
labels: StoreNames,
series: velocities
};
var options = {
seriesBarDistance: 10,
horizontalBars: true,
AxisY: 50,
};
var responsiveOptions = [
['screen and (max-width: 640px)', {
seriesBarDistance: 15,
axisX: {
labelInterpolationFnc: function (value) {
return value[0];
}
}
}]
];
new Chartist.Bar('.ct-chart', data, options, responsiveOptions);
}