2019-03-03 23:18:23 +02:00
|
|
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
|
|
|
|
2023-05-28 17:38:10 +03:00
|
|
|
import { POLLS_IMPORT } from 'flavours/glitch/actions/importer';
|
|
|
|
|
2023-06-01 01:10:21 +03:00
|
|
|
import { normalizePollOptionTranslation } from '../actions/importer/normalizer';
|
|
|
|
import { STATUS_TRANSLATE_SUCCESS, STATUS_TRANSLATE_UNDO } from '../actions/statuses';
|
|
|
|
|
2019-03-03 23:18:23 +02:00
|
|
|
const importPolls = (state, polls) => state.withMutations(map => polls.forEach(poll => map.set(poll.id, fromJS(poll))));
|
|
|
|
|
2023-06-01 01:10:21 +03:00
|
|
|
const statusTranslateSuccess = (state, pollTranslation) => {
|
|
|
|
return state.withMutations(map => {
|
|
|
|
if (pollTranslation) {
|
|
|
|
const poll = state.get(pollTranslation.id);
|
|
|
|
|
|
|
|
pollTranslation.options.forEach((item, index) => {
|
|
|
|
map.setIn([pollTranslation.id, 'options', index, 'translation'], fromJS(normalizePollOptionTranslation(item, poll)));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const statusTranslateUndo = (state, id) => {
|
|
|
|
return state.withMutations(map => {
|
|
|
|
const options = map.getIn([id, 'options']);
|
|
|
|
|
|
|
|
if (options) {
|
|
|
|
options.forEach((item, index) => map.deleteIn([id, 'options', index, 'translation']));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-03-03 23:18:23 +02:00
|
|
|
const initialState = ImmutableMap();
|
|
|
|
|
|
|
|
export default function polls(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
|
|
|
case POLLS_IMPORT:
|
|
|
|
return importPolls(state, action.polls);
|
2023-06-01 01:10:21 +03:00
|
|
|
case STATUS_TRANSLATE_SUCCESS:
|
|
|
|
return statusTranslateSuccess(state, action.translation.poll);
|
|
|
|
case STATUS_TRANSLATE_UNDO:
|
|
|
|
return statusTranslateUndo(state, action.pollId);
|
2019-03-03 23:18:23 +02:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|