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.

87 lines
2.5 KiB

  1. import api, { getLinks } from '../api';
  2. export const FAVOURITED_STATUSES_FETCH_REQUEST = 'FAVOURITED_STATUSES_FETCH_REQUEST';
  3. export const FAVOURITED_STATUSES_FETCH_SUCCESS = 'FAVOURITED_STATUSES_FETCH_SUCCESS';
  4. export const FAVOURITED_STATUSES_FETCH_FAIL = 'FAVOURITED_STATUSES_FETCH_FAIL';
  5. export const FAVOURITED_STATUSES_EXPAND_REQUEST = 'FAVOURITED_STATUSES_EXPAND_REQUEST';
  6. export const FAVOURITED_STATUSES_EXPAND_SUCCESS = 'FAVOURITED_STATUSES_EXPAND_SUCCESS';
  7. export const FAVOURITED_STATUSES_EXPAND_FAIL = 'FAVOURITED_STATUSES_EXPAND_FAIL';
  8. export function fetchFavouritedStatuses() {
  9. return (dispatch, getState) => {
  10. if (getState().getIn(['status_lists', 'favourites', 'isLoading'])) {
  11. return;
  12. }
  13. dispatch(fetchFavouritedStatusesRequest());
  14. api(getState).get('/api/v1/favourites').then(response => {
  15. const next = getLinks(response).refs.find(link => link.rel === 'next');
  16. dispatch(fetchFavouritedStatusesSuccess(response.data, next ? next.uri : null));
  17. }).catch(error => {
  18. dispatch(fetchFavouritedStatusesFail(error));
  19. });
  20. };
  21. };
  22. export function fetchFavouritedStatusesRequest() {
  23. return {
  24. type: FAVOURITED_STATUSES_FETCH_REQUEST,
  25. };
  26. };
  27. export function fetchFavouritedStatusesSuccess(statuses, next) {
  28. return {
  29. type: FAVOURITED_STATUSES_FETCH_SUCCESS,
  30. statuses,
  31. next,
  32. };
  33. };
  34. export function fetchFavouritedStatusesFail(error) {
  35. return {
  36. type: FAVOURITED_STATUSES_FETCH_FAIL,
  37. error,
  38. };
  39. };
  40. export function expandFavouritedStatuses() {
  41. return (dispatch, getState) => {
  42. const url = getState().getIn(['status_lists', 'favourites', 'next'], null);
  43. if (url === null || getState().getIn(['status_lists', 'favourites', 'isLoading'])) {
  44. return;
  45. }
  46. dispatch(expandFavouritedStatusesRequest());
  47. api(getState).get(url).then(response => {
  48. const next = getLinks(response).refs.find(link => link.rel === 'next');
  49. dispatch(expandFavouritedStatusesSuccess(response.data, next ? next.uri : null));
  50. }).catch(error => {
  51. dispatch(expandFavouritedStatusesFail(error));
  52. });
  53. };
  54. };
  55. export function expandFavouritedStatusesRequest() {
  56. return {
  57. type: FAVOURITED_STATUSES_EXPAND_REQUEST,
  58. };
  59. };
  60. export function expandFavouritedStatusesSuccess(statuses, next) {
  61. return {
  62. type: FAVOURITED_STATUSES_EXPAND_SUCCESS,
  63. statuses,
  64. next,
  65. };
  66. };
  67. export function expandFavouritedStatusesFail(error) {
  68. return {
  69. type: FAVOURITED_STATUSES_EXPAND_FAIL,
  70. error,
  71. };
  72. };