{"size": 1439, "ext": "js", "max_stars_repo_path": "src/reducers/imageReducer.js", "max_stars_repo_name": "madeshv5543/materialauction", "max_stars_repo_stars_event_min_datetime": "2020-03-11T06:35:30.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T17:50:05.000Z", "max_issues_repo_path": "src/reducers/imageReducer.js", "max_issues_repo_name": "madeshv5543/materialauction", "max_issues_repo_issues_event_min_datetime": "2020-09-07T08:09:17.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T23:06:46.000Z", "max_forks_repo_path": "src/reducers/imageReducer.js", "max_forks_repo_name": "madeshv5543/materialauction", "max_forks_repo_forks_event_min_datetime": "2021-01-19T10:35:43.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-27T12:39:48.000Z"}
{"max_stars_count": 8, "max_issues_count": 10, "max_forks_count": 2, "avg_line_length": 20.5571428571, "max_line_length": 72, "alphanum_fraction": 0.5503822099}
import { keyBy } from 'lodash'

import {
  GET_IMAGES,
  GET_IMAGES_SUCCESS,
  GET_IMAGE,
  SET_ERROR,
  UPLOAD_IMAGE,
  UPLOAD_IMAGE_SUCCESS,
} from '../actions/types'

const initialState = {
  images: null,
  image: null,
  loading: false,
  error: null,
}

export default (state = initialState, action) => {
  switch (action.type) {
    case GET_IMAGES:
      return {
        ...state,
        loading: true,
        error: null,
      }
    case GET_IMAGES_SUCCESS:
      const imagesByIndex = keyBy(state.images, 'index')
      const updatedImages = action.payload.map((image) => {
        const updatedImage = { ...imagesByIndex[image.index], ...image }
        return updatedImage
      })
      return {
        ...state,
        loading: false,
        images: updatedImages,
        error: null,
        image: null,
      }
    case GET_IMAGE:
      return {
        ...state,
        loading: false,
        image: state.images ? state.images[action.payload] : null,
        error: null,
      }
    case UPLOAD_IMAGE:
      return {
        ...state,
        loading: true,
        error: null,
      }
    case UPLOAD_IMAGE_SUCCESS:
      return {
        ...state,
        images: [...state.images, action.payload],
        loading: false,
        error: null,
      }
    case SET_ERROR:
      return {
        ...state,
        loading: false,
        error: action.payload,
      }
    default:
      return state
  }
}
