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.

123 lines
2.9 KiB

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