bookmarks.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import api, { getLinks } from '../api';
  2. import { importFetchedStatuses } from './importer';
  3. export const BOOKMARKED_STATUSES_FETCH_REQUEST = 'BOOKMARKED_STATUSES_FETCH_REQUEST';
  4. export const BOOKMARKED_STATUSES_FETCH_SUCCESS = 'BOOKMARKED_STATUSES_FETCH_SUCCESS';
  5. export const BOOKMARKED_STATUSES_FETCH_FAIL = 'BOOKMARKED_STATUSES_FETCH_FAIL';
  6. export const BOOKMARKED_STATUSES_EXPAND_REQUEST = 'BOOKMARKED_STATUSES_EXPAND_REQUEST';
  7. export const BOOKMARKED_STATUSES_EXPAND_SUCCESS = 'BOOKMARKED_STATUSES_EXPAND_SUCCESS';
  8. export const BOOKMARKED_STATUSES_EXPAND_FAIL = 'BOOKMARKED_STATUSES_EXPAND_FAIL';
  9. export function fetchBookmarkedStatuses() {
  10. return (dispatch, getState) => {
  11. if (getState().getIn(['status_lists', 'bookmarks', 'isLoading'])) {
  12. return;
  13. }
  14. dispatch(fetchBookmarkedStatusesRequest());
  15. api(getState).get('/api/v1/bookmarks').then(response => {
  16. const next = getLinks(response).refs.find(link => link.rel === 'next');
  17. dispatch(importFetchedStatuses(response.data));
  18. dispatch(fetchBookmarkedStatusesSuccess(response.data, next ? next.uri : null));
  19. }).catch(error => {
  20. dispatch(fetchBookmarkedStatusesFail(error));
  21. });
  22. };
  23. }
  24. export function fetchBookmarkedStatusesRequest() {
  25. return {
  26. type: BOOKMARKED_STATUSES_FETCH_REQUEST,
  27. };
  28. }
  29. export function fetchBookmarkedStatusesSuccess(statuses, next) {
  30. return {
  31. type: BOOKMARKED_STATUSES_FETCH_SUCCESS,
  32. statuses,
  33. next,
  34. };
  35. }
  36. export function fetchBookmarkedStatusesFail(error) {
  37. return {
  38. type: BOOKMARKED_STATUSES_FETCH_FAIL,
  39. error,
  40. };
  41. }
  42. export function expandBookmarkedStatuses() {
  43. return (dispatch, getState) => {
  44. const url = getState().getIn(['status_lists', 'bookmarks', 'next'], null);
  45. if (url === null || getState().getIn(['status_lists', 'bookmarks', 'isLoading'])) {
  46. return;
  47. }
  48. dispatch(expandBookmarkedStatusesRequest());
  49. api(getState).get(url).then(response => {
  50. const next = getLinks(response).refs.find(link => link.rel === 'next');
  51. dispatch(importFetchedStatuses(response.data));
  52. dispatch(expandBookmarkedStatusesSuccess(response.data, next ? next.uri : null));
  53. }).catch(error => {
  54. dispatch(expandBookmarkedStatusesFail(error));
  55. });
  56. };
  57. }
  58. export function expandBookmarkedStatusesRequest() {
  59. return {
  60. type: BOOKMARKED_STATUSES_EXPAND_REQUEST,
  61. };
  62. }
  63. export function expandBookmarkedStatusesSuccess(statuses, next) {
  64. return {
  65. type: BOOKMARKED_STATUSES_EXPAND_SUCCESS,
  66. statuses,
  67. next,
  68. };
  69. }
  70. export function expandBookmarkedStatusesFail(error) {
  71. return {
  72. type: BOOKMARKED_STATUSES_EXPAND_FAIL,
  73. error,
  74. };
  75. }