<commit_before>import _ from 'lodash';

import * as ActionTypes from '../action-types';
import * as ActionHelpers from '../action-helpers';

const { request, response, fail } = ActionHelpers;
const indexById = list => _.keyBy(list || [], 'id');
const mergeByIds = (state, array) => ({ ...state, ...indexById(array) });

const initPostViewState = (post) => {
  const id = post.id;
  const isEditing = false;
  const errorStatus = '';
  const errorMessage = '';
  const commentError = '';

  return { id, isEditing, errorStatus, errorMessage, commentError };
};

export default function postViews(state = {}, action) {
  if (ActionHelpers.isFeedResponse(action)) {
    return mergeByIds(state, (action.payload.posts || []).map(initPostViewState));
  }
  switch (action.type) {
    case request(ActionTypes.SHOW_MORE_LIKES_ASYNC): {
      const id = action.payload.postId;
      const isLoadingLikes = true;
      return { ...state, [id]: { ...state[id], isLoadingLikes } };
    }
    case response(ActionTypes.SHOW_MORE_LIKES_ASYNC): {
      const id = action.payload.posts.id;
      const isLoadingLikes = false;
      return { ...state, [id]: { ...state[id], isLoadingLikes } };
    }
    case fail(ActionTypes.SHOW_MORE_LIKES_ASYNC): {
      const id = action.request.postId;
      const isLoadingLikes = false;
      return { ...state, [id]: { ...state[id], isLoadingLikes } };
    }
    case request(ActionTypes.SHOW_MORE_COMMENTS): {
      const id = action.payload.postId;
      const isLoadingComments = true;

      return { ...state, [id]: { ...state[id], isLoadingComments } };
    }
    case response(ActionTypes.SHOW_MORE_COMMENTS): {
      const id = action.payload.posts.id;
      const isLoadingComments = false;

      return { ...state, [id]: { ...state[id], isLoadingComments } };
    }
    case response(ActionTypes.GET_SINGLE_POST): {
      const id = action.payload.posts.id;
      return { ...state, [id]: initPostViewState(action.payload.posts) };
    }
    case ActionTypes.REALTIME_POST_NEW:
    case ActionTypes.REALTIME_POST_UPDATE: {
      const id = action.post.id;
      const postAlreadyAdded = !!state[id];
      if (postAlreadyAdded) {
        return state;
      }
      return { ...state, [id]: initPostViewState(action.post) };
    }
    case fail(ActionTypes.GET_SINGLE_POST): {
      const id = action.request.postId;<commit_msg>Add throbber for "X more comments" on Feed->Post transition<commit_after>import _ from 'lodash';

import * as ActionTypes from '../action-types';
import * as ActionHelpers from '../action-helpers';

const { request, response, fail } = ActionHelpers;
const indexById = list => _.keyBy(list || [], 'id');
const mergeByIds = (state, array) => ({ ...state, ...indexById(array) });

const initPostViewState = (post) => {
  const id = post.id;
  const isEditing = false;
  const errorStatus = '';
  const errorMessage = '';
  const commentError = '';

  return { id, isEditing, errorStatus, errorMessage, commentError };
};

export default function postViews(state = {}, action) {
  if (ActionHelpers.isFeedResponse(action)) {
    return mergeByIds(state, (action.payload.posts || []).map(initPostViewState));
  }
  switch (action.type) {
    case request(ActionTypes.SHOW_MORE_LIKES_ASYNC): {
      const id = action.payload.postId;
      const isLoadingLikes = true;
      return { ...state, [id]: { ...state[id], isLoadingLikes } };
    }
    case response(ActionTypes.SHOW_MORE_LIKES_ASYNC): {
      const id = action.payload.posts.id;
      const isLoadingLikes = false;
      return { ...state, [id]: { ...state[id], isLoadingLikes } };
    }
    case fail(ActionTypes.SHOW_MORE_LIKES_ASYNC): {
      const id = action.request.postId;
      const isLoadingLikes = false;
      return { ...state, [id]: { ...state[id], isLoadingLikes } };
    }
    case request(ActionTypes.SHOW_MORE_COMMENTS):
    case request(ActionTypes.GET_SINGLE_POST): {
      const id = action.payload.postId;
      const isLoadingComments = true;
      return { ...state, [id]: { ...state[id], isLoadingComments } };
    }
    case response(ActionTypes.SHOW_MORE_COMMENTS): {
      const id = action.payload.posts.id;
      const isLoadingComments = false;
      return { ...state, [id]: { ...state[id], isLoadingComments } };
    }
    case response(ActionTypes.GET_SINGLE_POST): {
      const id = action.payload.posts.id;
      return { ...state, [id]: initPostViewState(action.payload.posts) };
    }
    case ActionTypes.REALTIME_POST_NEW:
    case ActionTypes.REALTIME_POST_UPDATE: {
      const id = action.post.id;
      const postAlreadyAdded = !!state[id];
      if (postAlreadyAdded) {
        return state;
      }
      return { ...state, [id]: initPostViewState(action.post) };
    }
    case fail(ActionTypes.GET_SINGLE_POST): {
      const id = action.request.postId;
{'fraction_non_alphanumeric': 0.14555256064690028, 'fraction_numerical': 0.0, 'mean_word_length': 3.133676092544987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2209646', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1508, 'n_words': 329}