diff --git a/src/actions/api/dictionary.js b/src/actions/api/dictionary.js index 643937f..f58ee61 100644 --- a/src/actions/api/dictionary.js +++ b/src/actions/api/dictionary.js @@ -29,15 +29,18 @@ const setAvailableApi = api => ({ export const wordInfo = word => async dispatch => { try { const api1 = await getResFromWordsAPI(word); - const api2 = await getResFromUrbanDictionary(word); - const availableApi = getAvailableApi([api1, api2]); - if (availableApi === false) { - throw new Error('word not found'); - } - console.log(availableApi); + //TODO add Urban Dictionary + + // const api2 = await getResFromUrbanDictionary(word); + // const availableApi = getAvailableApi([api1, api2]); + // if (availableApi === false) { + // throw new Error('word not found'); + // } + //function which return universal template for more simple interaction with api - await dispatch(setAvailableApi(availableApi)); - createDictionary(availableApi); + //TODO available dictionary instead api1 + await dispatch(setAvailableApi(api1)); + await createDictionary(api1); } catch (e) { console.log(e); } diff --git a/src/actions/api/word-sound.js b/src/actions/api/word-sound.js index d26ad6b..2b561a1 100644 --- a/src/actions/api/word-sound.js +++ b/src/actions/api/word-sound.js @@ -1,5 +1,5 @@ -const getAudio = async url => { - const site = await fetch(url, { +const getAudio = async word => { + const site = await fetch(word, { method: 'GET', headers: {'Content-Type': 'text/html'}, }); diff --git a/src/actions/api/words-api.js b/src/actions/api/words-api.js index 3442e20..f553e52 100644 --- a/src/actions/api/words-api.js +++ b/src/actions/api/words-api.js @@ -37,8 +37,9 @@ const getDefinitionList = wordsArray => { const currentWord = wordsArray[pos.id]; definitionList.push({ definition: currentWord.definition, - example: currentWord.examples[0], + example: currentWord.examples ? currentWord.examples[0] : undefined, id: pos.id, + pos: pos.pos, }); }); return definitionList; @@ -46,5 +47,5 @@ const getDefinitionList = wordsArray => { export const parseWordsApi = api => ({ pronunciation: api.pronunciation.all, - words: getDefinitionList(api.results), + words: api.results ? getDefinitionList(api.results) : [], }); diff --git a/src/actions/api/yandex-dictionary.js b/src/actions/api/yandex-dictionary.js index 6f4530e..f95c7a9 100644 --- a/src/actions/api/yandex-dictionary.js +++ b/src/actions/api/yandex-dictionary.js @@ -13,8 +13,8 @@ const yDictionary = async ( {method: 'GET'}, ); const json = await res.json(); + // console.log(word, json.def); return findPartofSpeech(json.def); - return json.def; } catch (e) { console.log('err in yandex-dictionary.js: ', e); } @@ -26,14 +26,15 @@ const translateTemplate = (pos, tr) => ({ }); export const findPartofSpeech = dictionary => { - return { - type: SET_YANDEX_DICTIONARY_RESPONSE, - payload: [ + if (dictionary.length > 1) { + return [ // TODO create flexible field selector, by poses count translateTemplate(dictionary[0].pos, dictionary[0].tr[0].text), translateTemplate(dictionary[1].pos, dictionary[1].tr[0].text), - ], - }; + ]; + } else { + return [translateTemplate(dictionary[0].pos, dictionary[0].tr[0].text)]; + } }; export default yDictionary; diff --git a/src/actions/dictionary/create-dictionary.js b/src/actions/dictionary/create-dictionary.js index 4463563..470be96 100644 --- a/src/actions/dictionary/create-dictionary.js +++ b/src/actions/dictionary/create-dictionary.js @@ -2,9 +2,9 @@ import {parseDictionary} from './parsing-dictionary'; import getAudio from '../api/word-sound'; import {compoundWithYDictionary} from './get-translate'; -export const createDictionary = apiRes => { +export const createDictionary = async apiRes => { const word = apiRes.word; const parsedDictionary = parseDictionary(apiRes); - const audio = getAudio(word); - compoundWithYDictionary(parsedDictionary, word); + // const audio = await getAudio(word); + await compoundWithYDictionary(parsedDictionary, word); }; diff --git a/src/actions/dictionary/get-translate.js b/src/actions/dictionary/get-translate.js index 11f680f..788a579 100644 --- a/src/actions/dictionary/get-translate.js +++ b/src/actions/dictionary/get-translate.js @@ -1,10 +1,26 @@ import yDictionary from '../api/yandex-dictionary'; -export const compoundWithYDictionary = (definitionList, word) => { - const translations = (async () => { - const res = await yDictionary(word); - return await res; - })(); - console.log(translations); +export const compoundWithYDictionary = async (definitionList, word) => { + try { + const translations = await yDictionary(word); + let compounded = []; + // console.log('YANDEX ', translations); + // console.log('WORDS', definitionList); + + translations.forEach(translate => { + definitionList.words.forEach(definition => { + if (definition.pos === translate.pos) { + const compound = {...definition, ...translate}; + compounded.push(compound); + } + }); + if (definitionList.words.length === 0) { + compounded.push(translate); + } + }); + console.log(`RESPONSE FOR: ${word}`, compounded); + } catch (e) { + console.log('erris HERE', e); + } }; /* diff --git a/src/components/anki-form.jsx b/src/components/anki-form.jsx index 79aeaa8..24c1316 100644 --- a/src/components/anki-form.jsx +++ b/src/components/anki-form.jsx @@ -9,7 +9,14 @@ import {wordInfo} from "../actions/api/dictionary"; const AnkiForm = props => { useEffect(() => { - props.wordInfo('like') + props.wordInfo('Urge'); + props.wordInfo('Maze'); + props.wordInfo('Ramification'); + props.wordInfo('Dare'); + props.wordInfo('Entrepreneurship'); + props.wordInfo('meagre'); + props.wordInfo('meager'); + }, []); return (