diff --git a/src/actions/api/dictionary.js b/src/actions/api/dictionary.js index dfcb92e..643937f 100644 --- a/src/actions/api/dictionary.js +++ b/src/actions/api/dictionary.js @@ -1,32 +1,16 @@ import {search} from 'urban-dictionary-client'; -import {SET_AVAILABLE_API} from '../../constants/api-constants'; +import { + SET_AVAILABLE_API, + URBAN_DICTIONARY_API, + WORDS_API, +} from '../../constants/api-constants'; const fetch = require('node-fetch'); - -async function getResFromWordsAPI(word) { - const req = await fetch(`https://wordsapiv1.p.rapidapi.com/words/${word}`, { - method: 'GET', - headers: { - 'x-rapidapi-host': 'wordsapiv1.p.rapidapi.com', - 'x-rapidapi-key': 'e08b0f617cmsh74abbf9a3b01eb0p164f22jsnabea29750b15', - }, - }); - const json = await req.json(); - const res = {...json, source: 'wordsAPI'}; - if (res.success === false) { - // throw new Error(res.message); - return false; - } - return Promise.resolve(res); -} - -async function getResFromUrbanDictionary(word) { - const res = await search(word); - if (res.list.length === 0) { - // throw new Error('nothing was found'); - return false; - } - return Promise.resolve({...res, word, source: 'urbanDictionary'}); -} +import {getResFromWordsAPI, parseWordsApi} from './words-api'; +import { + getResFromUrbanDictionary, + parseUrbanDictionaryApi, +} from './urban-dictionary'; +import {createDictionary} from '../dictionary/create-dictionary'; const getAvailableApi = (apiArray = []) => { for (const api of apiArray) { @@ -41,16 +25,19 @@ const setAvailableApi = api => ({ type: SET_AVAILABLE_API, payload: api, }); + export const wordInfo = word => async dispatch => { try { const api1 = await getResFromWordsAPI(word); const api2 = await getResFromUrbanDictionary(word); const availableApi = getAvailableApi([api1, api2]); - console.log(availableApi); if (availableApi === false) { throw new Error('word not found'); } + console.log(availableApi); + //function which return universal template for more simple interaction with api await dispatch(setAvailableApi(availableApi)); + createDictionary(availableApi); } catch (e) { console.log(e); } diff --git a/src/actions/api/parse-actions.js b/src/actions/api/parse-actions.js new file mode 100644 index 0000000..e69de29 diff --git a/src/actions/api/urban-dictionary.js b/src/actions/api/urban-dictionary.js index e69de29..2d5909f 100644 --- a/src/actions/api/urban-dictionary.js +++ b/src/actions/api/urban-dictionary.js @@ -0,0 +1,13 @@ +import {search} from "urban-dictionary-client"; +import {URBAN_DICTIONARY_API} from "../../constants/api-constants"; + +export async function getResFromUrbanDictionary(word) { + const res = await search(word); + if (res.list.length === 0) { + // throw new Error('nothing was found'); + return false; + } + return Promise.resolve({...res, word, source: URBAN_DICTIONARY_API}); +} + +export const parseUrbanDictionaryApi = api => {} \ No newline at end of file diff --git a/src/actions/api/words-api.js b/src/actions/api/words-api.js index e69de29..3442e20 100644 --- a/src/actions/api/words-api.js +++ b/src/actions/api/words-api.js @@ -0,0 +1,50 @@ +import {WORDS_API} from '../../constants/api-constants'; + +export async function getResFromWordsAPI(word) { + const req = await fetch(`https://wordsapiv1.p.rapidapi.com/words/${word}`, { + method: 'GET', + headers: { + 'x-rapidapi-host': 'wordsapiv1.p.rapidapi.com', + 'x-rapidapi-key': 'e08b0f617cmsh74abbf9a3b01eb0p164f22jsnabea29750b15', + }, + }); + const json = await req.json(); + const res = {...json, source: WORDS_API}; + if (res.success === false) { + // throw new Error(res.message); + return false; + } + return Promise.resolve(res); +} + +const parsePOS = wordArray => { + let posSet = new Set(); + let posArray = []; + wordArray.forEach((result, id) => { + const pos = result.partOfSpeech; + if (!posSet.has(pos)) { + posSet.add(pos); + posArray.push({pos, id}); + } + }); + return posArray; +}; +//get all what anki template need for template +const getDefinitionList = wordsArray => { + const partOfSpeeches = parsePOS(wordsArray); + let definitionList = []; + partOfSpeeches.forEach(pos => { + const currentWord = wordsArray[pos.id]; + definitionList.push({ + definition: currentWord.definition, + example: currentWord.examples[0], + id: pos.id, + }); + }); + return definitionList; +}; + +export const parseWordsApi = api => ({ + pronunciation: api.pronunciation.all, + words: getDefinitionList(api.results), +}); diff --git a/src/actions/dictionary/create-dictionary.js b/src/actions/dictionary/create-dictionary.js new file mode 100644 index 0000000..3e3883b --- /dev/null +++ b/src/actions/dictionary/create-dictionary.js @@ -0,0 +1,5 @@ +import {parseDictionary} from './parsing-dictionary'; + +export const createDictionary = apiRes => { + const parsedDictionary = parseDictionary(apiRes); +}; diff --git a/src/actions/dictionary/parsing-dictionary.js b/src/actions/dictionary/parsing-dictionary.js new file mode 100644 index 0000000..9c681a9 --- /dev/null +++ b/src/actions/dictionary/parsing-dictionary.js @@ -0,0 +1,16 @@ +//middleware between two api response parsers +import {URBAN_DICTIONARY_API, WORDS_API} from '../../constants/api-constants'; +import {parseWordsApi} from '../api/words-api'; +import {parseUrbanDictionaryApi} from '../api/urban-dictionary'; + +export const parseDictionary = api => { + switch (api.source) { + case WORDS_API: + return parseWordsApi(api); + case URBAN_DICTIONARY_API: + alert('Not available yet'); + return parseUrbanDictionaryApi(api); + default: + throw new Error('wrong api'); + } +}; diff --git a/src/components/anki-form.jsx b/src/components/anki-form.jsx index 252c1ee..7525c34 100644 --- a/src/components/anki-form.jsx +++ b/src/components/anki-form.jsx @@ -11,8 +11,8 @@ import {wordInfo} from "../actions/api/dictionary"; const AnkiForm = props => { useEffect(() => { - props.yDictionary('wealth'); - props.wordInfo('suck') + props.yDictionary('like'); + props.wordInfo('penis poop') }, []); return ( diff --git a/src/constants/api-constants.js b/src/constants/api-constants.js index 8601898..7fb6333 100644 --- a/src/constants/api-constants.js +++ b/src/constants/api-constants.js @@ -1,3 +1,8 @@ -// export const c = 'c' +// export const ct = 'ct' export const SET_YANDEX_DICTIONARY_RESPONSE = 'SET_YANDEX_DICTIONARY_RESPONSE'; export const SET_AVAILABLE_API = 'SET_AVAILABLE_API'; +export const WORDS_API = 'WORDS_API' +export const URBAN_DICTIONARY_API = 'URBAN_DICTIONARY_API' + + +export const SET_PARSED_DICTIONARY = 'SET_PARSED_DICTIONARY' diff --git a/src/reducers/api-reducer.js b/src/reducers/api-reducer.js index 20eba91..13a5f96 100644 --- a/src/reducers/api-reducer.js +++ b/src/reducers/api-reducer.js @@ -10,6 +10,7 @@ const initialState = { availableApi: {}, availableApiName: '', yandexDictionaryInfo: [], + parsedDictionary: {}, }; const apiReducer = (state = initialState, action) => {