refactor translate api
This commit is contained in:
parent
4766e3d7fb
commit
322b153e5e
|
@ -3,4 +3,5 @@ module.exports = {
|
|||
jsxBracketSameLine: true,
|
||||
singleQuote: true,
|
||||
trailingComma: 'all',
|
||||
tabWidth: 2,
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
posSet.forEach(pos => {
|
||||
let defArray = [];
|
||||
wordsArray.forEach(words => {
|
||||
if (pos === words.partOfSpeech) {
|
||||
defArray.push(words.definition);
|
||||
}
|
||||
});
|
||||
console.log(definitionList);
|
||||
return definitionList;
|
||||
definitionList.push({pos, definitions: defArray});
|
||||
});
|
||||
return {examples, definitions: definitionList};
|
||||
};
|
||||
|
||||
export const parseWordsApi = api => ({
|
||||
export const parseWordsApi = api => {
|
||||
const words = getDefinitionList(api.results).definitions;
|
||||
const examples = getDefinitionList(api.results).examples;
|
||||
return {
|
||||
pronunciation: api.pronunciation.all,
|
||||
words: api.results ? getDefinitionList(api.results) : [],
|
||||
});
|
||||
words: api.results ? words : [],
|
||||
examples,
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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 [
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
});
|
||||
console.log(compounded);
|
||||
const selected = selectByPos(compounded);
|
||||
// console.log(`RESPONSE FOR: ${word}`, {word, compounded});
|
||||
// compounded.push({pos, trs, definitions});
|
||||
compounded.push({
|
||||
pos: pos,
|
||||
translates: trs,
|
||||
definitions,
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
word,
|
||||
pronunciation: `/${definitionList.pronunciation}/`,
|
||||
compounded,
|
||||
filtered: selected,
|
||||
examples: words.examples,
|
||||
};
|
||||
} catch (e) {
|
||||
console.log('error is HERE', e);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -3,6 +3,5 @@ import {addNote} from './createAnkiLanModel';
|
|||
|
||||
export const submit = () => ({});
|
||||
export const sendWord = async fields => {
|
||||
console.log(fields);
|
||||
addNote(fields.payload);
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -27,7 +27,6 @@ const AnkiForm = props => {
|
|||
const submit = () => {
|
||||
props.wordInfo(target);
|
||||
setSubmitted(true);
|
||||
console.log(props.data)
|
||||
};
|
||||
|
||||
return (
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue