From aa9cb5924a26ff76e736af405962dbd45dac2efa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D1=82=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B5=D0=B2=20=D0=9C=D0=B8=D1=80=D0=BB=D0=B0=D0=BD?= Date: Mon, 20 Dec 2021 06:32:29 +0000 Subject: [PATCH] Ott 2091 lang change --- src/features/GlobalStores/index.tsx | 3 +-- .../LexicsStore/helpers/isSupportedLang/index.tsx | 9 --------- src/features/LexicsStore/hooks/useLang.tsx | 8 +++++--- .../helpers/isSupportedLang/__tests__/index.tsx | 0 src/helpers/isSupportedLang/index.tsx | 10 ++++++++++ src/helpers/languageUrlParam/index.tsx | 9 ++++++--- src/hooks/useStorage/index.tsx | 10 ++++++++++ 7 files changed, 32 insertions(+), 17 deletions(-) delete mode 100644 src/features/LexicsStore/helpers/isSupportedLang/index.tsx rename src/{features/LexicsStore => }/helpers/isSupportedLang/__tests__/index.tsx (100%) create mode 100644 src/helpers/isSupportedLang/index.tsx diff --git a/src/features/GlobalStores/index.tsx b/src/features/GlobalStores/index.tsx index 2baa0191..9d858a1e 100644 --- a/src/features/GlobalStores/index.tsx +++ b/src/features/GlobalStores/index.tsx @@ -1,12 +1,11 @@ import { ReactNode } from 'react' -import { client } from 'config/clients' import { getLanguageUrlParam } from 'helpers/languageUrlParam' import { AuthStore } from 'features/AuthStore' import { LexicsStore } from 'features/LexicsStore' -const initialLanguage = getLanguageUrlParam() || client.defaultLanguage +const initialLanguage = getLanguageUrlParam() type Props = { children: ReactNode, diff --git a/src/features/LexicsStore/helpers/isSupportedLang/index.tsx b/src/features/LexicsStore/helpers/isSupportedLang/index.tsx deleted file mode 100644 index e1fd224c..00000000 --- a/src/features/LexicsStore/helpers/isSupportedLang/index.tsx +++ /dev/null @@ -1,9 +0,0 @@ -import map from 'lodash/map' -import includes from 'lodash/includes' - -import { langsList } from 'config/languages' - -export const isSupportedLang = (lang: string) => { - const supportedLangs = map(langsList, 'locale') - return includes(supportedLangs, lang) -} diff --git a/src/features/LexicsStore/hooks/useLang.tsx b/src/features/LexicsStore/hooks/useLang.tsx index 0024d611..5d2db3ba 100644 --- a/src/features/LexicsStore/hooks/useLang.tsx +++ b/src/features/LexicsStore/hooks/useLang.tsx @@ -3,14 +3,16 @@ import { useCallback } from 'react' import { useLocalStore } from 'hooks' import type { Languages } from 'config/languages' +import { client } from 'config/clients' -import { isSupportedLang } from '../helpers/isSupportedLang' +import { isSupportedLang } from 'helpers/isSupportedLang' const LANG_KEY = 'lang' -const DEFAULT_LANG = 'en' +const DEFAULT_LANG = client.defaultLanguage || 'en' -export const useLang = (initialLanguage: Languages = DEFAULT_LANG) => { +export const useLang = (initialLanguage?: Languages) => { const [lang, setLang] = useLocalStore({ + defaultValue: DEFAULT_LANG, initialValue: initialLanguage, key: LANG_KEY, validator: isSupportedLang, diff --git a/src/features/LexicsStore/helpers/isSupportedLang/__tests__/index.tsx b/src/helpers/isSupportedLang/__tests__/index.tsx similarity index 100% rename from src/features/LexicsStore/helpers/isSupportedLang/__tests__/index.tsx rename to src/helpers/isSupportedLang/__tests__/index.tsx diff --git a/src/helpers/isSupportedLang/index.tsx b/src/helpers/isSupportedLang/index.tsx new file mode 100644 index 00000000..2e9a2938 --- /dev/null +++ b/src/helpers/isSupportedLang/index.tsx @@ -0,0 +1,10 @@ +import map from 'lodash/map' +import includes from 'lodash/includes' + +import type { Languages } from 'config/languages' +import { langsList } from 'config/languages' + +export const isSupportedLang = (lang: string): lang is Languages => { + const supportedLangs = map(langsList, ({ locale }) => locale) + return includes(supportedLangs, lang) +} diff --git a/src/helpers/languageUrlParam/index.tsx b/src/helpers/languageUrlParam/index.tsx index c5b352e4..432bb63a 100644 --- a/src/helpers/languageUrlParam/index.tsx +++ b/src/helpers/languageUrlParam/index.tsx @@ -1,11 +1,14 @@ import type { Languages } from 'config/languages' import { history } from 'config/history' +import { isSupportedLang } from 'helpers/isSupportedLang' + const KEY = 'lang' -export const getLanguageUrlParam = () => ( - new URLSearchParams(history.location.search).get(KEY) as Languages -) +export const getLanguageUrlParam = () => { + const lang = new URLSearchParams(history.location.search).get(KEY) + return lang && isSupportedLang(lang) ? lang : undefined +} export const addLanguageUrlParam = (lang: Languages, url: string) => { const urlObject = new URL(url) diff --git a/src/hooks/useStorage/index.tsx b/src/hooks/useStorage/index.tsx index a984348b..96fe4a6a 100644 --- a/src/hooks/useStorage/index.tsx +++ b/src/hooks/useStorage/index.tsx @@ -9,7 +9,17 @@ const defaultSerializer = (key: string, value: any) => value type Args = { clearOnUnmount?: boolean, + + /** + * Дефолтное знаение + * используется если полученный из сторежда значение не валидно + */ defaultValue?: T, + + /** + * Начальное значение + * если указано пропускает считывание из стореджа + */ initialValue?: T, key: string, serialize?: (key: string, value: T) => string,