You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.1 KiB
50 lines
1.1 KiB
import { useEffect, useCallback } from 'react'
|
|
|
|
import isEmpty from 'lodash/isEmpty'
|
|
|
|
import { getLexics } from 'requests'
|
|
|
|
import {
|
|
getLexicIds,
|
|
mapTranslationsToLocalKeys,
|
|
} from 'features/LexicsStore/helpers'
|
|
|
|
import { useLang } from './useLang'
|
|
import { useLexicsConfig } from './useLexicsConfig'
|
|
import { useTranslations } from './useTranslations'
|
|
|
|
export const useLexics = () => {
|
|
const { changeLang, lang } = useLang()
|
|
const { addLexicsConfig, lexicsConfig } = useLexicsConfig()
|
|
const { addTranslations, translate } = useTranslations()
|
|
|
|
const fetchLexics = useCallback(
|
|
async () => {
|
|
const lexicIds = getLexicIds(lexicsConfig)
|
|
if (isEmpty(lexicIds)) return
|
|
|
|
const newTranslations = await getLexics(lang, lexicIds)
|
|
addTranslations(mapTranslationsToLocalKeys(newTranslations, lexicsConfig))
|
|
},
|
|
[
|
|
lang,
|
|
lexicsConfig,
|
|
addTranslations,
|
|
],
|
|
)
|
|
|
|
useEffect(() => {
|
|
fetchLexics()
|
|
}, [
|
|
lang,
|
|
lexicsConfig,
|
|
fetchLexics,
|
|
])
|
|
|
|
return {
|
|
addLexicsConfig,
|
|
changeLang,
|
|
lang,
|
|
translate,
|
|
}
|
|
}
|
|
|