{"size": 7047, "ext": "js", "max_stars_repo_path": "react-client/src/modules/news/reducer.js", "max_stars_repo_name": "uzubtsou/LiquidRecipes", "max_stars_repo_stars_event_min_datetime": "2017-08-08T18:45:09.000Z", "max_stars_repo_stars_event_max_datetime": "2018-06-10T17:47:50.000Z", "max_issues_repo_path": "react-client/src/modules/news/reducer.js", "max_issues_repo_name": "uzubtsou/LiquidRecipes", "max_issues_repo_issues_event_min_datetime": "2021-03-01T22:18:08.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T22:36:44.000Z", "max_forks_repo_path": "react-client/src/modules/news/reducer.js", "max_forks_repo_name": "uzubtsou/LiquidRecipes", "max_forks_repo_forks_event_min_datetime": "2017-08-08T18:45:58.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-11T09:38:49.000Z"}
{"max_stars_count": 7, "max_issues_count": 7, "max_forks_count": 5, "avg_line_length": 42.9695121951, "max_line_length": 131, "alphanum_fraction": 0.7459912019}
import * as constants from "./constants";
import * as store from "./initialState";

export const reducer = (state = store.initialState, action) => {
  switch (action.type) {

  case constants.GET_NEWS_REQUEST: return handleRequest(state);
  case constants.GET_NEWS_FAIL:    return handleFail(state, action);
  case constants.GET_NEWS_SUCCESS: return state
    .set(store.list, action.payload)
    .set(store.loading, false)
    .set(store.error, null);


  case constants.GET_TOP_NEWS_REQUEST:    return handleRequest(state);
  case constants.GET_TOP_10_NEWS_REQUEST: return handleRequest(state);
  case constants.GET_TOP_NEWS_FAIL:       return handleFail(state, action);
  case constants.GET_TOP_NEWS_SUCCESS:    return state
    .set(store.list, action.payload)
    .set(store.loading, false)
    .set(store.error, null);


  case constants.CREATE_SINGLE_NEWS_REQUEST: return handleRequest(state);
  case constants.CREATE_SINGLE_NEWS_FAIL:    return handleFail(state, action);
  case constants.CREATE_SINGLE_NEWS_SUCCESS: return state
    .updateIn([store.list], (arr) => arr.concat(action.payload))
    .set(store.loading, false)
    .set(store.error, null);


  case constants.UPDATE_SINGLE_NEWS_REQUEST: return handleRequest(state);
  case constants.UPDATE_SINGLE_NEWS_FAIL:    return handleFail(state, action);
  case constants.UPDATE_SINGLE_NEWS_SUCCESS: return state
    .updateIn([store.list], (arr) => updateElementInArray(arr, action.payload))
    .updateIn([store.userList], (arr) => updateElementInArray(arr, action.payload))
    .set(store.loading, false)
    .set(store.error, null);


  case constants.DELETE_SINGLE_NEWS_REQUEST: return handleRequest(state);
  case constants.DELETE_SINGLE_NEWS_FAIL:    return handleFail(state, action);
  case constants.DELETE_SINGLE_NEWS_SUCCESS: return state
    .updateIn([store.list], (arr) => arr.filter((val) => val.id !== action.payload))
    .updateIn([store.userList], (arr) => arr.filter((val) => val.id !== action.payload))
    .set(store.loading, false)
    .set(store.error, null);


  case constants.CREATE_SINGLE_NEWS_COMMENT_REQUEST: return handleRequest(state);
  case constants.CREATE_SINGLE_NEWS_COMMENT_FAIL:    return handleFail(state, action);
  case constants.CREATE_SINGLE_NEWS_COMMENT_SUCCESS: return state
    .updateIn([store.list], (arr) =>
      updateElementsPropertyInArray(arr, action.payload.singleNewsId, store.comments, action.payload.comment, updateInSublist))
    .updateIn([store.userList], (arr) =>
      updateElementsPropertyInArray(arr, action.payload.singleNewsId, store.comments, action.payload.comment, updateInSublist))
    .set(store.loading, false)
    .set(store.error, null);


  case constants.DELETE_SINGLE_NEWS_COMMENT_REQUEST: return handleRequest(state);
  case constants.DELETE_SINGLE_NEWS_COMMENT_FAIL:    return handleFail(state, action);
  case constants.DELETE_SINGLE_NEWS_COMMENT_SUCCESS: return state
    .updateIn([store.list], (arr) =>
      updateElementsPropertyInArray(arr, action.payload.singleNewsId, store.comments, action.payload.commentId, deleteFromSublist))
    .updateIn([store.userList], (arr) =>
      updateElementsPropertyInArray(arr, action.payload.singleNewsId, store.comments, action.payload.commentId, deleteFromSublist))
    .set(store.loading, false)
    .set(store.error, null);

  case constants.CREATE_SINGLE_NEWS_LIKE_REQUEST: return handleRequest(state);
  case constants.CREATE_SINGLE_NEWS_LIKE_FAIL:    return handleFail(state, action);
  case constants.CREATE_SINGLE_NEWS_LIKE_SUCCESS: return state
    .updateIn([store.list], (arr) =>
      updateElementsPropertyInArray(arr, action.payload.singleNewsId, store.likes, action.payload.like, updateInSublist))
    .updateIn([store.userList], (arr) =>
      updateElementsPropertyInArray(arr, action.payload.singleNewsId, store.likes, action.payload.like, updateInSublist))
    .set(store.loading, false)
    .set(store.error, null);


  case constants.DELETE_SINGLE_NEWS_LIKE_REQUEST: return handleRequest(state);
  case constants.DELETE_SINGLE_NEWS_LIKE_FAIL:    return handleFail(state, action);
  case constants.DELETE_SINGLE_NEWS_LIKE_SUCCESS: return state
    .updateIn([store.list], (arr) =>
      updateElementsPropertyInArray(arr, action.payload.singleNewsId, store.likes, action.payload.likeId, deleteFromSublist))
    .updateIn([store.userList], (arr) =>
      updateElementsPropertyInArray(arr, action.payload.singleNewsId, store.likes, action.payload.likeId, deleteFromSublist))
    .set(store.loading, false)
    .set(store.error, null);


  case constants.GET_USER_NEWS_REQUEST: return handleRequest(state);
  case constants.GET_USER_NEWS_FAIL:    return handleFail(state, action);
  case constants.GET_USER_NEWS_SUCCESS: return state
    .set(store.userList, action.payload)
    .set(store.loading, false)
    .set(store.error, null);


  case constants.ADD_SINGLE_NEWS_TO_USER_REQUEST: return handleRequest(state);
  case constants.ADD_SINGLE_NEWS_TO_USER_FAIL:    return handleFail(state, action);
  case constants.ADD_SINGLE_NEWS_TO_USER_SUCCESS: return state
    .updateIn([store.userList], (arr) => arr.concat(action.payload))
    .set(store.loading, false)
    .set(store.error, null);


  case constants.DELETE_SINGLE_NEWS_FROM_USER_REQUEST: return handleRequest(state);
  case constants.DELETE_SINGLE_NEWS_FROM_USER_FAIL:    return handleFail(state, action);
  case constants.DELETE_SINGLE_NEWS_FROM_USER_SUCCESS: return state
    .updateIn([store.userList], (arr) => arr.filter((val) => val.id !== action.payload))
    .set(store.loading, false)
    .set(store.error, null);


  case constants.CLEAR_NEWS: return store.initialState;


  default: return state;
  }
};

const updateElementInArray = function(array, element) {
  const index = array.findIndex((item) => item.id === element.id);
  if (index) array[ index ] = element;
};

const updateElementsPropertyInArray = function (array, elementId, sublistName, sublistItem, updateFunction) {
  const index = array.findIndex((item) => item.id === elementId);
  if (index) array[ index ] = updateFunction(array[ index ], sublistName, sublistItem);
  return array;
};

const updateInSublist = function (element, sublistName, sublistItem) {
  if (!element[ sublistName ]) element[ sublistName ] = [];
  const index = element[ sublistName ].findIndex((item) => item.id === sublistItem.id);
  if (index)
    element[ sublistName ][ index ] = sublistItem;
  else
    element[ sublistName ].concat(sublistItem);

  return element;
};

const deleteFromSublist = function (element, sublistName, sublistItemId) {
  if (!element[ sublistName ]) element[ sublistName ] = [];
  const index = element[ sublistName ].findIndex((item) => item.id === sublistItemId);
  if (index) element[ sublistName ].splice(index, 1);
  return element;
};

const handleRequest = function (state) {
  return state
    .set(store.loading, true)
    .set(store.error, null);
};

const handleFail = function (state, action) {
  return state
    .set(store.loading, false)
    .set(store.error, action.payload);
};
