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.

125 lines
3.0 KiB

  1. import api from '../api'
  2. export const COMPOSE_CHANGE = 'COMPOSE_CHANGE';
  3. export const COMPOSE_SUBMIT = 'COMPOSE_SUBMIT';
  4. export const COMPOSE_SUBMIT_REQUEST = 'COMPOSE_SUBMIT_REQUEST';
  5. export const COMPOSE_SUBMIT_SUCCESS = 'COMPOSE_SUBMIT_SUCCESS';
  6. export const COMPOSE_SUBMIT_FAIL = 'COMPOSE_SUBMIT_FAIL';
  7. export const COMPOSE_REPLY = 'COMPOSE_REPLY';
  8. export const COMPOSE_REPLY_CANCEL = 'COMPOSE_REPLY_CANCEL';
  9. export const COMPOSE_UPLOAD = 'COMPOSE_UPLOAD';
  10. export const COMPOSE_UPLOAD_REQUEST = 'COMPOSE_UPLOAD_REQUEST';
  11. export const COMPOSE_UPLOAD_SUCCESS = 'COMPOSE_UPLOAD_SUCCESS';
  12. export const COMPOSE_UPLOAD_FAIL = 'COMPOSE_UPLOAD_FAIL';
  13. export const COMPOSE_UPLOAD_PROGRESS = 'COMPOSE_UPLOAD_PROGRESS';
  14. export const COMPOSE_UPLOAD_UNDO = 'COMPOSE_UPLOAD_UNDO';
  15. export function changeCompose(text) {
  16. return {
  17. type: COMPOSE_CHANGE,
  18. text: text
  19. };
  20. }
  21. export function replyCompose(status) {
  22. return {
  23. type: COMPOSE_REPLY,
  24. status: status
  25. };
  26. }
  27. export function cancelReplyCompose() {
  28. return {
  29. type: COMPOSE_REPLY_CANCEL
  30. };
  31. }
  32. export function submitCompose() {
  33. return function (dispatch, getState) {
  34. dispatch(submitComposeRequest());
  35. api(getState).post('/api/statuses', {
  36. status: getState().getIn(['compose', 'text'], ''),
  37. in_reply_to_id: getState().getIn(['compose', 'in_reply_to'], null),
  38. media_ids: getState().getIn(['compose', 'media_attachments']).map(item => item.get('id'))
  39. }).then(function (response) {
  40. dispatch(submitComposeSuccess(response.data));
  41. }).catch(function (error) {
  42. dispatch(submitComposeFail(error));
  43. });
  44. };
  45. }
  46. export function submitComposeRequest() {
  47. return {
  48. type: COMPOSE_SUBMIT_REQUEST
  49. };
  50. }
  51. export function submitComposeSuccess(status) {
  52. return {
  53. type: COMPOSE_SUBMIT_SUCCESS,
  54. status: status
  55. };
  56. }
  57. export function submitComposeFail(error) {
  58. return {
  59. type: COMPOSE_SUBMIT_FAIL,
  60. error: error
  61. };
  62. }
  63. export function uploadCompose(files) {
  64. return function (dispatch, getState) {
  65. dispatch(uploadComposeRequest());
  66. let data = new FormData();
  67. data.append('file', files[0]);
  68. api(getState).post('/api/media', data, {
  69. onUploadProgress: function (e) {
  70. dispatch(uploadComposeProgress(e.loaded, e.total));
  71. }
  72. }).then(function (response) {
  73. dispatch(uploadComposeSuccess(response.data));
  74. }).catch(function (error) {
  75. dispatch(uploadComposeFail(error));
  76. });
  77. };
  78. }
  79. export function uploadComposeRequest() {
  80. return {
  81. type: COMPOSE_UPLOAD_REQUEST
  82. };
  83. }
  84. export function uploadComposeProgress(loaded, total) {
  85. return {
  86. type: COMPOSE_UPLOAD_PROGRESS,
  87. loaded: loaded,
  88. total: total
  89. };
  90. }
  91. export function uploadComposeSuccess(media) {
  92. return {
  93. type: COMPOSE_UPLOAD_SUCCESS,
  94. media: media
  95. };
  96. }
  97. export function uploadComposeFail(error) {
  98. return {
  99. type: COMPOSE_UPLOAD_FAIL,
  100. error: error
  101. };
  102. }
  103. export function undoUploadCompose(media_id) {
  104. return {
  105. type: COMPOSE_UPLOAD_UNDO,
  106. media_id: media_id
  107. };
  108. }