create stable working dictionary api. Yeahvi

This commit is contained in:
horhik 2020-03-19 17:04:57 -03:00
parent 3f2abc4954
commit 2a05cd5cb3
7 changed files with 56 additions and 28 deletions

View file

@ -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);
}

View file

@ -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'},
});

View file

@ -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) : [],
});

View file

@ -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;

View file

@ -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);
};

View file

@ -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);
}
};
/*

View file

@ -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 (
<Container style={{padding: 20}}>