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.

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