about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/actions/trends.js
blob: edda0b5b5d0eb5afa1b59601d28b1dfda4aaa6a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import api, { getLinks } from '../api';
import { importFetchedStatuses } from './importer';

export const TRENDS_TAGS_FETCH_REQUEST = 'TRENDS_TAGS_FETCH_REQUEST';
export const TRENDS_TAGS_FETCH_SUCCESS = 'TRENDS_TAGS_FETCH_SUCCESS';
export const TRENDS_TAGS_FETCH_FAIL    = 'TRENDS_TAGS_FETCH_FAIL';

export const TRENDS_LINKS_FETCH_REQUEST = 'TRENDS_LINKS_FETCH_REQUEST';
export const TRENDS_LINKS_FETCH_SUCCESS = 'TRENDS_LINKS_FETCH_SUCCESS';
export const TRENDS_LINKS_FETCH_FAIL    = 'TRENDS_LINKS_FETCH_FAIL';

export const TRENDS_STATUSES_FETCH_REQUEST = 'TRENDS_STATUSES_FETCH_REQUEST';
export const TRENDS_STATUSES_FETCH_SUCCESS = 'TRENDS_STATUSES_FETCH_SUCCESS';
export const TRENDS_STATUSES_FETCH_FAIL    = 'TRENDS_STATUSES_FETCH_FAIL';

export const TRENDS_STATUSES_EXPAND_REQUEST = 'TRENDS_STATUSES_EXPAND_REQUEST';
export const TRENDS_STATUSES_EXPAND_SUCCESS = 'TRENDS_STATUSES_EXPAND_SUCCESS';
export const TRENDS_STATUSES_EXPAND_FAIL    = 'TRENDS_STATUSES_EXPAND_FAIL';

export const fetchTrendingHashtags = () => (dispatch, getState) => {
  dispatch(fetchTrendingHashtagsRequest());

  api(getState)
    .get('/api/v1/trends/tags')
    .then(({ data }) => dispatch(fetchTrendingHashtagsSuccess(data)))
    .catch(err => dispatch(fetchTrendingHashtagsFail(err)));
};

export const fetchTrendingHashtagsRequest = () => ({
  type: TRENDS_TAGS_FETCH_REQUEST,
  skipLoading: true,
});

export const fetchTrendingHashtagsSuccess = trends => ({
  type: TRENDS_TAGS_FETCH_SUCCESS,
  trends,
  skipLoading: true,
});

export const fetchTrendingHashtagsFail = error => ({
  type: TRENDS_TAGS_FETCH_FAIL,
  error,
  skipLoading: true,
  skipAlert: true,
});

export const fetchTrendingLinks = () => (dispatch, getState) => {
  dispatch(fetchTrendingLinksRequest());

  api(getState)
    .get('/api/v1/trends/links')
    .then(({ data }) => dispatch(fetchTrendingLinksSuccess(data)))
    .catch(err => dispatch(fetchTrendingLinksFail(err)));
};

export const fetchTrendingLinksRequest = () => ({
  type: TRENDS_LINKS_FETCH_REQUEST,
  skipLoading: true,
});

export const fetchTrendingLinksSuccess = trends => ({
  type: TRENDS_LINKS_FETCH_SUCCESS,
  trends,
  skipLoading: true,
});

export const fetchTrendingLinksFail = error => ({
  type: TRENDS_LINKS_FETCH_FAIL,
  error,
  skipLoading: true,
  skipAlert: true,
});

export const fetchTrendingStatuses = () => (dispatch, getState) => {
  if (getState().getIn(['status_lists', 'trending', 'isLoading'])) {
    return;
  }

  dispatch(fetchTrendingStatusesRequest());

  api(getState).get('/api/v1/trends/statuses').then(response => {
    const next = getLinks(response).refs.find(link => link.rel === 'next');
    dispatch(importFetchedStatuses(response.data));
    dispatch(fetchTrendingStatusesSuccess(response.data, next ? next.uri : null));
  }).catch(err => dispatch(fetchTrendingStatusesFail(err)));
};

export const fetchTrendingStatusesRequest = () => ({
  type: TRENDS_STATUSES_FETCH_REQUEST,
  skipLoading: true,
});

export const fetchTrendingStatusesSuccess = (statuses, next) => ({
  type: TRENDS_STATUSES_FETCH_SUCCESS,
  statuses,
  next,
  skipLoading: true,
});

export const fetchTrendingStatusesFail = error => ({
  type: TRENDS_STATUSES_FETCH_FAIL,
  error,
  skipLoading: true,
  skipAlert: true,
});


export const expandTrendingStatuses = () => (dispatch, getState) => {
  const url = getState().getIn(['status_lists', 'trending', 'next'], null);

  if (url === null || getState().getIn(['status_lists', 'trending', 'isLoading'])) {
    return;
  }

  dispatch(expandTrendingStatusesRequest());

  api(getState).get(url).then(response => {
    const next = getLinks(response).refs.find(link => link.rel === 'next');
    dispatch(importFetchedStatuses(response.data));
    dispatch(expandTrendingStatusesSuccess(response.data, next ? next.uri : null));
  }).catch(error => {
    dispatch(expandTrendingStatusesFail(error));
  });
};

export const expandTrendingStatusesRequest = () => ({
  type: TRENDS_STATUSES_EXPAND_REQUEST,
});

export const expandTrendingStatusesSuccess = (statuses, next) => ({
  type: TRENDS_STATUSES_EXPAND_SUCCESS,
  statuses,
  next,
});

export const expandTrendingStatusesFail = error => ({
  type: TRENDS_STATUSES_EXPAND_FAIL,
  error,
});