{"size": 833, "ext": "js", "max_stars_repo_path": "src/app/views/containers/Language/reducer.js", "max_stars_repo_name": "loantd/react-redux-skeleton", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/app/views/containers/Language/reducer.js", "max_issues_repo_name": "loantd/react-redux-skeleton", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/app/views/containers/Language/reducer.js", "max_forks_repo_name": "loantd/react-redux-skeleton", "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": 29.75, "max_line_length": 97, "alphanum_fraction": 0.7142857143}
import { isNull, isEqual } from 'lodash';

import { LanguageActionTypes } from './types';
import { AppConstants } from 'common/index';

const localeSession = localStorage.getItem(AppConstants.LocaleSessionStorageKey);
const isValidLocale = localeSession && !isNull(localeSession) && !isEqual(localeSession, 'null');
const locale = isValidLocale ? localeSession : 'en';

export const initialState = {
  newLocale: locale,
  isSwitched: isValidLocale
};

const reducer = (state = initialState, action) => {
  const { payload, type } = action;
  switch (type) {
    case LanguageActionTypes.LANGUAGE_CHANGE:
      return { ...state, newLocale: payload.locale, isSwitched: true };
    case LanguageActionTypes.LANGUAGE_CLEAR:
      return { ...state, isSwitched: false };
    default:
      return state;
  }
};

export default reducer;
