You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.3 KiB

  1. import {
  2. COMPOSE_CHANGE,
  3. COMPOSE_REPLY,
  4. COMPOSE_REPLY_CANCEL,
  5. COMPOSE_SUBMIT_REQUEST,
  6. COMPOSE_SUBMIT_SUCCESS,
  7. COMPOSE_SUBMIT_FAIL,
  8. COMPOSE_UPLOAD_REQUEST,
  9. COMPOSE_UPLOAD_SUCCESS,
  10. COMPOSE_UPLOAD_FAIL,
  11. COMPOSE_UPLOAD_UNDO,
  12. COMPOSE_UPLOAD_PROGRESS
  13. } from '../actions/compose';
  14. import { TIMELINE_DELETE } from '../actions/timelines';
  15. import Immutable from 'immutable';
  16. const initialState = Immutable.Map({
  17. text: '',
  18. in_reply_to: null,
  19. is_submitting: false,
  20. is_uploading: false,
  21. progress: 0,
  22. media_attachments: Immutable.List([])
  23. });
  24. export default function compose(state = initialState, action) {
  25. switch(action.type) {
  26. case COMPOSE_CHANGE:
  27. return state.set('text', action.text);
  28. case COMPOSE_REPLY:
  29. return state.withMutations(map => {
  30. map.set('in_reply_to', action.status.get('id'));
  31. map.set('text', `@${action.status.getIn(['account', 'acct'])} `);
  32. });
  33. case COMPOSE_REPLY_CANCEL:
  34. return state.withMutations(map => {
  35. map.set('in_reply_to', null);
  36. map.set('text', '');
  37. });
  38. case COMPOSE_SUBMIT_REQUEST:
  39. return state.set('is_submitting', true);
  40. case COMPOSE_SUBMIT_SUCCESS:
  41. return state.withMutations(map => {
  42. map.set('text', '');
  43. map.set('is_submitting', false);
  44. map.set('in_reply_to', null);
  45. map.update('media_attachments', list => list.clear());
  46. });
  47. case COMPOSE_SUBMIT_FAIL:
  48. return state.set('is_submitting', false);
  49. case COMPOSE_UPLOAD_REQUEST:
  50. return state.set('is_uploading', true);
  51. case COMPOSE_UPLOAD_SUCCESS:
  52. return state.withMutations(map => {
  53. map.update('media_attachments', list => list.push(Immutable.fromJS(action.media)));
  54. map.set('is_uploading', false);
  55. });
  56. case COMPOSE_UPLOAD_FAIL:
  57. return state.set('is_uploading', false);
  58. case COMPOSE_UPLOAD_UNDO:
  59. return state.update('media_attachments', list => list.filterNot(item => item.get('id') === action.media_id));
  60. case COMPOSE_UPLOAD_PROGRESS:
  61. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  62. case TIMELINE_DELETE:
  63. if (action.id === state.get('in_reply_to')) {
  64. return state.set('in_reply_to', null);
  65. } else {
  66. return state;
  67. }
  68. default:
  69. return state;
  70. }
  71. };