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.

90 lines
2.6 KiB

  1. import api, { getLinks } from 'flavours/glitch/util/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. skipLoading: true,
  26. };
  27. };
  28. export function fetchFavouritedStatusesSuccess(statuses, next) {
  29. return {
  30. type: FAVOURITED_STATUSES_FETCH_SUCCESS,
  31. statuses,
  32. next,
  33. skipLoading: true,
  34. };
  35. };
  36. export function fetchFavouritedStatusesFail(error) {
  37. return {
  38. type: FAVOURITED_STATUSES_FETCH_FAIL,
  39. error,
  40. skipLoading: true,
  41. };
  42. };
  43. export function expandFavouritedStatuses() {
  44. return (dispatch, getState) => {
  45. const url = getState().getIn(['status_lists', 'favourites', 'next'], null);
  46. if (url === null || getState().getIn(['status_lists', 'favourites', 'isLoading'])) {
  47. return;
  48. }
  49. dispatch(expandFavouritedStatusesRequest());
  50. api(getState).get(url).then(response => {
  51. const next = getLinks(response).refs.find(link => link.rel === 'next');
  52. dispatch(expandFavouritedStatusesSuccess(response.data, next ? next.uri : null));
  53. }).catch(error => {
  54. dispatch(expandFavouritedStatusesFail(error));
  55. });
  56. };
  57. };
  58. export function expandFavouritedStatusesRequest() {
  59. return {
  60. type: FAVOURITED_STATUSES_EXPAND_REQUEST,
  61. };
  62. };
  63. export function expandFavouritedStatusesSuccess(statuses, next) {
  64. return {
  65. type: FAVOURITED_STATUSES_EXPAND_SUCCESS,
  66. statuses,
  67. next,
  68. };
  69. };
  70. export function expandFavouritedStatusesFail(error) {
  71. return {
  72. type: FAVOURITED_STATUSES_EXPAND_FAIL,
  73. error,
  74. };
  75. };