diff --git a/.prettierrc.js b/.prettierrc.js index 5c4de1a..f9185bb 100644 --- a/.prettierrc.js +++ b/.prettierrc.js @@ -3,4 +3,5 @@ module.exports = { jsxBracketSameLine: true, singleQuote: true, trailingComma: 'all', + tabWidth: 2, }; diff --git a/src/actions/anki-get-actions.js b/src/actions/anki-get-actions.js index 18500ce..c973946 100644 --- a/src/actions/anki-get-actions.js +++ b/src/actions/anki-get-actions.js @@ -6,10 +6,12 @@ import { GET_FIELD_LIST, GET_MODEL_LIST, REQUEST_PERMISSIONS, + SET_ANKI_DATA, SET_EXISTING_OF_ANKI_LAN_MODEL, SET_FIELD_LIST, } from '../constants/anki-constants'; import {createAnkiLanModel} from './createAnkiLanModel'; +import {getAnkiData} from './filesystem'; /*Permissions*/ @@ -22,7 +24,6 @@ export const requestAnkiPermission = () => async dispatch => { try { const [err, res] = await AnkiDroid.requestPermission(); if (err) throw err; - console.log(res); await dispatch(setRequestAnkiPermissions(err, res)); return res; } catch (err) { @@ -122,14 +123,22 @@ export const checkAnkiLanModelForExisting = ( }; export const getModelId = (models, name) => { - console.log(models, name); - let id = ''; + let id; models.forEach(model => { if (model.name === name) { - console.log('id', model.id); id = model.id; return id; } }); return id; }; + +const setSavedData = data => ({ + type: SET_ANKI_DATA, + payload: data, +}); + +export const getSavedData = () => async dispatch => { + const data = await getAnkiData(); + setSavedData(data); +}; diff --git a/src/actions/api/words-api.js b/src/actions/api/words-api.js index ae83e61..019ccc5 100644 --- a/src/actions/api/words-api.js +++ b/src/actions/api/words-api.js @@ -21,18 +21,33 @@ export async function getResFromWordsAPI(word) { const getDefinitionList = wordsArray => { let definitionList = []; /*partOfSpeeches*/ - wordsArray.forEach(currentWord => { - definitionList.push({ - definition: currentWord.definition, - example: currentWord.examples ? currentWord.examples[0] : undefined, - pos: currentWord.partOfSpeech, - }); + let posSet = new Set(); + let examples = []; + wordsArray.forEach(words => { + posSet.add(words.partOfSpeech); + if (words.examples) { + examples.push(...words.examples); + } }); - console.log(definitionList); - return definitionList; + + posSet.forEach(pos => { + let defArray = []; + wordsArray.forEach(words => { + if (pos === words.partOfSpeech) { + defArray.push(words.definition); + } + }); + definitionList.push({pos, definitions: defArray}); + }); + return {examples, definitions: definitionList}; }; -export const parseWordsApi = api => ({ - pronunciation: api.pronunciation.all, - words: api.results ? getDefinitionList(api.results) : [], -}); +export const parseWordsApi = api => { + const words = getDefinitionList(api.results).definitions; + const examples = getDefinitionList(api.results).examples; + return { + pronunciation: api.pronunciation.all, + words: api.results ? words : [], + examples, + }; +}; diff --git a/src/actions/api/yandex-dictionary.js b/src/actions/api/yandex-dictionary.js index a14eef3..b06d8bc 100644 --- a/src/actions/api/yandex-dictionary.js +++ b/src/actions/api/yandex-dictionary.js @@ -13,7 +13,8 @@ const yDictionary = async ( {method: 'GET'}, ); const json = await res.json(); - return findPartofSpeech(json.def); + // console.log('JSON', json); + return parseResponse(json.def) } catch (e) { console.log('err in yandex-dictionary.js: ', e); } @@ -24,6 +25,15 @@ const translateTemplate = (pos, tr) => ({ tr, }); +const parseResponse = res => { + const parsed = res.map((item, id) => { + const pos = item.pos; + const tr = item.tr.map(translate => translate.text); + return {pos, tr}; + }); + return parsed; +}; + export const findPartofSpeech = dictionary => { if (dictionary.length > 1) { return [ diff --git a/src/actions/dictionary/get-translate.js b/src/actions/dictionary/get-translate.js index f3900b1..d166c2f 100644 --- a/src/actions/dictionary/get-translate.js +++ b/src/actions/dictionary/get-translate.js @@ -10,35 +10,47 @@ const selectByPos = wordArray => { selectedArray.push(result); } }); - console.log(selectedArray); return selectedArray; }; export const compoundWithYDictionary = async (definitionList, word) => { try { const translations = await yDictionary(word); + const words = definitionList.words; 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); + + console.log('YANDEX ', translations); + console.log('WORDS', definitionList); + let PoSes = new Set(); + translations.forEach(tr => PoSes.add(tr.pos)); + words.forEach(df => PoSes.add(df.pos)); + + PoSes.forEach(pos => { + let trs = []; + translations.forEach(tr => { + if (tr.pos === pos) { + tr.tr.forEach(trans => trs.push(trans)); } }); - if (definitionList.words.length === 0) { - compounded.push(translate); - } + let definitions = []; + words.forEach(word => { + if (word.pos === pos) { + word.definitions.forEach(w => definitions.push(w)); + } + }); + // compounded.push({pos, trs, definitions}); + compounded.push({ + pos: pos, + translates: trs, + definitions, + }); }); - console.log(compounded); - const selected = selectByPos(compounded); - // console.log(`RESPONSE FOR: ${word}`, {word, compounded}); + return { word, pronunciation: `/${definitionList.pronunciation}/`, compounded, - filtered: selected, + examples: words.examples, }; } catch (e) { console.log('error is HERE', e); diff --git a/src/actions/filesystem.js b/src/actions/filesystem.js index 517348a..cd6eb5f 100644 --- a/src/actions/filesystem.js +++ b/src/actions/filesystem.js @@ -4,13 +4,11 @@ import { ANKILAN_NOTE_CREATOR, ANKILAN_NOTE_TEMPLATE, } from '../constants/anki-constants'; -import JSONfn from 'jsonfn'; const sendDataToLocaleStorage = async data => { try { await AsyncStorage.clear(); - await AsyncStorage.setItem(ANKILAN_DATA, JSONfn.stringify(data)); - console.log(data); + await AsyncStorage.setItem(ANKILAN_DATA, JSON.stringify(data)); } catch (e) { // saving error alert('Error while syncing with filesystem'); @@ -23,7 +21,7 @@ export const getAnkiData = async () => { const value = await AsyncStorage.getItem(ANKILAN_DATA); if (value !== null) { // value previously stored - return JSONfn.parse(value); + return JSON.parse(value); } } catch (e) { // error reading value diff --git a/src/actions/form-actions.js b/src/actions/form-actions.js index 5564fd5..70b0a75 100644 --- a/src/actions/form-actions.js +++ b/src/actions/form-actions.js @@ -3,6 +3,5 @@ import {addNote} from './createAnkiLanModel'; export const submit = () => ({}); export const sendWord = async fields => { - console.log(fields); addNote(fields.payload); }; diff --git a/src/components/add-anklan-model.jsx b/src/components/add-anklan-model.jsx index dcd18f3..8ef0815 100644 --- a/src/components/add-anklan-model.jsx +++ b/src/components/add-anklan-model.jsx @@ -11,12 +11,14 @@ import { checkAnkiLanModelForExisting, getDeckList, getModelList, + getSavedData } from '../actions/anki-get-actions'; const StartScreen = props => { useEffect(() => { props.getDeckList(); props.getModelList(); + props.getSavedData(); }, []); useEffect(() => { props.checkAnkiLanModelForExisting( @@ -51,5 +53,6 @@ export default connect( checkAnkiLanModelForExisting, getDeckList, getModelList, + getSavedData }, )(StartScreen); diff --git a/src/components/anki-form.jsx b/src/components/anki-form.jsx index 8e4a75e..aed18b6 100644 --- a/src/components/anki-form.jsx +++ b/src/components/anki-form.jsx @@ -27,7 +27,6 @@ const AnkiForm = props => { const submit = () => { props.wordInfo(target); setSubmitted(true); - console.log(props.data) }; return ( diff --git a/src/constants/anki-constants.js b/src/constants/anki-constants.js index 54bfcb1..b21bccb 100644 --- a/src/constants/anki-constants.js +++ b/src/constants/anki-constants.js @@ -18,6 +18,7 @@ export const SET_WORD_TRANSLATE = 'SET_WORD_TRANSLATE'; export const SET_ANKI_NOTE_CREATOR = 'SET_ANKI_NOTE_CREATOR'; export const SET_CREATOR_TEMPLATE = 'SET_CREATOR_TEMPLATE'; export const SET_FIELDS = 'SET_FIELDS'; +export const SET_ANKI_DATA = 'SET_ANKI_DATA'; //Anki ui actions export const SHOW_FIELDS = 'SHOW_FIELDS'; // Anki check actions diff --git a/src/reducers/anki-reducer.js b/src/reducers/anki-reducer.js index 2975ebf..30847e2 100644 --- a/src/reducers/anki-reducer.js +++ b/src/reducers/anki-reducer.js @@ -2,6 +2,7 @@ import { GET_DECK_LIST, GET_MODEL_LIST, REQUEST_PERMISSIONS, + SET_ANKI_DATA, SET_ANKI_NOTE_CREATOR, SET_CREATOR_TEMPLATE, SET_DECK, @@ -23,6 +24,7 @@ const initialState = { ankiLanModelName: 'develop_final', noteCreator: {}, noteTemplate: [], + savedData: {}, }; const ankiReducer = (state = initialState, action) => { @@ -44,6 +46,11 @@ const ankiReducer = (state = initialState, action) => { return {...state, noteTemplate: action.payload}; case SET_ANKI_NOTE_CREATOR: return {...state, noteCreator: action.payload}; + case SET_ANKI_DATA: + return { + ...state, + savedData: action.payload, + }; default: return state; }