{"size": 1873, "ext": "js", "max_stars_repo_path": "src/intl/LocaleReloader.js", "max_stars_repo_name": "jihwahn1018/ovirt-web-ui", "max_stars_repo_stars_event_min_datetime": "2017-02-12T15:46:53.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-26T12:26:30.000Z", "max_issues_repo_path": "src/intl/LocaleReloader.js", "max_issues_repo_name": "hstastna/ovirt-web-ui", "max_issues_repo_issues_event_min_datetime": "2017-01-05T15:01:14.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T16:35:54.000Z", "max_forks_repo_path": "src/intl/LocaleReloader.js", "max_forks_repo_name": "hstastna/ovirt-web-ui", "max_forks_repo_forks_event_min_datetime": "2017-02-07T01:12:44.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-08T23:38:10.000Z"}
{"max_stars_count": 89, "max_issues_count": 1191, "max_forks_count": 59, "avg_line_length": 39.8510638298, "max_line_length": 188, "alphanum_fraction": 0.7170315003}
import { useEffect } from 'react'
import PropTypes from 'prop-types'

import { connect } from 'react-redux'

import { withMsg } from '_/intl'
import OvirtApi from '_/ovirtapi'

const LocaleReloader = ({ children, localeFromStore, locale, reloadMsg }) => {
  /* Basic flow:
   * 1. after language settings change the value is saved on the server and in the Redux store
   * 2. value from the Redux store is passed via props and is used to trigger useEffect hook
   * 3. in the hook the change is detected by comparing value from MsgContext with Redux store
   * 4. if detected, the locale are regenerated and served via MsgContext
   * Special cases:
   * 1. locale provided by URL <server>/?locale=en_US (or inferred from browser settings) is used only if there is no locale persisted on the server (or if locale persistence is disabled).
   * 2. no data from the server (yet) - UI will try to gues user locale. When user settings will be fetched
   *    from the server and incorrect locale was guessed then locale will get hot-reloaded.
   * 3. no property on server exists but UI was launched using non-default locale(i.e. from URL) - save that locale on the server(unless locale persistence is disabled).
   */
  useEffect(() => {
    if (localeFromStore !== locale) {
      console.warn(`reload due to locale change: ${locale} -> ${localeFromStore}`)
      reloadMsg(localeFromStore)
    }
  }, [localeFromStore, locale, reloadMsg])

  useEffect(() => {
    OvirtApi.updateLocale(locale)
  }, [locale])

  return ([children])
}

LocaleReloader.propTypes = {
  children: PropTypes.node,
  localeFromStore: PropTypes.string.isRequired,
  reloadMsg: PropTypes.func.isRequired,
  locale: PropTypes.string.isRequired,
}

export default connect(
  state => ({
    localeFromStore: state.options.getIn(['remoteOptions', 'locale', 'content']),
  })
)(withMsg(LocaleReloader))
