polls.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { Map as ImmutableMap, fromJS } from 'immutable';
  2. import { POLLS_IMPORT } from 'mastodon/actions/importer';
  3. import { normalizePollOptionTranslation } from '../actions/importer/normalizer';
  4. import { STATUS_TRANSLATE_SUCCESS, STATUS_TRANSLATE_UNDO } from '../actions/statuses';
  5. const importPolls = (state, polls) => state.withMutations(map => polls.forEach(poll => map.set(poll.id, fromJS(poll))));
  6. const statusTranslateSuccess = (state, pollTranslation) => {
  7. return state.withMutations(map => {
  8. if (pollTranslation) {
  9. const poll = state.get(pollTranslation.id);
  10. pollTranslation.options.forEach((item, index) => {
  11. map.setIn([pollTranslation.id, 'options', index, 'translation'], fromJS(normalizePollOptionTranslation(item, poll)));
  12. });
  13. }
  14. });
  15. };
  16. const statusTranslateUndo = (state, id) => {
  17. return state.withMutations(map => {
  18. const options = map.getIn([id, 'options']);
  19. if (options) {
  20. options.forEach((item, index) => map.deleteIn([id, 'options', index, 'translation']));
  21. }
  22. });
  23. };
  24. const initialState = ImmutableMap();
  25. export default function polls(state = initialState, action) {
  26. switch(action.type) {
  27. case POLLS_IMPORT:
  28. return importPolls(state, action.polls);
  29. case STATUS_TRANSLATE_SUCCESS:
  30. return statusTranslateSuccess(state, action.translation.poll);
  31. case STATUS_TRANSLATE_UNDO:
  32. return statusTranslateUndo(state, action.pollId);
  33. default:
  34. return state;
  35. }
  36. }