Sending word to Anki works

but only one translate available instead one OR two
This commit is contained in:
horhik 2020-03-25 10:02:48 -03:00
parent f8b2ff9ccc
commit 1dda00ab6f
10 changed files with 250 additions and 172 deletions

View file

@ -13,6 +13,7 @@
"@react-native-community/async-storage": "^1.8.1", "@react-native-community/async-storage": "^1.8.1",
"axios": "^0.19.2", "axios": "^0.19.2",
"babel-preset-react-native": "^4.0.1", "babel-preset-react-native": "^4.0.1",
"jsonfn": "^0.31.0",
"native-base": "^2.13.8", "native-base": "^2.13.8",
"node-fetch": "^2.6.0", "node-fetch": "^2.6.0",
"react": "16.13.0", "react": "16.13.0",

View file

@ -69,7 +69,6 @@ export const getModelList = () => async dispatch => {
if (err) { if (err) {
throw err; throw err;
} }
console.log(res);
await dispatch(setModelList(res)); await dispatch(setModelList(res));
} catch (err) { } catch (err) {
console.log(err); console.log(err);

View file

@ -13,6 +13,7 @@ import {
} from './urban-dictionary'; } from './urban-dictionary';
import {createDictionary} from '../dictionary/create-dictionary'; import {createDictionary} from '../dictionary/create-dictionary';
import {setFields} from '../anki-set-actions'; import {setFields} from '../anki-set-actions';
import {sendWord, submit} from '../form-actions';
const getAvailableApi = (apiArray = []) => { const getAvailableApi = (apiArray = []) => {
for (const api of apiArray) { for (const api of apiArray) {
@ -48,7 +49,7 @@ export const wordInfo = word => async dispatch => {
await dispatch(setAvailableApi(api1)); await dispatch(setAvailableApi(api1));
const wordDictionary = await createDictionary(api1); const wordDictionary = await createDictionary(api1);
dispatch(setDictioanry(wordDictionary)); dispatch(setDictioanry(wordDictionary));
dispatch(setFields(wordDictionary)); sendWord(setFields(wordDictionary));
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }

27
src/actions/api/f-json.js Normal file
View file

@ -0,0 +1,27 @@
// 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;
});
};

View file

@ -1,182 +1,35 @@
import {AnkiDroid} from 'react-native-ankidroid/dist/ankidroid'; import {AnkiDroid} from 'react-native-ankidroid/dist/ankidroid';
import {setAnkiNoteCreator, setCreatorTemplate} from './anki-set-actions'; import {setAnkiNoteCreator, setCreatorTemplate} from './anki-set-actions';
import sendDataToLocaleStorage from './filesystem'; import sendDataToLocaleStorage from './filesystem';
import setSettings, {
modelFields,
valueFields,
} from '../constants/anki-model-values';
import { import {
checkAnkiLanModelForExisting, checkAnkiLanModelForExisting,
getFieldList, getFieldList,
getModelList, getModelList,
} from './anki-get-actions'; } from './anki-get-actions';
import JSONfn from 'jsonfn';
import store from '../store';
export const createAnkiLanModel = model => async dispatch => { export const createAnkiLanModel = model => async dispatch => {
try { try {
const deckName = model.deck.name; const settings = setSettings(model);
const modelName = model.name;
const dbDeckReference = 'com.anki.decks';
const dbModelReference = 'com.anki.models';
const tags = ['AnkiLan'];
const modelFields = [
'Word or sentence',
'Part of speech 1',
'Translation 1',
'Definition 1',
'Part of speech 2',
'Translation 2',
'Definition 2',
'Usage example',
'Transcription',
'Sound',
];
const cardNames = ['Russian>English', 'English>Russian'];
const css = `.card {
font-family: 'Roboto', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
font-size: 24px;
text-align: center;
color: black;
background-color: white;
word-wrap: break-word;
}
.big {
font-size: 48px;
}
.small {
font-size: 18px;
}
.sound--container {
display: flex;
justify-content: center;
padding: 0px;
align-items: center;
}
.sound {
padding-left: 10px;
}
.transcription {
color: #1e7efc;
font-weight: bold;
}
.list{
list-style: none;
display: flex;
flex-direction:column;
align-items:center;
padding: 0;
}
.definition{
font-size: 20px;
font-famyli: sans-serif;
font-style: italic;
display: block;
margin: 7px ;
padding: 0 20px ;
white-space: normal;
width: 70vw;
}
.usage{
background-color: rgba(100,100,100,0.1);
padding: 10px;
font-size: 20px;
white-space: normal;
width: 70vw;
margin: 0 auto;
border-radius: 5px;
}`;
const questionFmt1 = `
<div class="big">{{Word or sentence}}</div>
<ul class="list">
<li>
<div class="small">{{Part of speech 1}}</div>
</li>
<li>
<div class="small">{{Part of speech 2}}</div>
</li>
</ul>
<div class="sound--container">
<div class="transcription">{{Transcription}}</div>
<span class="sound">{{Sound}}</span>
`;
const questionFmt2 = '<div class="big">{{Translation 1}}</div>';
const questionFormat = [questionFmt1, questionFmt2];
const answerFmt1 = `
<div class="big">{{Word or sentence}}</div>
<div class="sound--container">
<div class="transcription">{{Transcription}}</div>
<span class="sound">{{Sound}}</span>
</div>
<hr>
<div class="usage">{{Usage example}}</div>
<hr />
<ul class="list">
<li>
<div>
<div class="small">{{Part of speech 1}}</div>
<pre class="definition">{{Definition 1}}</pre>
<div>{{Translation 1}}</div>
</div>
</li>
<li>
<div>
<div class="small">{{Part of speech 2}}</div>
<pre class="definition">{{Definition 2}}</pre>
<div>{{Translation 2}}</div>
</div>
</li>
</ul>
</div>`;
const answerFormat = [answerFmt1, answerFmt1];
// ADDING NOTES //
const deckProperties = {
name: deckName,
reference: dbDeckReference,
};
const modelProperties = {
name: modelName,
reference: dbModelReference,
fields: modelFields,
tags,
cardNames,
questionFormat,
answerFormat,
css,
};
const valueFields = [
'AnkiLan',
'Translate by typing the word',
'',
'You can type the word in input field and get translate, sound and definition of this word',
'Translate via sharing',
'',
'You can mark the word in you phone and share it with AnkiLan',
'IMAGE WILL BE HERE',
'guide',
'',
];
const settings = {
modelId: undefined,
modelProperties: modelProperties,
deckId: model.deck.id,
deckProperties: deckProperties,
};
const selectedDeck = new AnkiDroid(settings); const selectedDeck = new AnkiDroid(settings);
await dispatch(setAnkiNoteCreator(selectedDeck)); await dispatch(setAnkiNoteCreator(selectedDeck));
await dispatch(setCreatorTemplate(modelFields)); await dispatch(setCreatorTemplate(modelFields));
sendDataToLocaleStorage( // const sd = JSON.parse(JSON.stringify(selectedDeck));
setAnkiNoteCreator(selectedDeck), //send creator to locale storage // console.log(sd === selectedDeck);
setCreatorTemplate(modelFields), // ****************
); alert('oh shit');
addNote(selectedDeck, valueFields, modelFields); addNote(selectedDeck, valueFields, modelFields);
// ****************
// sendDataToLocaleStorage(
// setAnkiNoteCreator(sd), //send creator to locale storage
// setCreatorTemplate(modelFields),
// );
checkAnkiLanModelForExisting(model.name, model.list); checkAnkiLanModelForExisting(model.name, model.list);
await dispatch(getModelList()); await dispatch(getModelList());
await dispatch(getFieldList(model.name)); await dispatch(getFieldList(model.name));
@ -186,6 +39,14 @@ export const createAnkiLanModel = model => async dispatch => {
}; };
//creator is an object what have to store in locale storage. //creator is an object what have to store in locale storage.
export const addNote = (creator, words, template) => { export const addNote = words => {
creator.addNote(words, template); const template = store.getState().anki.noteTemplate;
const settings = {
deckId: '1',
modelId: '1585139654585',
};
const creator = new AnkiDroid(settings);
creator.addNote(words, template);
alert('sucssess');
}; };

View file

@ -3,10 +3,17 @@ 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 (creator, template) => { const sendDataToLocaleStorage = async (creator, template) => {
try { try {
await AsyncStorage.setItem(ANKILAN_NOTE_CREATOR, JSON.stringify(creator)); await AsyncStorage.clear();
await AsyncStorage.setItem(ANKILAN_NOTE_TEMPLATE, JSON.stringify(template));
await AsyncStorage.setItem(ANKILAN_NOTE_CREATOR, JSONfn.stringify(creator));
await AsyncStorage.setItem(
ANKILAN_NOTE_TEMPLATE,
JSONfn.stringify(template),
);
} catch (e) { } catch (e) {
// saving error // saving error
alert('Error while syncing with filesystem'); alert('Error while syncing with filesystem');
@ -18,9 +25,10 @@ export default sendDataToLocaleStorage;
export const getTemplate = async () => { export const getTemplate = async () => {
try { try {
const value = await AsyncStorage.getItem(ANKILAN_NOTE_TEMPLATE); const value = await AsyncStorage.getItem(ANKILAN_NOTE_TEMPLATE);
console.log(value);
if (value !== null) { if (value !== null) {
// value previously stored // value previously stored
return value; return JSONfn.parse(value);
} }
} catch (e) { } catch (e) {
// error reading value // error reading value
@ -29,10 +37,10 @@ export const getTemplate = async () => {
export const getCreator = async () => { export const getCreator = async () => {
try { try {
const value = await AsyncStorage.getItem(ANKILAN_NOTE_TEMPLATE); const value = await AsyncStorage.getItem(ANKILAN_NOTE_CREATOR);
if (value !== null) { if (value !== null) {
// value previously stored // value previously stored
return value; return JSONfn.parse(value);
} }
} catch (e) { } catch (e) {
// error reading value // error reading value

View file

@ -1 +1,12 @@
import {getCreator, getTemplate} from './filesystem';
import {addNote} from './createAnkiLanModel';
export const submit = () => ({}); 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);
};

View file

@ -0,0 +1,165 @@
export const modelFields = [
'Word or sentence',
'Part of speech 1',
'Translation 1',
'Definition 1',
'Part of speech 2',
'Translation 2',
'Definition 2',
'Usage example',
'Transcription',
'Sound',
];
export const valueFields = [
'AnkiLan',
'Translate by typing the word',
'',
'You can type the word in input field and get translate, sound and definition of this word',
'Translate via sharing',
'',
'You can mark the word in you phone and share it with AnkiLan',
'IMAGE WILL BE HERE',
'guide',
'',
];
const setSettings = model => {
const deckName = model.deck.name;
const modelName = model.name;
const dbDeckReference = 'com.anki.decks';
const dbModelReference = 'com.anki.models';
const tags = ['AnkiLan'];
const cardNames = ['Russian>English', 'English>Russian'];
const css = `.card {
font-family: 'Roboto', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
font-size: 24px;
text-align: center;
color: black;
background-color: white;
word-wrap: break-word;
}
.big {
font-size: 48px;
}
.small {
font-size: 18px;
}
.sound--container {
display: flex;
justify-content: center;
padding: 0px;
align-items: center;
}
.sound {
padding-left: 10px;
}
.transcription {
color: #1e7efc;
font-weight: bold;
}
.list{
list-style: none;
display: flex;
flex-direction:column;
align-items:center;
padding: 0;
}
.definition{
font-size: 20px;
font-famyli: sans-serif;
font-style: italic;
display: block;
margin: 7px ;
padding: 0 20px ;
white-space: normal;
width: 70vw;
}
.usage{
background-color: rgba(100,100,100,0.1);
padding: 10px;
font-size: 20px;
white-space: normal;
width: 70vw;
margin: 0 auto;
border-radius: 5px;
}`;
const questionFmt1 = `
<div class="big">{{Word or sentence}}</div>
<ul class="list">
<li>
<div class="small">{{Part of speech 1}}</div>
</li>
<li>
<div class="small">{{Part of speech 2}}</div>
</li>
</ul>
<div class="sound--container">
<div class="transcription">{{Transcription}}</div>
<span class="sound">{{Sound}}</span>
`;
const questionFmt2 = '<div class="big">{{Translation 1}}</div>';
const questionFormat = [questionFmt1, questionFmt2];
const answerFmt1 = `
<div class="big">{{Word or sentence}}</div>
<div class="sound--container">
<div class="transcription">{{Transcription}}</div>
<span class="sound">{{Sound}}</span>
</div>
<hr>
<div class="usage">{{Usage example}}</div>
<hr />
<ul class="list">
<li>
<div>
<div class="small">{{Part of speech 1}}</div>
<pre class="definition">{{Definition 1}}</pre>
<div>{{Translation 1}}</div>
</div>
</li>
<li>
<div>
<div class="small">{{Part of speech 2}}</div>
<pre class="definition">{{Definition 2}}</pre>
<div>{{Translation 2}}</div>
</div>
</li>
</ul>
</div>`;
const answerFormat = [answerFmt1, answerFmt1];
// ADDING NOTES //
const deckProperties = {
name: deckName,
reference: dbDeckReference,
};
const modelProperties = {
name: modelName,
reference: dbModelReference,
fields: modelFields,
tags,
cardNames,
questionFormat,
answerFormat,
css,
};
return {
modelId: undefined,
modelProperties: modelProperties,
deckId: model.deck.id,
deckProperties: deckProperties,
};
};
export default setSettings;

View file

@ -20,7 +20,7 @@ const initialState = {
mainFieldIsAvailable: false, mainFieldIsAvailable: false,
fieldList: [], fieldList: [],
ankiLanModelIsAlreadyExists: false, ankiLanModelIsAlreadyExists: false,
ankiLanModelName: 'AnkiLan_final1', ankiLanModelName: '1AnkiLan1111',
noteCreator: {}, noteCreator: {},
noteTemplate: [], noteTemplate: [],
}; };

View file

@ -5026,6 +5026,11 @@ jsonfile@^4.0.0:
optionalDependencies: optionalDependencies:
graceful-fs "^4.1.6" graceful-fs "^4.1.6"
jsonfn@^0.31.0:
version "0.31.0"
resolved "https://registry.yarnpkg.com/jsonfn/-/jsonfn-0.31.0.tgz#8cd09ee376d46df75c2c0de3c548a95dbb81dc8b"
integrity sha1-jNCe43bUbfdcLA3jxUipXbuB3Is=
jsonify@~0.0.0: jsonify@~0.0.0:
version "0.0.0" version "0.0.0"
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73" resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"