Ankilan/src/reducers/anki-reducer.js

129 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-03-07 14:30:26 +00:00
import {
DEF_LIST1,
DEF_LIST2,
EXAMPLES,
2020-03-07 14:30:26 +00:00
GET_DECK_LIST,
2020-03-08 07:18:34 +00:00
GET_MODEL_LIST,
PRONUNCIATION,
2020-03-07 14:30:26 +00:00
REQUEST_PERMISSIONS,
SEND_FIELD,
2020-04-01 13:16:29 +00:00
SET_ANKI_DATA,
2020-03-08 11:17:42 +00:00
SET_ANKI_NOTE_CREATOR,
SET_CREATOR_TEMPLATE,
2020-03-07 14:30:26 +00:00
SET_DECK,
2020-03-07 19:38:01 +00:00
SET_EXISTING_OF_ANKI_LAN_MODEL,
SOUND,
WORD,
2020-03-07 14:30:26 +00:00
} from '../constants/anki-constants';
2020-03-05 22:16:34 +00:00
const initialState = {
isApiAvailable: false,
2020-03-07 14:30:26 +00:00
appHasAccess: false,
2020-03-05 22:16:34 +00:00
deckList: [],
2020-03-08 07:18:34 +00:00
modelList: [],
selectedDeck: {
id: '1',
deck: Object,
},
2020-03-05 22:16:34 +00:00
mainFieldIsAvailable: false,
fieldList: [],
2020-03-07 19:38:01 +00:00
ankiLanModelIsAlreadyExists: false,
ankiLanModelName: 'develop_final_maybe_maybe_maybe',
2020-03-08 11:17:42 +00:00
noteCreator: {},
noteTemplate: [],
currentFields: {
word: '',
compounded: [
{pos: '', tr: '', definition: ''},
{pos: '', tr: '', definition: ''},
],
example: '',
pronunciation: '',
sound: '',
},
2020-04-01 13:16:29 +00:00
savedData: {},
2020-03-05 22:16:34 +00:00
};
const ankiReducer = (state = initialState, action) => {
switch (action.type) {
2020-03-07 14:30:26 +00:00
case REQUEST_PERMISSIONS:
return {...state, appHasAccess: action.payload};
case GET_DECK_LIST:
return {...state, deckList: action.payload};
2020-03-08 07:18:34 +00:00
case GET_MODEL_LIST:
return {...state, modelList: action.payload};
2020-03-07 14:30:26 +00:00
case SET_DECK:
2020-03-08 07:18:34 +00:00
return {
...state,
selectedDeck: {...state.selectedDeck, ...action.payload},
};
2020-03-07 19:38:01 +00:00
case SET_EXISTING_OF_ANKI_LAN_MODEL:
return {...state, ankiLanModelIsAlreadyExists: action.payload};
2020-03-08 11:17:42 +00:00
case SET_CREATOR_TEMPLATE:
return {...state, noteTemplate: action.payload};
case SET_ANKI_NOTE_CREATOR:
return {...state, noteCreator: action.payload};
2020-04-01 13:16:29 +00:00
case SET_ANKI_DATA:
return {
...state,
savedData: action.payload,
};
case SEND_FIELD: {
const fields = state.currentFields;
switch (action.role) {
case EXAMPLES: {
return {
...state,
currentFields: {...state.currentFields, example: action.payload},
};
}
case SOUND: {
return {
...state,
currentFields: {...state.currentFields, sound: action.payload},
};
}
case PRONUNCIATION: {
return {
...state,
currentFields: {
...state.currentFields,
pronunciation: action.payload,
},
};
}
case DEF_LIST1: {
console.log(action.payload);
console.log(action.role);
return {
...state,
currentFields: {
...state.currentFields,
compounded: [action.payload, state.currentFields.compounded[1]],
},
};
}
case DEF_LIST2: {
return {
...state,
currentFields: {
...state.currentFields,
compounded: [state.currentFields.compounded[0], action.payload],
},
};
}
case WORD: {
alert(action.payload);
return {
...state,
currentFields: {...state.currentFields, word: action.payload},
};
}
}
}
2020-03-05 22:16:34 +00:00
default:
return state;
}
};
export default ankiReducer;