Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- YUI().use('node-core', 'promise',
- 'dom-base', 'node-base',
- 'io-base', 'json', 'array-extras',
- function(Y) {
- function checkDom() {
- return new Y.Promise(function(resolve, reject) {
- try {
- Y.one('#openRemaining').setHTML("--");
- Y.one('#artiRemaining').setHTML("--");
- return resolve();
- } catch (e) {
- return reject('Unable to setup DOM. ' + e);
- }
- });
- }
- function fetchJson() {
- return new Y.Promise(function(resolve, reject) {
- Y.io('/new-products/?format=json&&nocache=' + (new Date().getTime()), {
- on: {
- success: function(trans, config) {
- try {
- var parsedJson = Y.JSON.parse(config.responseText);
- return resolve(parsedJson);
- } catch (e) {
- return reject('Unable to parse JSON: ' + e);
- }
- },
- failure: function(trans, e, config) {
- return reject('Unable to make request: ' + e);
- }
- }
- });
- });
- }
- function makeCountDownTimers() {
- return new Y.Promise(function(resolve, reject) {
- var periodArr = [/* {
- name: 'earlyregistration', endDate: new Date("May 29, 2017 00:00:00 CST")
- }, {
- name: 'registrationclose', endDate: new Date("June 9, 2017 00:00:00 CST")
- }, */ {
- name: 'firstdrawing', endDate: new Date("April 1, 2018 00:00:00 CST")
- }, /*{
- name: 'seconddrawing', endDate: new Date("May 1, 2018 00:00:00 CST")
- }, */ {
- name: 'tournament', endDate: new Date("June 8, 2019 05:45:00 CST")
- }, {
- name: 'calcutta', endDate: new Date("June 6, 2019 06:30:00 CST")
- }];
- periodArr.forEach(function(cV) {
- var now = new Date();
- var timeLeft = (cV.endDate - now) / 1000;
- var days = "--";
- var hours = "--";
- var minutes = "--";
- var seconds = "--";
- if (timeLeft > 0) {
- days = Math.floor(timeLeft / 86400);
- hours = Math.floor((timeLeft - (days * 86400)) / 3600);
- minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600)) / 60);
- //seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60)));
- if (hours < "10") { hours = "0" + hours; }
- if (minutes < "10") { minutes = "0" + minutes; }
- if (seconds < "10") { seconds = "0" + seconds; }
- }
- try {
- Y.one('#' + cV.name + '_days').setHTML("<span class='red'>" + days + "</span><span class='sub'>D</span>");
- Y.one('#' + cV.name + '_hours').setHTML("<span class='red'>" + hours + "</span><span class='sub'>H</span>");
- Y.one('#' + cV.name + '_minutes').setHTML("<span class='red'>" + minutes + "</span><span class='sub'>M</span>");
- //Y.one('#' + cV.name + '_seconds').setHTML("<span class='red'>" + seconds + "</span><span class='sub'>S</span>");
- } catch (e) {
- return reject('Error parsing ' + cV.name + ' elements');
- }
- });
- return resolve();
- });
- }
- function parseStock(json) {
- return new Y.Promise(function(resolve, reject) {
- var openMax = 50;
- var artiMax = 50;
- var openQty = 0;
- var artiQty = 0;
- var openDate = new Date("March 1, 2019 07:59:59 CST");
- if(Date.now() > openDate)
- {
- var stockObj = Y.Array.find(json.items, function(cV) {
- return cV.urlId == "hewvop1g0fhef0c30llsacgxo67ec7";
- });
- if (stockObj === null) {
- return reject('stockObj never found in JSON response.');
- }
- stockObj.variants.forEach(function(cV) {
- switch (cV.attributes.Division) {
- case "Open":
- openQty += cV.qtyInStock
- break;
- case "Artificial":
- artiQty += cV.qtyInStock
- break;
- default:
- // Silently Fail
- }
- });
- openQty = openMax - (150 - openQty);
- artiQty = artiMax - (150 - artiQty);
- Y.one('#openRemaining').setHTML(openQty > 0 ? openQty : 'SOLD OUT');
- Y.one('#artiRemaining').setHTML(artiQty > 0 ? artiQty : 'SOLD OUT');
- }
- else
- {
- Y.one('#openRemaining').setHTML('SOLD OUT');
- Y.one('#artiRemaining').setHTML('SOLD OUT');
- }
- //Check out of stock
- //This doesn't seem to be affecting the drop down. I can do code injection in the header of all pages or I can do it just on the entry page but then I would have to parse the JSON a second time to get the current counts.
- var dropdown = document.getElementById("yui_3_17_2_1_1551322185265_458");
- let toDisable = "Open";
- for (var i = 0; i < dropdown.children.length; i++) {
- if (dropdown.children[i].value === toDisable) {
- dropdown.children[i].setAttribute("disabled", "disable");
- }
- }
- return resolve();
- });
- }
- function updateStock() {
- return fetchJson()
- .then(parseStock)
- .then(makeCountDownTimers)
- .then(setTimeout(updateStock, (1000 * 60))) // Refresh once a minute
- }
- Y.on('domready', function() {
- checkDom()
- .then(updateStock)
- .catch(function(e) {
- return console.log(e);
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement