trends.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import api, { getLinks } from '../api';
  2. import { importFetchedStatuses } from './importer';
  3. export const TRENDS_TAGS_FETCH_REQUEST = 'TRENDS_TAGS_FETCH_REQUEST';
  4. export const TRENDS_TAGS_FETCH_SUCCESS = 'TRENDS_TAGS_FETCH_SUCCESS';
  5. export const TRENDS_TAGS_FETCH_FAIL = 'TRENDS_TAGS_FETCH_FAIL';
  6. export const TRENDS_LINKS_FETCH_REQUEST = 'TRENDS_LINKS_FETCH_REQUEST';
  7. export const TRENDS_LINKS_FETCH_SUCCESS = 'TRENDS_LINKS_FETCH_SUCCESS';
  8. export const TRENDS_LINKS_FETCH_FAIL = 'TRENDS_LINKS_FETCH_FAIL';
  9. export const TRENDS_STATUSES_FETCH_REQUEST = 'TRENDS_STATUSES_FETCH_REQUEST';
  10. export const TRENDS_STATUSES_FETCH_SUCCESS = 'TRENDS_STATUSES_FETCH_SUCCESS';
  11. export const TRENDS_STATUSES_FETCH_FAIL = 'TRENDS_STATUSES_FETCH_FAIL';
  12. export const TRENDS_STATUSES_EXPAND_REQUEST = 'TRENDS_STATUSES_EXPAND_REQUEST';
  13. export const TRENDS_STATUSES_EXPAND_SUCCESS = 'TRENDS_STATUSES_EXPAND_SUCCESS';
  14. export const TRENDS_STATUSES_EXPAND_FAIL = 'TRENDS_STATUSES_EXPAND_FAIL';
  15. export const fetchTrendingHashtags = () => (dispatch, getState) => {
  16. dispatch(fetchTrendingHashtagsRequest());
  17. api(getState)
  18. .get('/api/v1/trends/tags')
  19. .then(({ data }) => dispatch(fetchTrendingHashtagsSuccess(data)))
  20. .catch(err => dispatch(fetchTrendingHashtagsFail(err)));
  21. };
  22. export const fetchTrendingHashtagsRequest = () => ({
  23. type: TRENDS_TAGS_FETCH_REQUEST,
  24. skipLoading: true,
  25. });
  26. export const fetchTrendingHashtagsSuccess = trends => ({
  27. type: TRENDS_TAGS_FETCH_SUCCESS,
  28. trends,
  29. skipLoading: true,
  30. });
  31. export const fetchTrendingHashtagsFail = error => ({
  32. type: TRENDS_TAGS_FETCH_FAIL,
  33. error,
  34. skipLoading: true,
  35. skipAlert: true,
  36. });
  37. export const fetchTrendingLinks = () => (dispatch, getState) => {
  38. dispatch(fetchTrendingLinksRequest());
  39. api(getState)
  40. .get('/api/v1/trends/links')
  41. .then(({ data }) => dispatch(fetchTrendingLinksSuccess(data)))
  42. .catch(err => dispatch(fetchTrendingLinksFail(err)));
  43. };
  44. export const fetchTrendingLinksRequest = () => ({
  45. type: TRENDS_LINKS_FETCH_REQUEST,
  46. skipLoading: true,
  47. });
  48. export const fetchTrendingLinksSuccess = trends => ({
  49. type: TRENDS_LINKS_FETCH_SUCCESS,
  50. trends,
  51. skipLoading: true,
  52. });
  53. export const fetchTrendingLinksFail = error => ({
  54. type: TRENDS_LINKS_FETCH_FAIL,
  55. error,
  56. skipLoading: true,
  57. skipAlert: true,
  58. });
  59. export const fetchTrendingStatuses = () => (dispatch, getState) => {
  60. if (getState().getIn(['status_lists', 'trending', 'isLoading'])) {
  61. return;
  62. }
  63. dispatch(fetchTrendingStatusesRequest());
  64. api(getState).get('/api/v1/trends/statuses').then(response => {
  65. const next = getLinks(response).refs.find(link => link.rel === 'next');
  66. dispatch(importFetchedStatuses(response.data));
  67. dispatch(fetchTrendingStatusesSuccess(response.data, next ? next.uri : null));
  68. }).catch(err => dispatch(fetchTrendingStatusesFail(err)));
  69. };
  70. export const fetchTrendingStatusesRequest = () => ({
  71. type: TRENDS_STATUSES_FETCH_REQUEST,
  72. skipLoading: true,
  73. });
  74. export const fetchTrendingStatusesSuccess = (statuses, next) => ({
  75. type: TRENDS_STATUSES_FETCH_SUCCESS,
  76. statuses,
  77. next,
  78. skipLoading: true,
  79. });
  80. export const fetchTrendingStatusesFail = error => ({
  81. type: TRENDS_STATUSES_FETCH_FAIL,
  82. error,
  83. skipLoading: true,
  84. skipAlert: true,
  85. });
  86. export const expandTrendingStatuses = () => (dispatch, getState) => {
  87. const url = getState().getIn(['status_lists', 'trending', 'next'], null);
  88. if (url === null || getState().getIn(['status_lists', 'trending', 'isLoading'])) {
  89. return;
  90. }
  91. dispatch(expandTrendingStatusesRequest());
  92. api(getState).get(url).then(response => {
  93. const next = getLinks(response).refs.find(link => link.rel === 'next');
  94. dispatch(importFetchedStatuses(response.data));
  95. dispatch(expandTrendingStatusesSuccess(response.data, next ? next.uri : null));
  96. }).catch(error => {
  97. dispatch(expandTrendingStatusesFail(error));
  98. });
  99. };
  100. export const expandTrendingStatusesRequest = () => ({
  101. type: TRENDS_STATUSES_EXPAND_REQUEST,
  102. });
  103. export const expandTrendingStatusesSuccess = (statuses, next) => ({
  104. type: TRENDS_STATUSES_EXPAND_SUCCESS,
  105. statuses,
  106. next,
  107. });
  108. export const expandTrendingStatusesFail = error => ({
  109. type: TRENDS_STATUSES_EXPAND_FAIL,
  110. error,
  111. });