interactions.jsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import api from '../api'
  2. export const REBLOG_REQUEST = 'REBLOG_REQUEST';
  3. export const REBLOG_SUCCESS = 'REBLOG_SUCCESS';
  4. export const REBLOG_FAIL = 'REBLOG_FAIL';
  5. export const FAVOURITE_REQUEST = 'FAVOURITE_REQUEST';
  6. export const FAVOURITE_SUCCESS = 'FAVOURITE_SUCCESS';
  7. export const FAVOURITE_FAIL = 'FAVOURITE_FAIL';
  8. export const UNREBLOG_REQUEST = 'UNREBLOG_REQUEST';
  9. export const UNREBLOG_SUCCESS = 'UNREBLOG_SUCCESS';
  10. export const UNREBLOG_FAIL = 'UNREBLOG_FAIL';
  11. export const UNFAVOURITE_REQUEST = 'UNFAVOURITE_REQUEST';
  12. export const UNFAVOURITE_SUCCESS = 'UNFAVOURITE_SUCCESS';
  13. export const UNFAVOURITE_FAIL = 'UNFAVOURITE_FAIL';
  14. export const REBLOGS_FETCH_REQUEST = 'REBLOGS_FETCH_REQUEST';
  15. export const REBLOGS_FETCH_SUCCESS = 'REBLOGS_FETCH_SUCCESS';
  16. export const REBLOGS_FETCH_FAIL = 'REBLOGS_FETCH_FAIL';
  17. export const FAVOURITES_FETCH_REQUEST = 'FAVOURITES_FETCH_REQUEST';
  18. export const FAVOURITES_FETCH_SUCCESS = 'FAVOURITES_FETCH_SUCCESS';
  19. export const FAVOURITES_FETCH_FAIL = 'FAVOURITES_FETCH_FAIL';
  20. export function reblog(status) {
  21. return function (dispatch, getState) {
  22. dispatch(reblogRequest(status));
  23. api(getState).post(`/api/v1/statuses/${status.get('id')}/reblog`).then(function (response) {
  24. // The reblog API method returns a new status wrapped around the original. In this case we are only
  25. // interested in how the original is modified, hence passing it skipping the wrapper
  26. dispatch(reblogSuccess(status, response.data.reblog));
  27. }).catch(function (error) {
  28. dispatch(reblogFail(status, error));
  29. });
  30. };
  31. };
  32. export function unreblog(status) {
  33. return (dispatch, getState) => {
  34. dispatch(unreblogRequest(status));
  35. api(getState).post(`/api/v1/statuses/${status.get('id')}/unreblog`).then(response => {
  36. dispatch(unreblogSuccess(status, response.data));
  37. }).catch(error => {
  38. dispatch(unreblogFail(status, error));
  39. });
  40. };
  41. };
  42. export function reblogRequest(status) {
  43. return {
  44. type: REBLOG_REQUEST,
  45. status: status
  46. };
  47. };
  48. export function reblogSuccess(status, response) {
  49. return {
  50. type: REBLOG_SUCCESS,
  51. status: status,
  52. response: response
  53. };
  54. };
  55. export function reblogFail(status, error) {
  56. return {
  57. type: REBLOG_FAIL,
  58. status: status,
  59. error: error
  60. };
  61. };
  62. export function unreblogRequest(status) {
  63. return {
  64. type: UNREBLOG_REQUEST,
  65. status: status
  66. };
  67. };
  68. export function unreblogSuccess(status, response) {
  69. return {
  70. type: UNREBLOG_SUCCESS,
  71. status: status,
  72. response: response
  73. };
  74. };
  75. export function unreblogFail(status, error) {
  76. return {
  77. type: UNREBLOG_FAIL,
  78. status: status,
  79. error: error
  80. };
  81. };
  82. export function favourite(status) {
  83. return function (dispatch, getState) {
  84. dispatch(favouriteRequest(status));
  85. api(getState).post(`/api/v1/statuses/${status.get('id')}/favourite`).then(function (response) {
  86. dispatch(favouriteSuccess(status, response.data));
  87. }).catch(function (error) {
  88. dispatch(favouriteFail(status, error));
  89. });
  90. };
  91. };
  92. export function unfavourite(status) {
  93. return (dispatch, getState) => {
  94. dispatch(unfavouriteRequest(status));
  95. api(getState).post(`/api/v1/statuses/${status.get('id')}/unfavourite`).then(response => {
  96. dispatch(unfavouriteSuccess(status, response.data));
  97. }).catch(error => {
  98. dispatch(unfavouriteFail(status, error));
  99. });
  100. };
  101. };
  102. export function favouriteRequest(status) {
  103. return {
  104. type: FAVOURITE_REQUEST,
  105. status: status
  106. };
  107. };
  108. export function favouriteSuccess(status, response) {
  109. return {
  110. type: FAVOURITE_SUCCESS,
  111. status: status,
  112. response: response
  113. };
  114. };
  115. export function favouriteFail(status, error) {
  116. return {
  117. type: FAVOURITE_FAIL,
  118. status: status,
  119. error: error
  120. };
  121. };
  122. export function unfavouriteRequest(status) {
  123. return {
  124. type: UNFAVOURITE_REQUEST,
  125. status: status
  126. };
  127. };
  128. export function unfavouriteSuccess(status, response) {
  129. return {
  130. type: UNFAVOURITE_SUCCESS,
  131. status: status,
  132. response: response
  133. };
  134. };
  135. export function unfavouriteFail(status, error) {
  136. return {
  137. type: UNFAVOURITE_FAIL,
  138. status: status,
  139. error: error
  140. };
  141. };
  142. export function fetchReblogs(id) {
  143. return (dispatch, getState) => {
  144. dispatch(fetchReblogsRequest(id));
  145. api(getState).get(`/api/v1/statuses/${id}/reblogged_by`).then(response => {
  146. dispatch(fetchReblogsSuccess(id, response.data));
  147. }).catch(error => {
  148. dispatch(fetchReblogsFail(id, error));
  149. });
  150. };
  151. };
  152. export function fetchReblogsRequest(id) {
  153. return {
  154. type: REBLOGS_FETCH_REQUEST,
  155. id
  156. };
  157. };
  158. export function fetchReblogsSuccess(id, accounts) {
  159. return {
  160. type: REBLOGS_FETCH_SUCCESS,
  161. id,
  162. accounts
  163. };
  164. };
  165. export function fetchReblogsFail(id, error) {
  166. return {
  167. type: REBLOGS_FETCH_FAIL,
  168. error
  169. };
  170. };
  171. export function fetchFavourites(id) {
  172. return (dispatch, getState) => {
  173. dispatch(fetchFavouritesRequest(id));
  174. api(getState).get(`/api/v1/statuses/${id}/favourited_by`).then(response => {
  175. dispatch(fetchFavouritesSuccess(id, response.data));
  176. }).catch(error => {
  177. dispatch(fetchFavouritesFail(id, error));
  178. });
  179. };
  180. };
  181. export function fetchFavouritesRequest(id) {
  182. return {
  183. type: FAVOURITES_FETCH_REQUEST,
  184. id
  185. };
  186. };
  187. export function fetchFavouritesSuccess(id, accounts) {
  188. return {
  189. type: FAVOURITES_FETCH_SUCCESS,
  190. id,
  191. accounts
  192. };
  193. };
  194. export function fetchFavouritesFail(id, error) {
  195. return {
  196. type: FAVOURITES_FETCH_FAIL,
  197. error
  198. };
  199. };