All files / posts-table middleware.js

100% Statements 58/58
85.19% Branches 23/27
100% Functions 12/12
100% Lines 56/56
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                  2x   2x 2x 2x       2x 2x 2x     2x 12x   2x   2x   2x   6x       2x 1x 1x     2x 1x 1x 1x 1x 6x 6x 12x 12x 2x 2x 12x 12x 6x 6x   10x     1x       9x 9x 9x   1x 1x             1x   1x 1x   1x                         1x   1x                         1x   1x                         1x   1x                   1x   1x 1x                         1x   1x                         1x   1x   9x    
import moment from 'moment';
import striptags from 'striptags';
import { actions } from '@bufferapp/async-data-fetch';
import { actionTypes as profileActionTypes } from '@bufferapp/analyze-profile-selector';
import { actionTypes as dateActionTypes } from '@bufferapp/analyze-date-picker';
import { actionTypes as exportActionTypes, actions as exportActions } from '@bufferapp/analyze-png-export';
import { actionTypes as exportCSVActionTypes, actions as exportCSVActions } from '@bufferapp/analyze-csv-export';
import { actionTypes as postsActionTypes } from './reducer';
 
const EXPORT_FILENAME = 'posts';
 
const formatMedia = (post) => {
  Eif (post.media) {
    return post.media.video ? post.media.video.details.transcoded_location : post.media.picture;
  }
};
 
const escapeText = (text) => {
  const escapedText = striptags(text, [], '\n').replace(/\n+/gm, ' ');
  return `"${escapedText}"`;
};
 
const formatDataForKey = (key, post, timezone) => {
  switch (key) {
    case 'date':
      return moment(post[key]).tz(timezone).format('MM/DD/YYYY HH:mm:ss');
    case 'media':
      return formatMedia(post);
    case 'text':
      return escapeText(post.text);
    default:
      return post[key];
  }
};
 
const getSelectedProfileTimezone = ({ profiles, selectedProfileId }) => {
  const selectedProfile = profiles.find(profile => profile.id === selectedProfileId);
  return selectedProfile.timezone;
};
 
const formatDataForCSVExport = ({ posts }, profileState) => {
  const keys = Object.keys(posts[0]);
  const timezone = getSelectedProfileTimezone(profileState);
  const data = {};
  keys.forEach((key) => {
    data[key] = [];
    posts.forEach((post) => {
      const formattedKey = formatDataForKey(key, post, timezone);
      if (key === 'statistics') {
        delete data.statistics;
        Object.keys(formattedKey).forEach((statistic) => {
          const value = formattedKey[statistic];
          if (data[statistic]) {
            data[statistic].push(value);
          } else data[statistic] = [value];
        });
      } else data[key].push(formattedKey);
    });
  });
  return data;
};
 
 
export default store => next => (action) => { // eslint-disable-line no-unused-vars
  const { dispatch, getState } = store;
  switch (action.type) {
    case exportCSVActionTypes.EXPORT_TO_CSV_START:
      Eif (getState().posts.posts.length) {
        dispatch(
          exportCSVActions.exportChart(
            EXPORT_FILENAME,
            formatDataForCSVExport(getState().posts, getState().profiles),
          ),
        );
      }
      break;
    case exportActionTypes.EXPORT_TO_PNG_START:
      dispatch(exportActions.exportChart('js-dom-to-png-posts', EXPORT_FILENAME));
      break;
    case postsActionTypes.SELECT_TOP_POSTS_METRIC:
      dispatch(actions.fetch({
        name: 'posts',
        args: {
          profileId: getState().profiles.selectedProfileId,
          service: getState().profiles.selectedProfileService,
          startDate: getState().date.startDate,
          endDate: getState().date.endDate,
          sortBy: action.metric.apiKey,
          descending: getState().posts.isDescendingSelected,
          limit: getState().posts.activePostsCount,
          searchTerms: getState().posts.searchTerms,
        },
      }));
      break;
    case postsActionTypes.SELECT_TOP_POSTS_COUNT:
      dispatch(actions.fetch({
        name: 'posts',
        args: {
          profileId: getState().profiles.selectedProfileId,
          service: getState().profiles.selectedProfileService,
          startDate: getState().date.startDate,
          endDate: getState().date.endDate,
          sortBy: getState().posts.selectedMetric.apiKey,
          descending: getState().posts.isDescendingSelected,
          limit: action.postsCount !== 'All' ? action.postsCount : undefined,
          searchTerms: getState().posts.searchTerms,
        },
      }));
      break;
    case postsActionTypes.SELECT_TOP_POSTS_ORDER:
      dispatch(actions.fetch({
        name: 'posts',
        args: {
          profileId: getState().profiles.selectedProfileId,
          service: getState().profiles.selectedProfileService,
          startDate: getState().date.startDate,
          endDate: getState().date.endDate,
          sortBy: getState().posts.selectedMetric.apiKey,
          descending: action.isDescendingSelected,
          limit: getState().posts.activePostsCount,
          searchTerms: getState().posts.searchTerms,
        },
      }));
      break;
    case profileActionTypes.SELECT_PROFILE:
      dispatch(actions.fetch({
        name: 'posts',
        args: {
          profileId: action.profile.id,
          service: action.profile.service,
          startDate: getState().date.startDate,
          endDate: getState().date.endDate,
          limit: getState().posts.activePostsCount,
        },
      }));
      break;
    case dateActionTypes.SET_DATE_RANGE:
      Eif (getState().profiles.selectedProfileId) {
        dispatch(actions.fetch({
          name: 'posts',
          args: {
            profileId: getState().profiles.selectedProfileId,
            startDate: action.startDate,
            endDate: action.endDate,
            sortBy: getState().posts.selectedMetric.apiKey,
            descending: getState().posts.isDescendingSelected,
            limit: getState().posts.activePostsCount,
            searchTerms: getState().posts.searchTerms,
          },
        }));
      }
      break;
    case postsActionTypes.SEARCH:
      dispatch(actions.fetch({
        name: 'posts',
        args: {
          profileId: getState().profiles.selectedProfileId,
          service: getState().profiles.selectedProfileService,
          startDate: getState().date.startDate,
          endDate: getState().date.endDate,
          sortBy: getState().posts.selectedMetric.apiKey,
          descending: getState().posts.isDescendingSelected,
          limit: getState().posts.activePostsCount,
          searchTerms: action.tags,
        },
      }));
      break;
    default:
      break;
  }
  return next(action);
};