Dev #4

Merged
Horhik merged 18 commits from dev into master 2020-04-11 16:52:54 +00:00
9 changed files with 63 additions and 42 deletions
Showing only changes of commit 0962aa31b5 - Show all commits

View file

@ -105,8 +105,10 @@ export const checkAnkiLanModelForExisting = (
modelList,
) => async dispatch => {
try {
let id = 0;
for (let model of modelList) {
if (model.name === name) {
id = model.id;
await dispatch(setExistingOfAnkiLanModel(true));
return true;
}
@ -118,3 +120,16 @@ export const checkAnkiLanModelForExisting = (
await dispatch(setExistingOfAnkiLanModel(false));
}
};
export const getModelId = (models, name) => {
console.log(models, name);
let id = '';
models.forEach(model => {
if (model.name === name) {
console.log('id', model.id);
id = model.id;
return id;
}
});
return id;
};

View file

@ -14,6 +14,9 @@ import {
import {createDictionary} from '../dictionary/create-dictionary';
import {setFields} from '../anki-set-actions';
import {sendWord, submit} from '../form-actions';
import store from '../../store';
import {getModelId} from '../anki-get-actions';
import {AnkiDroid} from 'react-native-ankidroid/dist/ankidroid';
const getAvailableApi = (apiArray = []) => {
for (const api of apiArray) {
@ -52,6 +55,7 @@ export const wordInfo = word => async dispatch => {
dispatch(setDictioanry(wordDictionary));
/* TODO: move sendWord to submit function */
// sendWord(setFields(wordDictionary));
drawFields();
} catch (e) {
console.log(e);
}

View file

@ -1,6 +1,6 @@
import {AnkiDroid} from 'react-native-ankidroid/dist/ankidroid';
import {setAnkiNoteCreator, setCreatorTemplate} from './anki-set-actions';
import sendDataToLocaleStorage from './filesystem';
import sendDataToLocaleStorage, {getAnkiData} from './filesystem';
import setSettings, {
modelFields,
valueFields,
@ -8,6 +8,7 @@ import setSettings, {
import {
checkAnkiLanModelForExisting,
getFieldList,
getModelId,
getModelList,
} from './anki-get-actions';
@ -16,15 +17,15 @@ import store from '../store';
export const createAnkiLanModel = model => async dispatch => {
try {
const settings = setSettings(model);
const selectedDeck = new AnkiDroid(settings);
await dispatch(setAnkiNoteCreator(selectedDeck));
await dispatch(setCreatorTemplate(modelFields));
// const sd = JSON.parse(JSON.stringify(selectedDeck));
// console.log(sd === selectedDeck);
// ****************
alert('oh shit');
addNote(selectedDeck, valueFields, modelFields);
firstNote(selectedDeck, valueFields, modelFields);
// ****************
// sendDataToLocaleStorage(
// setAnkiNoteCreator(sd), //send creator to locale storage
@ -32,19 +33,31 @@ export const createAnkiLanModel = model => async dispatch => {
// );
checkAnkiLanModelForExisting(model.name, model.list);
await dispatch(getModelList());
await dispatch(getFieldList(model.name));
const [err, modelList] = await AnkiDroid.getModelList();
const modelId = await getModelId(modelList, model.name);
await console.log(modelId);
const [, fieldList] = await AnkiDroid.getFieldList(model.name);
sendDataToLocaleStorage({
fieldList,
modelName: model.name,
modelId,
});
} catch (err) {
console.log(err);
console.log('irror is ghere', err);
}
};
const firstNote = (creator, fields, template) =>
creator.addNote(fields, template);
//creator is an object what have to store in locale storage.
export const addNote = words => {
export const addNote = async words => {
const template = store.getState().anki.noteTemplate;
const deckId = store.getState().anki.selectedDeck.id;
const modelId = await getAnkiData().modelId;
const settings = {
deckId,
modelId: '1585139654585',
modelId,
};
const creator = new AnkiDroid(settings);

View file

@ -1,48 +1,33 @@
import AsyncStorage from '@react-native-community/async-storage';
import {
ANKILAN_DATA,
ANKILAN_NOTE_CREATOR,
ANKILAN_NOTE_TEMPLATE,
} from '../constants/anki-constants';
import JSONfn from 'jsonfn';
const sendDataToLocaleStorage = async (creator, template) => {
const sendDataToLocaleStorage = async data => {
try {
await AsyncStorage.clear();
await AsyncStorage.setItem(ANKILAN_NOTE_CREATOR, JSONfn.stringify(creator));
await AsyncStorage.setItem(
ANKILAN_NOTE_TEMPLATE,
JSONfn.stringify(template),
);
await AsyncStorage.setItem(ANKILAN_DATA, JSONfn.stringify(data));
console.log(data);
} catch (e) {
// saving error
alert('Error while syncing with filesystem');
console.log(e);
}
};
export const getAnkiData = async () => {
try {
const value = await AsyncStorage.getItem(ANKILAN_DATA);
if (value !== null) {
// value previously stored
return JSONfn.parse(value);
}
} catch (e) {
// error reading value
}
};
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 JSONfn.parse(value);
}
} catch (e) {
// error reading value
}
};
export const getCreator = async () => {
try {
const value = await AsyncStorage.getItem(ANKILAN_NOTE_CREATOR);
if (value !== null) {
// value previously stored
return JSONfn.parse(value);
}
} catch (e) {
// error reading value
}
};

View file

@ -0,0 +1,2 @@
const drawFields = () => {};
export default drawFields;

View file

@ -9,6 +9,7 @@ import {wordInfo} from "../actions/api/dictionary";
const AnkiForm = props => {
const [target, setTarget] = useState('')
const [fields, setFields] = useState({})
useEffect(() => {
// props.wordInfo('Urge');
// props.wordInfo('Maze');

View file

@ -5,7 +5,7 @@ import {Button, Text} from 'native-base';
const SubmitButton = props => {
return (
<Button onPress={props.onSubmit}>
<Text>Submit</Text>
<Text>Send</Text>
</Button>
);
};

View file

@ -26,3 +26,4 @@ export const SET_EXISTING_OF_ANKI_LAN_MODEL = 'SET_EXISTING_OF_ANKI_LAN_MODEL';
//rn-async-storage kesy
export const ANKILAN_NOTE_CREATOR = '@ANKILAN_NOTE_CREATOR';
export const ANKILAN_NOTE_TEMPLATE = '@ANKILAN_NOTE_TEMPLATE';
export const ANKILAN_DATA = 'ANKILAN_DATA';

View file

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