diff --git a/readme.md b/readme.md index 79dc8af..4a83246 100644 --- a/readme.md +++ b/readme.md @@ -5,7 +5,7 @@ App which provide translating and adding words to anki ![screenshot](./readme-files/screenshot1.png) ## Build and start -```shell +```bash git clone https://github.com/Horhik/ankilan yarn install diff --git a/src/actions/api/dictionary.js b/src/actions/api/dictionary.js index cfbf269..f4b2180 100644 --- a/src/actions/api/dictionary.js +++ b/src/actions/api/dictionary.js @@ -36,21 +36,25 @@ const setDictioanry = dictioanry => ({ export const wordInfo = word => async dispatch => { try { const api1 = await getResFromWordsAPI(word); + //TODO add Urban Dictionary - - // const api2 = await getResFromUrbanDictionary(word); - // const availableApi = getAvailableApi([api1, api2]); - // if (availableApi === false) { - // throw new Error('word not found'); - // } - + /* ********Working with 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 //TODO available dictionary instead api1 await dispatch(setAvailableApi(api1)); const wordDictionary = await createDictionary(api1); dispatch(setDictioanry(wordDictionary)); - sendWord(setFields(wordDictionary)); + /* TODO: move sendWord to submit function */ + // sendWord(setFields(wordDictionary)); } catch (e) { console.log(e); } }; + +export const drawFields = fields => {}; diff --git a/src/actions/api/f-json.js b/src/actions/api/f-json.js deleted file mode 100644 index 7285e89..0000000 --- a/src/actions/api/f-json.js +++ /dev/null @@ -1,27 +0,0 @@ -// The object -// the string version of a function with /Function( -// in front and )/ at the end. -export const fJsonStingify = obj => { - return JSON.stringify(obj, function(key, value) { - if (typeof value === 'function') { - return '/Function(' + value.toString() + ')/'; - } - return value; - }); -}; -// Convert to an object using a reviver function that -// recognizes the /Function(...)/ value and converts it -// into a function via -shudder- `eval`. -export const fJsonParse = json => { - return JSON.parse(json, function(key, value) { - if ( - typeof value === 'string' && - value.startsWith('/Function(') && - value.endsWith(')/') - ) { - value = value.substring(10, value.length - 2); - return eval('(' + value + ')'); - } - return value; - }); -}; diff --git a/src/actions/api/urban-dictionary.js b/src/actions/api/urban-dictionary.js index 2d5909f..5c761f1 100644 --- a/src/actions/api/urban-dictionary.js +++ b/src/actions/api/urban-dictionary.js @@ -1,13 +1,13 @@ -import {search} from "urban-dictionary-client"; -import {URBAN_DICTIONARY_API} from "../../constants/api-constants"; +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}); + 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 +export const parseUrbanDictionaryApi = api => {}; diff --git a/src/actions/api/words-api.js b/src/actions/api/words-api.js index f553e52..ae83e61 100644 --- a/src/actions/api/words-api.js +++ b/src/actions/api/words-api.js @@ -17,31 +17,18 @@ export async function getResFromWordsAPI(word) { 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]; + /*partOfSpeeches*/ + wordsArray.forEach(currentWord => { definitionList.push({ definition: currentWord.definition, example: currentWord.examples ? currentWord.examples[0] : undefined, - id: pos.id, - pos: pos.pos, + pos: currentWord.partOfSpeech, }); }); + console.log(definitionList); return definitionList; }; diff --git a/src/actions/createAnkiLanModel.js b/src/actions/createAnkiLanModel.js index 99589fc..ed5e324 100644 --- a/src/actions/createAnkiLanModel.js +++ b/src/actions/createAnkiLanModel.js @@ -41,8 +41,9 @@ export const createAnkiLanModel = model => async dispatch => { //creator is an object what have to store in locale storage. export const addNote = words => { const template = store.getState().anki.noteTemplate; + const deckId = store.getState().anki.selectedDeck.id; const settings = { - deckId: '1', + deckId, modelId: '1585139654585', }; const creator = new AnkiDroid(settings); diff --git a/src/actions/dictionary/create-dictionary.js b/src/actions/dictionary/create-dictionary.js index 0aa7bf2..a8a5c1a 100644 --- a/src/actions/dictionary/create-dictionary.js +++ b/src/actions/dictionary/create-dictionary.js @@ -6,6 +6,8 @@ export const createDictionary = async apiRes => { const word = apiRes.word; const parsedDictionary = parseDictionary(apiRes); const audio = await getAudio(word); + const compounded = await compoundWithYDictionary(parsedDictionary, word); + //TODO add shrinkToOneExample(compounded) return {...compounded, sound: audio}; }; diff --git a/src/actions/dictionary/get-translate.js b/src/actions/dictionary/get-translate.js index f4955fd..f3900b1 100644 --- a/src/actions/dictionary/get-translate.js +++ b/src/actions/dictionary/get-translate.js @@ -1,4 +1,19 @@ import yDictionary from '../api/yandex-dictionary'; + +const selectByPos = wordArray => { + let posSet = new Set(); + let selectedArray = []; + wordArray.forEach((result, id) => { + const pos = result.pos; + if (!posSet.has(pos)) { + posSet.add(pos); + selectedArray.push(result); + } + }); + console.log(selectedArray); + return selectedArray; +}; + export const compoundWithYDictionary = async (definitionList, word) => { try { const translations = await yDictionary(word); @@ -16,28 +31,16 @@ export const compoundWithYDictionary = async (definitionList, word) => { compounded.push(translate); } }); + console.log(compounded); + const selected = selectByPos(compounded); // console.log(`RESPONSE FOR: ${word}`, {word, compounded}); return { word, pronunciation: `/${definitionList.pronunciation}/`, compounded, + filtered: selected, }; } catch (e) { - console.log('erris HERE', e); + console.log('error is HERE', e); } }; - -/* -import yDictionary from '../api/yandex-dictionary'; -let smallStore = {}; - -export const compoundWithYDictionary = async (definitionList, word) => { - let properDefinitions = []; - const translations = (() => { - yDictionary(word).then(res => { - smallStore.res = res; - }); - })(); - const yDictionaryRes = smallStore.res.payload; - console.log(yDictionaryRes, definitionList); -};*/ diff --git a/src/actions/form-actions.js b/src/actions/form-actions.js index 9f6bf0a..5564fd5 100644 --- a/src/actions/form-actions.js +++ b/src/actions/form-actions.js @@ -3,10 +3,6 @@ import {addNote} from './createAnkiLanModel'; export const submit = () => ({}); export const sendWord = async fields => { - // const creator = await getCreator(); - // const template = JSON.parse(await getTemplate()).payload; - // console.log(creator); - // console.log(template); console.log(fields); addNote(fields.payload); }; diff --git a/src/constants/api-constants.js b/src/constants/api-constants.js index 7fb6333..b120b5e 100644 --- a/src/constants/api-constants.js +++ b/src/constants/api-constants.js @@ -1,8 +1,8 @@ // 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 WORDS_API = 'WORDS_API'; +export const URBAN_DICTIONARY_API = 'URBAN_DICTIONARY_API'; +export const SET_ALL_FIELDS = 'SET_ALL_FIELDS'; - -export const SET_PARSED_DICTIONARY = 'SET_PARSED_DICTIONARY' +export const SET_PARSED_DICTIONARY = 'SET_PARSED_DICTIONARY'; diff --git a/src/reducers/api-reducer.js b/src/reducers/api-reducer.js index b595531..a152f5f 100644 --- a/src/reducers/api-reducer.js +++ b/src/reducers/api-reducer.js @@ -1,8 +1,10 @@ import { + SET_ALL_FIELDS, SET_AVAILABLE_API, SET_PARSED_DICTIONARY, SET_YANDEX_DICTIONARY_RESPONSE, } from '../constants/api-constants'; +import {SET_FIELDS} from '../constants/anki-constants'; const initialState = { word: '', @@ -12,6 +14,8 @@ const initialState = { availableApiName: '', yandexDictionaryInfo: [], parsedDictionary: {}, + availableFields: [], + allFields: [], }; const apiReducer = (state = initialState, action) => { @@ -32,6 +36,16 @@ const apiReducer = (state = initialState, action) => { ...state, parsedDictionary: action.payload, }; + case SET_FIELDS: + return { + ...state, + availableFields: action.payload, + }; + case SET_ALL_FIELDS: + return { + ...state, + allFields: action.payload, + }; default: return state; }