reports.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import {
  2. REPORT_INIT,
  3. REPORT_SUBMIT_REQUEST,
  4. REPORT_SUBMIT_SUCCESS,
  5. REPORT_SUBMIT_FAIL,
  6. REPORT_CANCEL,
  7. REPORT_STATUS_TOGGLE,
  8. REPORT_COMMENT_CHANGE
  9. } from '../actions/reports';
  10. import Immutable from 'immutable';
  11. const initialState = Immutable.Map({
  12. new: Immutable.Map({
  13. isSubmitting: false,
  14. account_id: null,
  15. status_ids: Immutable.Set(),
  16. comment: ''
  17. })
  18. });
  19. export default function reports(state = initialState, action) {
  20. switch(action.type) {
  21. case REPORT_INIT:
  22. return state.withMutations(map => {
  23. map.setIn(['new', 'isSubmitting'], false);
  24. map.setIn(['new', 'account_id'], action.account.get('id'));
  25. if (state.getIn(['new', 'account_id']) !== action.account.get('id')) {
  26. map.setIn(['new', 'status_ids'], action.status ? Immutable.Set([action.status.getIn(['reblog', 'id'], action.status.get('id'))]) : Immutable.Set());
  27. map.setIn(['new', 'comment'], '');
  28. } else {
  29. map.updateIn(['new', 'status_ids'], Immutable.Set(), set => set.add(action.status.getIn(['reblog', 'id'], action.status.get('id'))));
  30. }
  31. });
  32. case REPORT_STATUS_TOGGLE:
  33. return state.updateIn(['new', 'status_ids'], Immutable.Set(), set => {
  34. if (action.checked) {
  35. return set.add(action.statusId);
  36. }
  37. return set.remove(action.statusId);
  38. });
  39. case REPORT_COMMENT_CHANGE:
  40. return state.setIn(['new', 'comment'], action.comment);
  41. case REPORT_SUBMIT_REQUEST:
  42. return state.setIn(['new', 'isSubmitting'], true);
  43. case REPORT_SUBMIT_FAIL:
  44. return state.setIn(['new', 'isSubmitting'], false);
  45. case REPORT_CANCEL:
  46. case REPORT_SUBMIT_SUCCESS:
  47. return state.withMutations(map => {
  48. map.setIn(['new', 'account_id'], null);
  49. map.setIn(['new', 'status_ids'], Immutable.Set());
  50. map.setIn(['new', 'comment'], '');
  51. map.setIn(['new', 'isSubmitting'], false);
  52. });
  53. default:
  54. return state;
  55. }
  56. };