diff --git a/package.json b/package.json index 5ca7c81..ca8c2e0 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@react-native-community/async-storage": "^1.8.1", "axios": "^0.19.2", "babel-preset-react-native": "^4.0.1", + "jsonfn": "^0.31.0", "native-base": "^2.13.8", "node-fetch": "^2.6.0", "react": "16.13.0", diff --git a/src/actions/anki-get-actions.js b/src/actions/anki-get-actions.js index 810053a..86d9343 100644 --- a/src/actions/anki-get-actions.js +++ b/src/actions/anki-get-actions.js @@ -69,7 +69,6 @@ export const getModelList = () => async dispatch => { if (err) { throw err; } - console.log(res); await dispatch(setModelList(res)); } catch (err) { console.log(err); diff --git a/src/actions/api/dictionary.js b/src/actions/api/dictionary.js index f6532f0..cfbf269 100644 --- a/src/actions/api/dictionary.js +++ b/src/actions/api/dictionary.js @@ -13,6 +13,7 @@ import { } from './urban-dictionary'; import {createDictionary} from '../dictionary/create-dictionary'; import {setFields} from '../anki-set-actions'; +import {sendWord, submit} from '../form-actions'; const getAvailableApi = (apiArray = []) => { for (const api of apiArray) { @@ -48,7 +49,7 @@ export const wordInfo = word => async dispatch => { await dispatch(setAvailableApi(api1)); const wordDictionary = await createDictionary(api1); dispatch(setDictioanry(wordDictionary)); - dispatch(setFields(wordDictionary)); + sendWord(setFields(wordDictionary)); } catch (e) { console.log(e); } diff --git a/src/actions/api/f-json.js b/src/actions/api/f-json.js new file mode 100644 index 0000000..7285e89 --- /dev/null +++ b/src/actions/api/f-json.js @@ -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; + }); +}; diff --git a/src/actions/createAnkiLanModel.js b/src/actions/createAnkiLanModel.js index 4290b53..99589fc 100644 --- a/src/actions/createAnkiLanModel.js +++ b/src/actions/createAnkiLanModel.js @@ -1,182 +1,35 @@ import {AnkiDroid} from 'react-native-ankidroid/dist/ankidroid'; import {setAnkiNoteCreator, setCreatorTemplate} from './anki-set-actions'; import sendDataToLocaleStorage from './filesystem'; +import setSettings, { + modelFields, + valueFields, +} from '../constants/anki-model-values'; import { checkAnkiLanModelForExisting, getFieldList, getModelList, } from './anki-get-actions'; +import JSONfn from 'jsonfn'; +import store from '../store'; export const createAnkiLanModel = model => async dispatch => { try { - const deckName = model.deck.name; - 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; - } + const settings = setSettings(model); - .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 = ` -
{{Word or sentence}}
- -
-
{{Transcription}}
- {{Sound}} - `; - - const questionFmt2 = '
{{Translation 1}}
'; - - const questionFormat = [questionFmt1, questionFmt2]; - - const answerFmt1 = ` -
{{Word or sentence}}
-
-
{{Transcription}}
- {{Sound}} -
-
-
{{Usage example}}
-
- -
`; - 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); await dispatch(setAnkiNoteCreator(selectedDeck)); await dispatch(setCreatorTemplate(modelFields)); - sendDataToLocaleStorage( - setAnkiNoteCreator(selectedDeck), //send creator to locale storage - setCreatorTemplate(modelFields), - ); + // const sd = JSON.parse(JSON.stringify(selectedDeck)); + // console.log(sd === selectedDeck); + // **************** + alert('oh shit'); addNote(selectedDeck, valueFields, modelFields); + // **************** + // sendDataToLocaleStorage( + // setAnkiNoteCreator(sd), //send creator to locale storage + // setCreatorTemplate(modelFields), + // ); checkAnkiLanModelForExisting(model.name, model.list); await dispatch(getModelList()); 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. -export const addNote = (creator, words, template) => { +export const addNote = words => { + const template = store.getState().anki.noteTemplate; + const settings = { + deckId: '1', + modelId: '1585139654585', + }; + const creator = new AnkiDroid(settings); + creator.addNote(words, template); + alert('sucssess'); }; diff --git a/src/actions/filesystem.js b/src/actions/filesystem.js index 4833258..aad5d6d 100644 --- a/src/actions/filesystem.js +++ b/src/actions/filesystem.js @@ -3,10 +3,17 @@ import { ANKILAN_NOTE_CREATOR, ANKILAN_NOTE_TEMPLATE, } from '../constants/anki-constants'; +import JSONfn from 'jsonfn'; + const sendDataToLocaleStorage = async (creator, template) => { try { - await AsyncStorage.setItem(ANKILAN_NOTE_CREATOR, JSON.stringify(creator)); - await AsyncStorage.setItem(ANKILAN_NOTE_TEMPLATE, JSON.stringify(template)); + await AsyncStorage.clear(); + + await AsyncStorage.setItem(ANKILAN_NOTE_CREATOR, JSONfn.stringify(creator)); + await AsyncStorage.setItem( + ANKILAN_NOTE_TEMPLATE, + JSONfn.stringify(template), + ); } catch (e) { // saving error alert('Error while syncing with filesystem'); @@ -18,9 +25,10 @@ export default sendDataToLocaleStorage; export const getTemplate = async () => { try { const value = await AsyncStorage.getItem(ANKILAN_NOTE_TEMPLATE); + console.log(value); if (value !== null) { // value previously stored - return value; + return JSONfn.parse(value); } } catch (e) { // error reading value @@ -29,10 +37,10 @@ export const getTemplate = async () => { export const getCreator = async () => { try { - const value = await AsyncStorage.getItem(ANKILAN_NOTE_TEMPLATE); + const value = await AsyncStorage.getItem(ANKILAN_NOTE_CREATOR); if (value !== null) { // value previously stored - return value; + return JSONfn.parse(value); } } catch (e) { // error reading value diff --git a/src/actions/form-actions.js b/src/actions/form-actions.js index 6ad0143..9f6bf0a 100644 --- a/src/actions/form-actions.js +++ b/src/actions/form-actions.js @@ -1 +1,12 @@ +import {getCreator, getTemplate} from './filesystem'; +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/anki-model-values.js b/src/constants/anki-model-values.js new file mode 100644 index 0000000..e145dca --- /dev/null +++ b/src/constants/anki-model-values.js @@ -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 = ` +
{{Word or sentence}}
+ +
+
{{Transcription}}
+ {{Sound}} + `; + + const questionFmt2 = '
{{Translation 1}}
'; + + const questionFormat = [questionFmt1, questionFmt2]; + + const answerFmt1 = ` +
{{Word or sentence}}
+
+
{{Transcription}}
+ {{Sound}} +
+
+
{{Usage example}}
+
+ +
`; + 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; diff --git a/src/reducers/anki-reducer.js b/src/reducers/anki-reducer.js index 91042ff..5c4d4ee 100644 --- a/src/reducers/anki-reducer.js +++ b/src/reducers/anki-reducer.js @@ -20,7 +20,7 @@ const initialState = { mainFieldIsAvailable: false, fieldList: [], ankiLanModelIsAlreadyExists: false, - ankiLanModelName: 'AnkiLan_final1', + ankiLanModelName: '1AnkiLan1111', noteCreator: {}, noteTemplate: [], }; diff --git a/yarn.lock b/yarn.lock index 72eecc2..0bf8f72 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5026,6 +5026,11 @@ jsonfile@^4.0.0: optionalDependencies: 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: version "0.0.0" resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"