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_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. console.error(error);
  41. dispatch(submitComposeFail(error));
  42. });
  43. };
  44. };
  45. export function submitComposeRequest() {
  46. return {
  47. type: COMPOSE_SUBMIT_REQUEST
  48. };
  49. };
  50. export function submitComposeSuccess(status) {
  51. return {
  52. type: COMPOSE_SUBMIT_SUCCESS,
  53. status: status
  54. };
  55. };
  56. export function submitComposeFail(error) {
  57. return {
  58. type: COMPOSE_SUBMIT_FAIL,
  59. error: error
  60. };
  61. };
  62. export function uploadCompose(files) {
  63. return function (dispatch, getState) {
  64. dispatch(uploadComposeRequest());
  65. let data = new FormData();
  66. data.append('file', files[0]);
  67. api(getState).post('/api/v1/media', data, {
  68. onUploadProgress: function (e) {
  69. dispatch(uploadComposeProgress(e.loaded, e.total));
  70. }
  71. }).then(function (response) {
  72. dispatch(uploadComposeSuccess(response.data));
  73. }).catch(function (error) {
  74. console.error(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. };