{"size": 2581, "ext": "js", "max_stars_repo_path": "src/_reducers/users.reducer.js", "max_stars_repo_name": "vskrip/twitter-clone-app", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/_reducers/users.reducer.js", "max_issues_repo_name": "vskrip/twitter-clone-app", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/_reducers/users.reducer.js", "max_forks_repo_name": "vskrip/twitter-clone-app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.0707070707, "max_line_length": 87, "alphanum_fraction": 0.5687717939}
import { userConstants } from "../_constants";

const initialState = {
  items: [],
  loading: false,
  error: null,
};

export function users(state = initialState, action) {
  switch (action.type) {
    case userConstants.GETALL_REQUEST:
      return {
        loading: true,
      };
    case userConstants.GETALL_SUCCESS:
      return {
        ...state,
        items: action.users,
        loading: false,
      };
    case userConstants.GETALL_FAILURE:
      return {
        error: action.error,
      };
    case userConstants.GET_USER_BY_ID_REQUEST:
      return {
        loading: true,
      };
    case userConstants.GET_USER_BY_ID_SUCCESS:
      return {
        items: action.user,
        loading: false,
      };
    case userConstants.GET_USER_BY_ID_FAILURE:
      return {
        error: action.error,
      };
    case userConstants.UPDATE_USER_REQUEST:
      return {
        ...state,
        loading: true,
      };
    case userConstants.UPDATE_USER_SUCCESS:
      return {
        ...state,
        loading: false,
      };
    case userConstants.UPDATE_USER_FAILURE:
      return {
        error: action.error,
      };
    case userConstants.MOVE_CLIENT_TO_USER_REQUEST:
      return {
        ...state,
        loading: true,
      };
    case userConstants.MOVE_CLIENT_TO_USER_SUCCESS:
      return {
        ...state,
        items: state.items.filter((user) => user.id !== action.user.id),
        loading: false,
      };
    case userConstants.MOVE_CLIENT_TO_USER_FAILURE:
      return {
        error: action.error,
      };
    case userConstants.DELETE_REQUEST:
      // add 'deleting:true' property to user being deleted
      return {
        ...state,
        items: state.items.map((user) =>
          user.id === action.id ? { ...user, deleting: true } : user
        ),
      };
    case userConstants.DELETE_SUCCESS:
      // remove deleted user from state
      return {
        items: state.items.filter((user) => user.id !== action.id),
      };
    case userConstants.DELETE_FAILURE:
      // remove 'deleting:true' property and add 'deleteError:[error]' property to user
      return {
        ...state,
        items: state.items.map((user) => {
          if (user.id === action.id) {
            // make copy of user without 'deleting:true' property
            const { deleting, ...userCopy } = user;
            // return copy of user with 'deleteError:[error]' property
            return { ...userCopy, deleteError: action.error };
          }

          return user;
        }),
      };
    default:
      return state;
  }
}
