Ankilan/src/reducers/anki-reducer.js

138 lines
3.3 KiB
JavaScript

import {
DEF_LIST1,
DEF_LIST2,
EXAMPLES,
GET_DECK_LIST,
GET_MODEL_LIST,
PRONUNCIATION,
REQUEST_PERMISSIONS,
SELECT_POS_ID,
SEND_FIELD,
SET_ANKI_DATA,
SET_ANKI_NOTE_CREATOR,
SET_CREATOR_TEMPLATE,
SET_DECK,
SET_EXISTING_OF_ANKI_LAN_MODEL,
SOUND,
WORD,
} from '../constants/anki-constants';
const initialState = {
isApiAvailable: false,
appHasAccess: false,
deckList: [],
modelList: [],
selectedDeck: {
id: '1',
deck: Object,
},
mainFieldIsAvailable: false,
fieldList: [],
ankiLanModelIsAlreadyExists: false,
ankiLanModelName: 'develop_final_maybe_maybe_maybe',
noteCreator: {},
noteTemplate: [],
pos1id: 0,
pos2id: 0,
currentFields: {
word: '',
compounded: [
{pos: '', tr: '', definition: ''},
{pos: '', tr: '', definition: ''},
],
example: '',
pronunciation: '',
sound: '',
},
savedData: {},
};
const ankiReducer = (state = initialState, action) => {
switch (action.type) {
case REQUEST_PERMISSIONS:
return {...state, appHasAccess: action.payload};
case GET_DECK_LIST:
return {...state, deckList: action.payload};
case GET_MODEL_LIST:
return {...state, modelList: action.payload};
case SET_DECK:
return {
...state,
selectedDeck: {...state.selectedDeck, ...action.payload},
};
case SET_EXISTING_OF_ANKI_LAN_MODEL:
return {...state, ankiLanModelIsAlreadyExists: action.payload};
case SET_CREATOR_TEMPLATE:
return {...state, noteTemplate: action.payload};
case SET_ANKI_NOTE_CREATOR:
return {...state, noteCreator: action.payload};
case SET_ANKI_DATA:
return {
...state,
savedData: action.payload,
};
case SELECT_POS_ID: {
console.log('click', action.id);
return action.id == 1
? {...state, pos2id: action.payload}
: {...state, pos1id: 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: {
return {
...state,
currentFields: {...state.currentFields, word: action.payload},
};
}
}
}
default:
return state;
}
};
export default ankiReducer;