import { composeWithTracker } from 'react-komposer';
import { Meteor } from 'meteor/meteor';
import { GiveawayMetaCard } from '../../components/giveaways/giveaway-meta-card';
import { Loading } from '../../components/loading';

import { Giveaways } from '../../../api/giveaways/giveaways';
import { GiveawayComments } from '../../../api/giveaway-comments/giveaway-comments';

const composer = (props, onData) => {
  if (Meteor.subscribe('giveaway-by-id', props.gaId).ready()) {
    const ga = Giveaways.findOne(props.gaId);

    if (Meteor.subscribe('user-by-id', ga.userId).ready()) {
      const user = Meteor.users.findOne({ _id: ga.userId });

      if (Meteor.subscribe('giveaways-by-user', ga.userId).ready()) {
        const shareCount = Giveaways.find({ userId: ga.userId }).count();

        if (ga.userId != Meteor.userId()) {
          onData(null, { ga, user, shareCount, commentCount: 0, pageViews: 0 });
        } else {
          if (Meteor.subscribe('comments-for-giveaway', props.gaId).ready()) {
            const commentCount = GiveawayComments.find({ giveawayId: props.gaId, isRemoved: { $ne: true } }).count();

            Meteor.call('giveaways.getPageviews', props.gaId, function (error, pageViews) {
              if (error)
                pageViews = 0;

              onData(null, { ga, user, shareCount, commentCount, pageViews });
            });
          }
        }

      }
    }
  }
};

export default composeWithTracker(composer, Loading)(GiveawayMetaCard);

@@ -369,16 +369,84 @@
 ents';%0A%0A
+let cachedPageViews = new ReactiveVar( 0 );%0Alet hasCached = false;%0A%0A
 const co
@@ -1186,32 +1186,62 @@
  %7D %7D).count();%0A%0A
+            if (!hasCached)%0A  
             Mete
@@ -1326,24 +1326,26 @@
             
+  
 if (error)%0A 
@@ -1343,16 +1343,18 @@
 (error)%0A
+  
         
@@ -1375,24 +1375,123 @@
 ews = 0;%0A%0A  
+              hasCached = true;%0A                cachedPageViews.set(pageViews);%0A              %7D);%0A%0A
             
@@ -1554,24 +1554,31 @@
 iews
- %7D);%0A           
+: cachedPageViews.get()
  %7D);

Implement simple caching for GiveawayMetaCard - prevent update lag to wait for API call to return