2020-03-05 22:16:34 +00:00
|
|
|
import {AnkiDroid} from 'react-native-ankidroid/dist/ankidroid';
|
|
|
|
import {
|
|
|
|
CHECK_ANKI_ACCESS,
|
|
|
|
ERROR,
|
|
|
|
GET_DECK_LIST,
|
|
|
|
GET_FIELD_LIST,
|
|
|
|
GET_MODEL_LIST,
|
|
|
|
REQUEST_PERMISSIONS,
|
2020-03-07 19:38:01 +00:00
|
|
|
SET_EXISTING_OF_ANKI_LAN_MODEL,
|
2020-03-10 04:22:42 +00:00
|
|
|
SET_FIELD_LIST,
|
2020-03-05 22:16:34 +00:00
|
|
|
} from '../constants/anki-constants';
|
2020-03-07 19:38:01 +00:00
|
|
|
import {createAnkiLanModel} from './createAnkiLanModel';
|
2020-03-05 22:16:34 +00:00
|
|
|
|
2020-03-07 14:30:26 +00:00
|
|
|
/*Permissions*/
|
|
|
|
|
|
|
|
const setRequestAnkiPermissions = (err, res) => {
|
|
|
|
return err
|
|
|
|
? {type: ERROR, err}
|
|
|
|
: {type: REQUEST_PERMISSIONS, payload: res === 'granted'};
|
|
|
|
};
|
|
|
|
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) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2020-03-05 22:16:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const checkAnkiAccess = async (
|
|
|
|
ankiApiProvider = AnkiDroid.isApiAvailable,
|
|
|
|
) => {
|
|
|
|
const [err, res] = await ankiApiProvider();
|
|
|
|
return err ? {type: ERROR, err} : {type: CHECK_ANKI_ACCESS, payload: res};
|
|
|
|
};
|
|
|
|
|
2020-03-07 14:30:26 +00:00
|
|
|
/*Anki Data*/
|
|
|
|
|
|
|
|
const setDeckList = deckList => {
|
|
|
|
return {
|
|
|
|
type: GET_DECK_LIST,
|
|
|
|
payload: deckList,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getDeckList = () => async dispatch => {
|
|
|
|
try {
|
|
|
|
const [err, deckList] = await AnkiDroid.getDeckList();
|
|
|
|
if (err) {
|
|
|
|
console.log('you have an error');
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await dispatch(setDeckList(deckList));
|
|
|
|
return deckList;
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2020-03-05 22:16:34 +00:00
|
|
|
};
|
|
|
|
|
2020-03-08 07:18:34 +00:00
|
|
|
const setModelList = res => {
|
|
|
|
return {type: GET_MODEL_LIST, payload: res};
|
|
|
|
};
|
|
|
|
export const getModelList = () => async dispatch => {
|
|
|
|
try {
|
|
|
|
const [err, res] = await AnkiDroid.getModelList();
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await dispatch(setModelList(res));
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2020-03-05 22:16:34 +00:00
|
|
|
};
|
|
|
|
|
2020-03-10 04:22:42 +00:00
|
|
|
export const setFieldList = fieldList => ({
|
|
|
|
type: SET_FIELD_LIST,
|
|
|
|
payload: fieldList,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const getFieldList = name => async dispatch => {
|
|
|
|
try {
|
|
|
|
const [err, res] = await AnkiDroid.getFieldList(name);
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await dispatch(setFieldList(res));
|
|
|
|
return res;
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2020-03-05 22:16:34 +00:00
|
|
|
};
|
2020-03-07 14:30:26 +00:00
|
|
|
|
2020-03-07 19:38:01 +00:00
|
|
|
/*Checking*/
|
|
|
|
const setExistingOfAnkiLanModel = existing => {
|
|
|
|
return {
|
|
|
|
type: SET_EXISTING_OF_ANKI_LAN_MODEL,
|
|
|
|
payload: existing,
|
|
|
|
};
|
|
|
|
};
|
2020-03-08 11:17:42 +00:00
|
|
|
export const checkAnkiLanModelForExisting = (
|
|
|
|
name,
|
|
|
|
modelList,
|
|
|
|
) => async dispatch => {
|
2020-03-07 19:38:01 +00:00
|
|
|
try {
|
2020-03-28 07:41:34 +00:00
|
|
|
let id = 0;
|
2020-03-08 11:17:42 +00:00
|
|
|
for (let model of modelList) {
|
|
|
|
if (model.name === name) {
|
2020-03-28 07:41:34 +00:00
|
|
|
id = model.id;
|
2020-03-08 11:17:42 +00:00
|
|
|
await dispatch(setExistingOfAnkiLanModel(true));
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-07 19:38:01 +00:00
|
|
|
}
|
2020-03-08 11:17:42 +00:00
|
|
|
const err = 'Model not found. Displaying message...';
|
|
|
|
throw err;
|
2020-03-07 19:38:01 +00:00
|
|
|
} catch (err) {
|
2020-03-08 11:17:42 +00:00
|
|
|
console.log(err);
|
2020-03-07 19:38:01 +00:00
|
|
|
await dispatch(setExistingOfAnkiLanModel(false));
|
|
|
|
}
|
|
|
|
};
|
2020-03-28 07:41:34 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|