{"size": 1076, "ext": "js", "max_stars_repo_path": "src/components/Language/LanguageSetter.js", "max_stars_repo_name": "Bnei-Baruch/kmedia-mdb", "max_stars_repo_stars_event_min_datetime": "2018-02-05T11:50:16.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-20T15:31:07.000Z", "max_issues_repo_path": "src/components/Language/LanguageSetter.js", "max_issues_repo_name": "Bnei-Baruch/kmedia-mdb", "max_issues_repo_issues_event_min_datetime": "2017-12-27T04:06:11.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-21T01:35:05.000Z", "max_forks_repo_path": "src/components/Language/LanguageSetter.js", "max_forks_repo_name": "Bnei-Baruch/kmedia-mdb", "max_forks_repo_forks_event_min_datetime": "2020-05-17T07:59:41.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-17T07:59:41.000Z"}
{"max_stars_count": 6, "max_issues_count": 69, "max_forks_count": 1, "avg_line_length": 30.7428571429, "max_line_length": 86, "alphanum_fraction": 0.6933085502}
import { useEffect } from 'react';
import PropTypes from 'prop-types';
import { useDispatch, useSelector } from 'react-redux';
import { actions, selectors } from '../../redux/modules/settings';
import { DEFAULT_LANGUAGE, LANGUAGES } from '../../helpers/consts';
import * as shapes from '../shapes';

// NOTE: yaniv -> edo: should we block rendering until language changed?

const LanguageSetter = ({ language, children }) => {
  const currentLanguage = useSelector(state => selectors.getLanguage(state.settings));
  const dispatch        = useDispatch();

  useEffect(() => {
    // catch language change only on client
    if (typeof window === 'undefined') {
      return;
    }

    if (language && language !== currentLanguage) {
      const actualLanguage = LANGUAGES[language] ? language : DEFAULT_LANGUAGE;
      dispatch(actions.setLanguage(actualLanguage));
    }
  }, [language, currentLanguage, dispatch]);

  return children;
};

LanguageSetter.propTypes = {
  language: PropTypes.string,
  children: shapes.Children.isRequired,
};

export default LanguageSetter;
