<commit_before>let update = require('immutability-helper');

import {
  GIPHY_SEARCH_PENDING,
  GIPHY_SEARCH_SUCCESS,
  GIPHY_SEARCH_ERROR,
} from '../constants';

const INITIAL_STATE = {
  searchResults: [],
  hasError: false,
  isLoading: false,
  isLoadingMore: false,
  term: null,
  pageNum: 0,
  canLoadMore: false,
};

function searchReducer(state = INITIAL_STATE,
                       action = { type: '', payload: null }) {
  switch (action.type) {

  case GIPHY_SEARCH_PENDING:
    return update(state, { $merge: {
      searchResults: state.term === action.payload.term ? state.searchResults : [],
      hasError: false,
      isLoading: action.payload.pageNum === 0,
      isLoadingMore: action.payload.pageNum > 0,
      term: action.payload.term,
      pageNum: action.payload.pageNum,
    }});

  case GIPHY_SEARCH_SUCCESS:
    const { count, offset, total_count } = action.payload.pagination;

    return update(state, { $merge: {
      searchResults: [...state.searchResults, ...action.payload.data],
      hasError: false,
      isLoading: false,
      isLoadingMore: false,
      canLoadMore: count + offset < total_count

    }});

  case GIPHY_SEARCH_ERROR:
    return update(state, { $merge: {
      hasError: true,
      isLoading: false,
      isLoadingMore: false,
      term: null,
      pageNum: 0,
      canLoadMore: false
    }});

  default:
    return state;
  }
}

export default searchReducer;
<commit_msg>Use pagenum to determine whether it's a new search or loading more from the same search.<commit_after>let update = require('immutability-helper');

import {
  GIPHY_SEARCH_PENDING,
  GIPHY_SEARCH_SUCCESS,
  GIPHY_SEARCH_ERROR,
} from '../constants';

const INITIAL_STATE = {
  searchResults: [],
  hasError: false,
  isLoading: false,
  isLoadingMore: false,
  term: null,
  pageNum: 0,
  canLoadMore: false,
};

function searchReducer(state = INITIAL_STATE,
                       action = { type: '', payload: null }) {
  switch (action.type) {

  case GIPHY_SEARCH_PENDING:
    const loadingMore = action.payload.pageNum > 0;

    return update(state, { $merge: {
      searchResults: loadingMore ? state.searchResults : [],
      hasError: false,
      isLoading: !loadingMore,
      isLoadingMore: loadingMore,
      term: action.payload.term,
      pageNum: action.payload.pageNum,
    }});

  case GIPHY_SEARCH_SUCCESS:
    const { count, offset, total_count } = action.payload.pagination;

    return update(state, { $merge: {
      searchResults: [...state.searchResults, ...action.payload.data],
      hasError: false,
      isLoading: false,
      isLoadingMore: false,
      canLoadMore: count + offset < total_count

    }});

  case GIPHY_SEARCH_ERROR:
    return update(state, { $merge: {
      hasError: true,
      isLoading: false,
      isLoadingMore: false,
      term: null,
      pageNum: 0,
      canLoadMore: false
    }});

  default:
    return state;
  }
}

export default searchReducer;

{'fraction_non_alphanumeric': 0.11502029769959404, 'fraction_numerical': 0.002368064952638701, 'mean_word_length': 2.9798115746971736, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7450184', 'n_tokens_mistral': 969, 'n_tokens_neox': 949, 'n_words': 216}