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.

61 lines
2.2 KiB

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