Advertisement
StreckerCM

ttc.js with openDate

Feb 27th, 2019
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. YUI().use('node-core', 'promise',
  2.   'dom-base', 'node-base',
  3.   'io-base', 'json', 'array-extras',
  4.   function(Y) {
  5.     function checkDom() {
  6.       return new Y.Promise(function(resolve, reject) {
  7.         try {
  8.           Y.one('#openRemaining').setHTML("--");
  9.           Y.one('#artiRemaining').setHTML("--");
  10.           return resolve();
  11.         } catch (e) {
  12.           return reject('Unable to setup DOM. ' + e);
  13.         }
  14.       });
  15.     }
  16.  
  17.     function fetchJson() {
  18.       return new Y.Promise(function(resolve, reject) {
  19.         Y.io('/new-products/?format=json&&nocache=' + (new Date().getTime()), {
  20.           on: {
  21.             success: function(trans, config) {
  22.               try {
  23.                 var parsedJson = Y.JSON.parse(config.responseText);
  24.                 return resolve(parsedJson);
  25.               } catch (e) {
  26.                 return reject('Unable to parse JSON: ' + e);
  27.               }
  28.             },
  29.             failure: function(trans, e, config) {
  30.               return reject('Unable to make request: ' + e);
  31.             }
  32.           }
  33.         });
  34.       });
  35.     }
  36.  
  37.     function makeCountDownTimers() {
  38.       return new Y.Promise(function(resolve, reject) {
  39.         var periodArr = [/* {
  40.           name: 'earlyregistration', endDate: new Date("May 29, 2017 00:00:00 CST")
  41.         }, {
  42.           name: 'registrationclose', endDate: new Date("June 9, 2017 00:00:00 CST")
  43.         }, */ {
  44.           name: 'firstdrawing', endDate: new Date("April 1, 2018 00:00:00 CST")
  45.         }, /*{
  46.           name: 'seconddrawing', endDate: new Date("May 1, 2018 00:00:00 CST")
  47.         }, */ {
  48.           name: 'tournament', endDate: new Date("June 8, 2019 05:45:00 CST")
  49.         }, {
  50.           name: 'calcutta', endDate: new Date("June 6, 2019 06:30:00 CST")
  51.         }];
  52.        
  53.  
  54.         periodArr.forEach(function(cV) {
  55.           var now = new Date();
  56.            
  57.           var timeLeft = (cV.endDate - now) / 1000;
  58.          
  59.           var days = "--";
  60.           var hours = "--";
  61.           var minutes = "--";
  62.           var seconds = "--";
  63.          
  64.           if (timeLeft > 0) {
  65.             days = Math.floor(timeLeft / 86400);
  66.             hours = Math.floor((timeLeft - (days * 86400)) / 3600);
  67.             minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600)) / 60);
  68.             //seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60)));
  69.            
  70.             if (hours < "10") { hours = "0" + hours; }
  71.             if (minutes < "10") { minutes = "0" + minutes; }
  72.             if (seconds < "10") { seconds = "0" + seconds; }
  73.           }
  74.            
  75.             try {
  76.                 Y.one('#' + cV.name + '_days').setHTML("<span class='red'>" + days + "</span><span class='sub'>D</span>");
  77.                 Y.one('#' + cV.name + '_hours').setHTML("<span class='red'>" + hours + "</span><span class='sub'>H</span>");
  78.                 Y.one('#' + cV.name + '_minutes').setHTML("<span class='red'>" + minutes + "</span><span class='sub'>M</span>");
  79.                 //Y.one('#' + cV.name + '_seconds').setHTML("<span class='red'>" + seconds + "</span><span class='sub'>S</span>");
  80.             } catch (e) {
  81.                 return reject('Error parsing ' + cV.name + ' elements');
  82.             }      
  83.         });
  84.        
  85.         return resolve();
  86.       });
  87.     }
  88.  
  89.     function parseStock(json) {
  90.       return new Y.Promise(function(resolve, reject) {
  91.         var openMax = 50;
  92.         var artiMax = 50;
  93.  
  94.         var openQty = 0;
  95.         var artiQty = 0;
  96.        
  97.         var openDate = new Date("March 1, 2019 07:59:59 CST");
  98.              
  99.         if(Date.now() > openDate)
  100.         {
  101.             var stockObj = Y.Array.find(json.items, function(cV) {
  102.               return cV.urlId == "hewvop1g0fhef0c30llsacgxo67ec7";
  103.             });
  104.  
  105.             if (stockObj === null) {
  106.               return reject('stockObj never found in JSON response.');
  107.             }
  108.  
  109.             stockObj.variants.forEach(function(cV) {
  110.               switch (cV.attributes.Division) {
  111.                 case "Open":
  112.                   openQty += cV.qtyInStock
  113.                   break;
  114.                 case "Artificial":
  115.                   artiQty += cV.qtyInStock
  116.                   break;
  117.                 default:
  118.                   // Silently Fail
  119.               }
  120.             });
  121.  
  122.             openQty = openMax - (150 - openQty);
  123.             artiQty = artiMax - (150 - artiQty);
  124.  
  125.             Y.one('#openRemaining').setHTML(openQty > 0 ? openQty : 'SOLD OUT');
  126.             Y.one('#artiRemaining').setHTML(artiQty > 0 ? artiQty : 'SOLD OUT');
  127.         }
  128.         else
  129.         {
  130.             Y.one('#openRemaining').setHTML('SOLD OUT');
  131.             Y.one('#artiRemaining').setHTML('SOLD OUT');
  132.         }
  133.        
  134.         //Check out of stock
  135.         //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.
  136.         var dropdown = document.getElementById("yui_3_17_2_1_1551322185265_458");
  137.        
  138.         let toDisable = "Open";
  139.  
  140.             for (var i = 0; i < dropdown.children.length; i++) {
  141.               if (dropdown.children[i].value === toDisable) {
  142.                 dropdown.children[i].setAttribute("disabled", "disable");
  143.               }
  144.             }
  145.  
  146.         return resolve();
  147.       });
  148.     }
  149.    
  150.     function updateStock() {
  151.         return fetchJson()
  152.         .then(parseStock)
  153.         .then(makeCountDownTimers)
  154.         .then(setTimeout(updateStock, (1000 * 60))) // Refresh once a minute
  155.     }
  156.    
  157.     Y.on('domready', function() {
  158.         checkDom()
  159.       .then(updateStock)
  160.       .catch(function(e) {
  161.         return console.log(e);
  162.       });
  163.     });
  164.  
  165.   });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement