From c9230f7254b7e63213c1ee2db2cd6dd838b518d7 Mon Sep 17 00:00:00 2001 From: horhik Date: Sat, 7 Mar 2020 14:38:01 -0500 Subject: [PATCH] create model template --- App.js | 2 +- src/actions/anki-get-actions.js | 26 ++++- src/actions/createAnkiLanModel.js | 101 ++++++++++++++++++ ...ain-component.jsx => add-anklan-model.jsx} | 5 +- src/components/add-word-form.jsx | 20 +++- src/components/permissions.js | 15 ++- src/components/view/add-main-template.jsx | 30 ++++++ src/components/view/deck-picker.jsx | 11 +- src/constants/anki-constants.js | 2 + src/reducers/anki-reducer.js | 8 +- 10 files changed, 199 insertions(+), 21 deletions(-) create mode 100644 src/actions/createAnkiLanModel.js rename src/components/{main-component.jsx => add-anklan-model.jsx} (75%) create mode 100644 src/components/view/add-main-template.jsx diff --git a/App.js b/App.js index 658fb72..7b346e3 100644 --- a/App.js +++ b/App.js @@ -1,6 +1,6 @@ import React from 'react'; import {Provider} from 'react-redux'; -import StartScreen from './src/components/main-component'; +import StartScreen from './src/components/add-anklan-model'; import store from './src/store'; const App = props => { return ( diff --git a/src/actions/anki-get-actions.js b/src/actions/anki-get-actions.js index 38cfce0..4036b23 100644 --- a/src/actions/anki-get-actions.js +++ b/src/actions/anki-get-actions.js @@ -6,8 +6,9 @@ import { GET_FIELD_LIST, GET_MODEL_LIST, REQUEST_PERMISSIONS, + SET_EXISTING_OF_ANKI_LAN_MODEL, } from '../constants/anki-constants'; -import {getPermissionName} from 'react-native-ankidroid/dist/utilities'; +import {createAnkiLanModel} from './createAnkiLanModel'; /*Permissions*/ @@ -74,4 +75,25 @@ export const getFieldList = async ( return err ? {type: ERROR, err} : {type: GET_FIELD_LIST, payload: res}; }; -export const dd = () => {}; +/*Checking*/ +const setExistingOfAnkiLanModel = existing => { + return { + type: SET_EXISTING_OF_ANKI_LAN_MODEL, + payload: existing, + }; +}; +export const checkAnkiLanModelForExisting = id => async dispatch => { + try { + const [err, res] = await AnkiDroid.getFieldList('', id); + if (err) { + throw err; + } + console.log(res); + await dispatch(setExistingOfAnkiLanModel(true)); + return res; + } catch (err) { + await dispatch(setExistingOfAnkiLanModel(false)); + await createAnkiLanModel(id); + console.log('errrororo', err); + } +}; diff --git a/src/actions/createAnkiLanModel.js b/src/actions/createAnkiLanModel.js new file mode 100644 index 0000000..d8bd678 --- /dev/null +++ b/src/actions/createAnkiLanModel.js @@ -0,0 +1,101 @@ +import {AnkiDroid} from 'react-native-ankidroid/dist/ankidroid'; + +export const createAnkiLanModel = model => async dispatch => { + try { + const modelFields = [ + 'Word or sentence', + 'Part of speech 1', + 'Translate 1', + 'Definition 1', + 'Part of speech 2', + 'Translate 2', + 'Definition 2', + 'Example', + 'Transcription', + 'Sound', + ]; + + const css = ``; + + const answerFormat = ` + {{FrontSide}} + +
+ +
    +
  1. +
    {{Translate 1}}
    +
    {{Definition 1}}
    +
  2. + +
  3. +
    {{Translate 2}}
    +
    {{Definition 2}}
    +
  4. +
+

{Example}}

\`; +{{Sound}} + `; + const questionFormat = ` + {{Word or sentence}} +
+
{{Transcription}}
+
+
    +
  1. +
    {{Part of speech 1}}
    +
  2. +
  3. +
    {{Part of speech 2}}
    +
  4. +
+ {{Sound}}`; + const modelProperties = { + name: model.name, + reference: 'com.ankilan.models', + fields: modelFields, + tags: [], + cardNames: ['Rus->En', 'En->Rus'], + questionFormat, + answerFormat, + css, + }; + const settings = { + modelId: model.id, + modelProperties, + deckId: '1582019808117', + }; + const currentDeck = new AnkiDroid(settings); + currentDeck.addNote( + [ + 'Word or sentence', + 'Part of speech 1', + 'Translate 1', + 'Definition 1', + 'Part of speech 2', + 'Translate 2', + 'Definition 2', + 'Example', + 'Transcription', + 'Sound', + ], + modelFields, + ); + } catch (err) { + console.log(err); + } +}; diff --git a/src/components/main-component.jsx b/src/components/add-anklan-model.jsx similarity index 75% rename from src/components/main-component.jsx rename to src/components/add-anklan-model.jsx index 8f51bba..364b224 100644 --- a/src/components/main-component.jsx +++ b/src/components/add-anklan-model.jsx @@ -5,12 +5,13 @@ import Permissions from './permissions'; import {connect, Provider} from 'react-redux'; import DeckPicker from './view/deck-picker'; import AddWordForm from './add-word-form'; +import AnkiTemplate from './view/add-main-template'; +import {Grid, Row} from 'native-base'; const StartScreen = props => { return ( - {!props.ankiAvailable? : null} - + {props.ankiAvailable? : } ) }; diff --git a/src/components/add-word-form.jsx b/src/components/add-word-form.jsx index 21e5daf..0b3cd29 100644 --- a/src/components/add-word-form.jsx +++ b/src/components/add-word-form.jsx @@ -1,14 +1,24 @@ import React from 'react' import {connect} from 'react-redux' import DeckPicker from './view/deck-picker'; -import{Form} from 'native-base'; +import{Form, Container} from 'native-base'; +import AnkiTemplate from './view/add-main-template'; +import {ScrollView} from 'react-native'; const AddWordForm = props => { return ( -
- - + + {props.ankiLanModelExists ? +
+ + + : + + } +
) } -export default connect()(AddWordForm) +export default connect(state => ({ + ankiLanModelExists: state.anki.ankiLanModelIsAlreadyExists +}))(AddWordForm) diff --git a/src/components/permissions.js b/src/components/permissions.js index 8b2c6a8..24da63a 100644 --- a/src/components/permissions.js +++ b/src/components/permissions.js @@ -1,5 +1,5 @@ import React, {useState, useEffect} from 'react'; -import {Text, Button} from 'native-base'; +import {Text, Button, Grid} from 'native-base'; import {connect} from 'react-redux'; import {requestAnkiPermission} from '../actions/anki-get-actions'; const Permissions = props => { @@ -7,9 +7,16 @@ const Permissions = props => { props.requestAnkiPermission(); }, []); return ( - + + + ); }; export default connect( diff --git a/src/components/view/add-main-template.jsx b/src/components/view/add-main-template.jsx new file mode 100644 index 0000000..044d89f --- /dev/null +++ b/src/components/view/add-main-template.jsx @@ -0,0 +1,30 @@ +import React, {useEffect} from 'react' +import {Text, Button,Grid, Container} from 'native-base'; +import {checkAnkiLanModelForExisting} from '../../actions/anki-get-actions'; +import {connect} from 'react-redux' +import {createAnkiLanModel} from '../../actions/createAnkiLanModel'; + +const AnkiTemplate = props => { + useEffect(() => { + props.checkAnkiLanModelForExisting(props.id) + }, []) + return( + + You have no AnkiLan card template + + + + ) + +} + +export default connect(state => ({ + modelId: state.anki.ankiLanModelID, + modelName: state.anki.ankiLanModelName +}), { + createAnkiLanModel, + checkAnkiLanModelForExisting +})(AnkiTemplate) diff --git a/src/components/view/deck-picker.jsx b/src/components/view/deck-picker.jsx index aab74dd..be9b43e 100644 --- a/src/components/view/deck-picker.jsx +++ b/src/components/view/deck-picker.jsx @@ -5,16 +5,15 @@ import { getDeckList} from '../../actions/anki-get-actions'; import {selectDeck} from '../../actions/anki-set-actions'; const DeckPicker = props => { - const [deckList, setDeckList] = useState([{name: "no decks", id:"666"}]) - const [selectedDeck, setSelectedDeck] = useState('1') + const [deckList, setDeckList] = useState([{name: "no decks", id: 0}]); useEffect(() => { props.getDeckList() setDeckList(props.decks) }, []) return ( - setSelectedDeck(id)} selectedValue={selectedDeck} > - {deckList.map(deck => ( - + props.selectDeck(id)} selectedValue={props.selectedDeck} > + {deckList.map((deck, index) => ( + ))} ) @@ -25,5 +24,5 @@ export default connect(state => ({ selectedDeck: state.anki.selectedDeck }),{ getDeckList, - setDeck: selectDeck + selectDeck })(DeckPicker) diff --git a/src/constants/anki-constants.js b/src/constants/anki-constants.js index c89ead6..046d3e6 100644 --- a/src/constants/anki-constants.js +++ b/src/constants/anki-constants.js @@ -16,3 +16,5 @@ export const SET_WORD_SOUND = 'SET_WORD_SOUND'; export const SET_WORD_TRANSLATE = 'SET_WORD_TRANSLATE'; //Anki ui actions export const SHOW_FIELDS = 'SHOW_FIELDS'; +// Anki check actions +export const SET_EXISTING_OF_ANKI_LAN_MODEL = 'SET_EXISTING_OF_ANKI_LAN_MODEL'; diff --git a/src/reducers/anki-reducer.js b/src/reducers/anki-reducer.js index 3bcd247..a30c850 100644 --- a/src/reducers/anki-reducer.js +++ b/src/reducers/anki-reducer.js @@ -2,15 +2,19 @@ import { GET_DECK_LIST, REQUEST_PERMISSIONS, SET_DECK, + SET_EXISTING_OF_ANKI_LAN_MODEL, } from '../constants/anki-constants'; const initialState = { isApiAvailable: false, appHasAccess: false, deckList: [], - selectedDeck: '0', + selectedDeck: '1', mainFieldIsAvailable: false, fieldList: [], + ankiLanModelIsAlreadyExists: false, + ankiLanModelID: '7410448765670', + ankiLanModelName: 'AnkiLan', }; const ankiReducer = (state = initialState, action) => { @@ -22,6 +26,8 @@ const ankiReducer = (state = initialState, action) => { return {...state, deckList: action.payload}; case SET_DECK: return {...state, selectedDeck: action.payload}; + case SET_EXISTING_OF_ANKI_LAN_MODEL: + return {...state, ankiLanModelIsAlreadyExists: action.payload}; default: return state; }