Advertisement
simonradev

Da

Apr 15th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const SammyHelpers = (function () {
  2.     function _renderPage(templateName, addPartialsFunc, appendDataFunc) {
  3.         addPartialsFunc && addPartialsFunc(TemplateLayout);
  4.  
  5.         this.loadPartials(
  6.             TemplateLayout
  7.         ).then(function () {
  8.             const data = {
  9.                 userIsLogged: User.isLogged()
  10.             };
  11.  
  12.             appendDataFunc && appendDataFunc(data);
  13.  
  14.             this.partial(templateName, data);
  15.         });
  16.     }
  17.  
  18.     function _calcTime(dateIsoFormat) {
  19.         let diff = new Date - (new Date(dateIsoFormat));
  20.         diff = Math.floor(diff / 60000);
  21.         if (diff < 1) return 'less than a minute';
  22.         if (diff < 60) return diff + ' minute' + pluralize(diff);
  23.         diff = Math.floor(diff / 60);
  24.         if (diff < 24) return diff + ' hour' + pluralize(diff);
  25.         diff = Math.floor(diff / 24);
  26.         if (diff < 30) return diff + ' day' + pluralize(diff);
  27.         diff = Math.floor(diff / 30);
  28.         if (diff < 12) return diff + ' month' + pluralize(diff);
  29.         diff = Math.floor(diff / 12);
  30.         return diff + ' year' + pluralize(diff);
  31.         function pluralize(value) {
  32.             if (value !== 1) return 's';
  33.             else return '';
  34.         }
  35.     }
  36.    
  37.     function showHome() {
  38.         if (User.isLogged()) {
  39.             const ajaxFeed = User.getFeed();
  40.             const ajaxChirps = User.getChirps();
  41.             const ajaxFollowing = User.getFollowing();
  42.             const ajaxFollowers = User.getFollowers();
  43.  
  44.             Promise.all([ajaxFeed, ajaxChirps, ajaxFollowing, ajaxFollowers])
  45.                    .then(([feed, chirps, following, followers]) => {
  46.                        feed.forEach(f => f['elapsedTime'] = _calcTime(f._kmd.ect));
  47.                        _renderPage.call(this,
  48.                                        TemplateConstants.path.home,
  49.                                        (template) => {
  50.                                            template.chirpForm = TemplateConstants.path.chirpForm;
  51.                                        },
  52.                                        (data) => {
  53.                                         data['username'] = User.getUsername();
  54.                                         data['feed'] = feed;
  55.                                         data['chirps'] = chirps.length;
  56.                                         data['following'] = following[0].subscriptions.length;
  57.                                         data['followers'] = followers.length;
  58.                                     });
  59.                    });
  60.         } else {
  61.             _renderPage.call(this, TemplateConstants.path.loginForm);
  62.         }
  63.     }
  64.  
  65.     function showLogin() {
  66.         _renderPage.call(this, TemplateConstants.path.loginForm);
  67.     }
  68.  
  69.     function showRegister() {
  70.         _renderPage.call(this, TemplateConstants.path.registerForm);
  71.     }
  72.  
  73.     function showProfile(username, isMyProfile) {
  74.         const ajaxFeed = User.getFeed(username);
  75.         const ajaxChirps = User.getChirps(username);
  76.         const ajaxFollowing = User.getFollowing(username);
  77.         const ajaxFollowers = User.getFollowers(username);
  78.  
  79.         Promise.all([ajaxFeed, ajaxChirps, ajaxFollowing, ajaxFollowers])
  80.                 .then(([feed, chirps, following, followers]) => {
  81.                     feed.forEach(f => {
  82.                         f['elapsedTime'] = _calcTime(f._kmd.ect);
  83.                         f['isMyProfile'] = isMyProfile;
  84.                     });
  85.  
  86.                     const userId = chirps.length ? chirps[0]._acl.creator : '';
  87.                     const subs = JSON.parse(User.getSubscriptions());
  88.                     const isFollowed = Boolean(subs.find(u => u === username));
  89.                     _renderPage.call(this,
  90.                                     TemplateConstants.path.profile,
  91.                                     (template) => {
  92.                                         template.chirpForm = TemplateConstants.path.chirpForm;
  93.                                     },
  94.                                     (data) => {
  95.                                         data['isMyProfile'] = isMyProfile;
  96.                                         data['feed'] = feed;
  97.                                         data['userId'] = userId;
  98.                                         data['isFollowed'] = isFollowed;
  99.                                         data['username'] = username;
  100.                                         data['chirps'] = chirps.length;
  101.                                         data['following'] = following[0].subscriptions.length;
  102.                                         data['followers'] = followers.length;
  103.                                 });
  104.                 });
  105.     }
  106.  
  107.     function showDiscover() {
  108.         const authToken = User.getAuthToken();
  109.         const self = this;
  110.         const ajaxUsers = KinveyService.getAllUsers(authToken)
  111.                                        .then(async function (users) {
  112.                                            users = users.filter(u => u.username !== User.getUsername());
  113.                                            const ajaxes = [];
  114.                                            for (const user of users) {
  115.                                                ajaxes.push(KinveyService.getFollowers(authToken, user.username));
  116.                                            }
  117.  
  118.                                            Promise.all(ajaxes)
  119.                                                   .then(function (followers) {
  120.                                                      for (let index = 0; index < users.length; index++) {
  121.                                                          const user = users[index];
  122.                                                          user.followers = followers[index].length;
  123.                                                      }
  124.                                                      
  125.                                                      _renderPage.call(self, TemplateConstants.path.discover, null, (data) => data['users'] = users);
  126.                                                   });
  127.  
  128.                                        });
  129.        
  130.     }
  131.  
  132.     function clearForm() {
  133.         $(this.target).find('input[type=text], input[type=password], textarea').val('');
  134.     }
  135.  
  136.     return {
  137.         showHome,
  138.         showLogin,
  139.         showRegister,
  140.         clearForm,
  141.         showProfile,
  142.         showDiscover
  143.     }
  144. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement