refactor translate api
This commit is contained in:
parent
4766e3d7fb
commit
322b153e5e
|
@ -3,4 +3,5 @@ module.exports = {
|
||||||
jsxBracketSameLine: true,
|
jsxBracketSameLine: true,
|
||||||
singleQuote: true,
|
singleQuote: true,
|
||||||
trailingComma: 'all',
|
trailingComma: 'all',
|
||||||
|
tabWidth: 2,
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,10 +6,12 @@ import {
|
||||||
GET_FIELD_LIST,
|
GET_FIELD_LIST,
|
||||||
GET_MODEL_LIST,
|
GET_MODEL_LIST,
|
||||||
REQUEST_PERMISSIONS,
|
REQUEST_PERMISSIONS,
|
||||||
|
SET_ANKI_DATA,
|
||||||
SET_EXISTING_OF_ANKI_LAN_MODEL,
|
SET_EXISTING_OF_ANKI_LAN_MODEL,
|
||||||
SET_FIELD_LIST,
|
SET_FIELD_LIST,
|
||||||
} from '../constants/anki-constants';
|
} from '../constants/anki-constants';
|
||||||
import {createAnkiLanModel} from './createAnkiLanModel';
|
import {createAnkiLanModel} from './createAnkiLanModel';
|
||||||
|
import {getAnkiData} from './filesystem';
|
||||||
|
|
||||||
/*Permissions*/
|
/*Permissions*/
|
||||||
|
|
||||||
|
@ -22,7 +24,6 @@ export const requestAnkiPermission = () => async dispatch => {
|
||||||
try {
|
try {
|
||||||
const [err, res] = await AnkiDroid.requestPermission();
|
const [err, res] = await AnkiDroid.requestPermission();
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
console.log(res);
|
|
||||||
await dispatch(setRequestAnkiPermissions(err, res));
|
await dispatch(setRequestAnkiPermissions(err, res));
|
||||||
return res;
|
return res;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -122,14 +123,22 @@ export const checkAnkiLanModelForExisting = (
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getModelId = (models, name) => {
|
export const getModelId = (models, name) => {
|
||||||
console.log(models, name);
|
let id;
|
||||||
let id = '';
|
|
||||||
models.forEach(model => {
|
models.forEach(model => {
|
||||||
if (model.name === name) {
|
if (model.name === name) {
|
||||||
console.log('id', model.id);
|
|
||||||
id = model.id;
|
id = model.id;
|
||||||
return id;
|
return 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 => {
|
const getDefinitionList = wordsArray => {
|
||||||
let definitionList = [];
|
let definitionList = [];
|
||||||
/*partOfSpeeches*/
|
/*partOfSpeeches*/
|
||||||
wordsArray.forEach(currentWord => {
|
let posSet = new Set();
|
||||||
definitionList.push({
|
let examples = [];
|
||||||
definition: currentWord.definition,
|
wordsArray.forEach(words => {
|
||||||
example: currentWord.examples ? currentWord.examples[0] : undefined,
|
posSet.add(words.partOfSpeech);
|
||||||
pos: currentWord.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 => ({
|
export const parseWordsApi = api => {
|
||||||
pronunciation: api.pronunciation.all,
|
const words = getDefinitionList(api.results).definitions;
|
||||||
words: api.results ? getDefinitionList(api.results) : [],
|
const examples = getDefinitionList(api.results).examples;
|
||||||
});
|
return {
|
||||||
|
pronunciation: api.pronunciation.all,
|
||||||
|
words: api.results ? words : [],
|
||||||
|
examples,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
|
@ -13,7 +13,8 @@ const yDictionary = async (
|
||||||
{method: 'GET'},
|
{method: 'GET'},
|
||||||
);
|
);
|
||||||
const json = await res.json();
|
const json = await res.json();
|
||||||
return findPartofSpeech(json.def);
|
// console.log('JSON', json);
|
||||||
|
return parseResponse(json.def)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('err in yandex-dictionary.js: ', e);
|
console.log('err in yandex-dictionary.js: ', e);
|
||||||
}
|
}
|
||||||
|
@ -24,6 +25,15 @@ const translateTemplate = (pos, tr) => ({
|
||||||
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 => {
|
export const findPartofSpeech = dictionary => {
|
||||||
if (dictionary.length > 1) {
|
if (dictionary.length > 1) {
|
||||||
return [
|
return [
|
||||||
|
|
|
@ -10,35 +10,47 @@ const selectByPos = wordArray => {
|
||||||
selectedArray.push(result);
|
selectedArray.push(result);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
console.log(selectedArray);
|
|
||||||
return selectedArray;
|
return selectedArray;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const compoundWithYDictionary = async (definitionList, word) => {
|
export const compoundWithYDictionary = async (definitionList, word) => {
|
||||||
try {
|
try {
|
||||||
const translations = await yDictionary(word);
|
const translations = await yDictionary(word);
|
||||||
|
const words = definitionList.words;
|
||||||
let compounded = [];
|
let compounded = [];
|
||||||
// console.log('YANDEX ', translations);
|
|
||||||
// console.log('WORDS', definitionList);
|
console.log('YANDEX ', translations);
|
||||||
translations.forEach(translate => {
|
console.log('WORDS', definitionList);
|
||||||
definitionList.words.forEach(definition => {
|
let PoSes = new Set();
|
||||||
if (definition.pos === translate.pos) {
|
translations.forEach(tr => PoSes.add(tr.pos));
|
||||||
const compound = {...definition, ...translate};
|
words.forEach(df => PoSes.add(df.pos));
|
||||||
compounded.push(compound);
|
|
||||||
|
PoSes.forEach(pos => {
|
||||||
|
let trs = [];
|
||||||
|
translations.forEach(tr => {
|
||||||
|
if (tr.pos === pos) {
|
||||||
|
tr.tr.forEach(trans => trs.push(trans));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (definitionList.words.length === 0) {
|
let definitions = [];
|
||||||
compounded.push(translate);
|
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 {
|
return {
|
||||||
word,
|
word,
|
||||||
pronunciation: `/${definitionList.pronunciation}/`,
|
pronunciation: `/${definitionList.pronunciation}/`,
|
||||||
compounded,
|
compounded,
|
||||||
filtered: selected,
|
examples: words.examples,
|
||||||
};
|
};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('error is HERE', e);
|
console.log('error is HERE', e);
|
||||||
|
|
|
@ -4,13 +4,11 @@ import {
|
||||||
ANKILAN_NOTE_CREATOR,
|
ANKILAN_NOTE_CREATOR,
|
||||||
ANKILAN_NOTE_TEMPLATE,
|
ANKILAN_NOTE_TEMPLATE,
|
||||||
} from '../constants/anki-constants';
|
} from '../constants/anki-constants';
|
||||||
import JSONfn from 'jsonfn';
|
|
||||||
|
|
||||||
const sendDataToLocaleStorage = async data => {
|
const sendDataToLocaleStorage = async data => {
|
||||||
try {
|
try {
|
||||||
await AsyncStorage.clear();
|
await AsyncStorage.clear();
|
||||||
await AsyncStorage.setItem(ANKILAN_DATA, JSONfn.stringify(data));
|
await AsyncStorage.setItem(ANKILAN_DATA, JSON.stringify(data));
|
||||||
console.log(data);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// saving error
|
// saving error
|
||||||
alert('Error while syncing with filesystem');
|
alert('Error while syncing with filesystem');
|
||||||
|
@ -23,7 +21,7 @@ export const getAnkiData = async () => {
|
||||||
const value = await AsyncStorage.getItem(ANKILAN_DATA);
|
const value = await AsyncStorage.getItem(ANKILAN_DATA);
|
||||||
if (value !== null) {
|
if (value !== null) {
|
||||||
// value previously stored
|
// value previously stored
|
||||||
return JSONfn.parse(value);
|
return JSON.parse(value);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// error reading value
|
// error reading value
|
||||||
|
|
|
@ -3,6 +3,5 @@ import {addNote} from './createAnkiLanModel';
|
||||||
|
|
||||||
export const submit = () => ({});
|
export const submit = () => ({});
|
||||||
export const sendWord = async fields => {
|
export const sendWord = async fields => {
|
||||||
console.log(fields);
|
|
||||||
addNote(fields.payload);
|
addNote(fields.payload);
|
||||||
};
|
};
|
||||||
|
|
|
@ -11,12 +11,14 @@ import {
|
||||||
checkAnkiLanModelForExisting,
|
checkAnkiLanModelForExisting,
|
||||||
getDeckList,
|
getDeckList,
|
||||||
getModelList,
|
getModelList,
|
||||||
|
getSavedData
|
||||||
} from '../actions/anki-get-actions';
|
} from '../actions/anki-get-actions';
|
||||||
|
|
||||||
const StartScreen = props => {
|
const StartScreen = props => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
props.getDeckList();
|
props.getDeckList();
|
||||||
props.getModelList();
|
props.getModelList();
|
||||||
|
props.getSavedData();
|
||||||
}, []);
|
}, []);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
props.checkAnkiLanModelForExisting(
|
props.checkAnkiLanModelForExisting(
|
||||||
|
@ -51,5 +53,6 @@ export default connect(
|
||||||
checkAnkiLanModelForExisting,
|
checkAnkiLanModelForExisting,
|
||||||
getDeckList,
|
getDeckList,
|
||||||
getModelList,
|
getModelList,
|
||||||
|
getSavedData
|
||||||
},
|
},
|
||||||
)(StartScreen);
|
)(StartScreen);
|
||||||
|
|
|
@ -27,7 +27,6 @@ const AnkiForm = props => {
|
||||||
const submit = () => {
|
const submit = () => {
|
||||||
props.wordInfo(target);
|
props.wordInfo(target);
|
||||||
setSubmitted(true);
|
setSubmitted(true);
|
||||||
console.log(props.data)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
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_ANKI_NOTE_CREATOR = 'SET_ANKI_NOTE_CREATOR';
|
||||||
export const SET_CREATOR_TEMPLATE = 'SET_CREATOR_TEMPLATE';
|
export const SET_CREATOR_TEMPLATE = 'SET_CREATOR_TEMPLATE';
|
||||||
export const SET_FIELDS = 'SET_FIELDS';
|
export const SET_FIELDS = 'SET_FIELDS';
|
||||||
|
export const SET_ANKI_DATA = 'SET_ANKI_DATA';
|
||||||
//Anki ui actions
|
//Anki ui actions
|
||||||
export const SHOW_FIELDS = 'SHOW_FIELDS';
|
export const SHOW_FIELDS = 'SHOW_FIELDS';
|
||||||
// Anki check actions
|
// Anki check actions
|
||||||
|
|
|
@ -2,6 +2,7 @@ import {
|
||||||
GET_DECK_LIST,
|
GET_DECK_LIST,
|
||||||
GET_MODEL_LIST,
|
GET_MODEL_LIST,
|
||||||
REQUEST_PERMISSIONS,
|
REQUEST_PERMISSIONS,
|
||||||
|
SET_ANKI_DATA,
|
||||||
SET_ANKI_NOTE_CREATOR,
|
SET_ANKI_NOTE_CREATOR,
|
||||||
SET_CREATOR_TEMPLATE,
|
SET_CREATOR_TEMPLATE,
|
||||||
SET_DECK,
|
SET_DECK,
|
||||||
|
@ -23,6 +24,7 @@ const initialState = {
|
||||||
ankiLanModelName: 'develop_final',
|
ankiLanModelName: 'develop_final',
|
||||||
noteCreator: {},
|
noteCreator: {},
|
||||||
noteTemplate: [],
|
noteTemplate: [],
|
||||||
|
savedData: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
const ankiReducer = (state = initialState, action) => {
|
const ankiReducer = (state = initialState, action) => {
|
||||||
|
@ -44,6 +46,11 @@ const ankiReducer = (state = initialState, action) => {
|
||||||
return {...state, noteTemplate: action.payload};
|
return {...state, noteTemplate: action.payload};
|
||||||
case SET_ANKI_NOTE_CREATOR:
|
case SET_ANKI_NOTE_CREATOR:
|
||||||
return {...state, noteCreator: action.payload};
|
return {...state, noteCreator: action.payload};
|
||||||
|
case SET_ANKI_DATA:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
savedData: action.payload,
|
||||||
|
};
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue