gab-social/app/javascript/gabsocial/actions/favorites.js

99 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-03-02 22:26:25 +00:00
import api, { getLinks } from '../api'
import { importFetchedStatuses } from './importer'
import { me } from '../initial_state'
2019-07-02 08:10:25 +01:00
2020-03-02 22:26:25 +00:00
export const FAVORITED_STATUSES_FETCH_REQUEST = 'FAVORITED_STATUSES_FETCH_REQUEST'
export const FAVORITED_STATUSES_FETCH_SUCCESS = 'FAVORITED_STATUSES_FETCH_SUCCESS'
export const FAVORITED_STATUSES_FETCH_FAIL = 'FAVORITED_STATUSES_FETCH_FAIL'
2019-07-02 08:10:25 +01:00
2020-03-02 22:26:25 +00:00
export const FAVORITED_STATUSES_EXPAND_REQUEST = 'FAVORITED_STATUSES_EXPAND_REQUEST'
export const FAVORITED_STATUSES_EXPAND_SUCCESS = 'FAVORITED_STATUSES_EXPAND_SUCCESS'
export const FAVORITED_STATUSES_EXPAND_FAIL = 'FAVORITED_STATUSES_EXPAND_FAIL'
2019-07-02 08:10:25 +01:00
2020-02-28 15:20:47 +00:00
export function fetchFavoritedStatuses() {
2019-07-02 08:10:25 +01:00
return (dispatch, getState) => {
2020-03-02 22:26:25 +00:00
if (!me) return
2019-07-02 08:10:25 +01:00
2020-03-02 22:26:25 +00:00
if (getState().getIn(['status_lists', 'favorites', 'isLoading'])) {
return
2019-07-02 08:10:25 +01:00
}
2020-03-02 22:26:25 +00:00
dispatch(fetchFavoritedStatusesRequest())
2019-07-02 08:10:25 +01:00
api(getState).get('/api/v1/favourites').then(response => {
2020-03-02 22:26:25 +00:00
const next = getLinks(response).refs.find(link => link.rel === 'next')
dispatch(importFetchedStatuses(response.data))
dispatch(fetchFavoritedStatusesSuccess(response.data, next ? next.uri : null))
2019-07-02 08:10:25 +01:00
}).catch(error => {
2020-03-02 22:26:25 +00:00
dispatch(fetchFavoritedStatusesFail(error))
})
}
}
2019-07-02 08:10:25 +01:00
2020-02-28 15:20:47 +00:00
export function fetchFavoritedStatusesRequest() {
2019-07-02 08:10:25 +01:00
return {
2020-02-29 15:42:47 +00:00
type: FAVORITED_STATUSES_FETCH_REQUEST,
2019-07-02 08:10:25 +01:00
skipLoading: true,
2020-03-02 22:26:25 +00:00
}
}
2019-07-02 08:10:25 +01:00
2020-02-28 15:20:47 +00:00
export function fetchFavoritedStatusesSuccess(statuses, next) {
2019-07-02 08:10:25 +01:00
return {
2020-02-29 15:42:47 +00:00
type: FAVORITED_STATUSES_FETCH_SUCCESS,
2019-07-02 08:10:25 +01:00
statuses,
next,
skipLoading: true,
2020-03-02 22:26:25 +00:00
}
}
2019-07-02 08:10:25 +01:00
2020-02-28 15:20:47 +00:00
export function fetchFavoritedStatusesFail(error) {
2019-07-02 08:10:25 +01:00
return {
2020-02-29 15:42:47 +00:00
type: FAVORITED_STATUSES_FETCH_FAIL,
2019-07-02 08:10:25 +01:00
error,
skipLoading: true,
2020-03-02 22:26:25 +00:00
}
}
2019-07-02 08:10:25 +01:00
2020-02-28 15:20:47 +00:00
export function expandFavoritedStatuses() {
2019-07-02 08:10:25 +01:00
return (dispatch, getState) => {
2020-03-02 22:26:25 +00:00
if (!me) return
2019-07-02 08:10:25 +01:00
2020-03-02 22:26:25 +00:00
const url = getState().getIn(['status_lists', 'favorites', 'next'], null)
if (url === null || getState().getIn(['status_lists', 'favorites', 'isLoading'])) {
return
2019-07-02 08:10:25 +01:00
}
2020-03-02 22:26:25 +00:00
dispatch(expandFavoritedStatusesRequest())
2019-07-02 08:10:25 +01:00
api(getState).get(url).then(response => {
2020-03-02 22:26:25 +00:00
const next = getLinks(response).refs.find(link => link.rel === 'next')
dispatch(importFetchedStatuses(response.data))
dispatch(expandFavoritedStatusesSuccess(response.data, next ? next.uri : null))
2019-07-02 08:10:25 +01:00
}).catch(error => {
2020-03-02 22:26:25 +00:00
dispatch(expandFavoritedStatusesFail(error))
})
}
}
2019-07-02 08:10:25 +01:00
2020-02-28 15:20:47 +00:00
export function expandFavoritedStatusesRequest() {
2019-07-02 08:10:25 +01:00
return {
2020-02-29 15:42:47 +00:00
type: FAVORITED_STATUSES_EXPAND_REQUEST,
2020-03-02 22:26:25 +00:00
}
}
2019-07-02 08:10:25 +01:00
2020-02-28 15:20:47 +00:00
export function expandFavoritedStatusesSuccess(statuses, next) {
2019-07-02 08:10:25 +01:00
return {
2020-02-29 15:42:47 +00:00
type: FAVORITED_STATUSES_EXPAND_SUCCESS,
2019-07-02 08:10:25 +01:00
statuses,
next,
2020-03-02 22:26:25 +00:00
}
}
2019-07-02 08:10:25 +01:00
2020-02-28 15:20:47 +00:00
export function expandFavoritedStatusesFail(error) {
2019-07-02 08:10:25 +01:00
return {
2020-02-29 15:42:47 +00:00
type: FAVORITED_STATUSES_EXPAND_FAIL,
2019-07-02 08:10:25 +01:00
error,
2020-03-02 22:26:25 +00:00
}
}