AllenYuan

src/actions/index.js first actions

Apr 27th, 2020
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import action from "./action";
  2. import { langs } from '../lang';
  3.  
  4. const API_HOST = 'https://api.opendota.com';
  5.  
  6. export const getMetadata = () => action('metadata', API_HOST, 'api/metadata');
  7. export const getStrings = () => async (dispatch) => {
  8.   // find i18n map for the given lang
  9.   const getLang = lang => langs.find(item => item.value === lang);
  10.   // get set language. default to english.
  11.   const savedLang = window.localStorage && window.localStorage.getItem('localization');
  12.   const userLang = window.navigator.language;
  13.   const defaultLang = langs[0];
  14.   const lang = getLang(savedLang) || getLang(userLang) || defaultLang;
  15.  
  16.   const defData = await import(`../lang/${defaultLang.value}.json`)
  17.   const selData = await import(`../lang/${lang.value}.json`);
  18.  
  19.   dispatch({ type: 'strings', payload: { ...defData, ...selData }});
  20. };
Advertisement
Add Comment
Please, Sign In to add comment