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

87 lines
2.5 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-11-15 18:48:32 +00:00
/**
*
*/
export const fetchFavoritedStatuses = () => (dispatch, getState) => {
if (!me) return
if (getState().getIn(['status_lists', 'favorites', 'isLoading'])) {
return
2020-03-02 22:26:25 +00:00
}
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
dispatch(fetchFavoritedStatusesRequest())
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
api(getState).get('/api/v1/favourites').then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next')
dispatch(importFetchedStatuses(response.data))
dispatch(fetchFavoritedStatusesSuccess(response.data, next ? next.uri : null))
}).catch(error => {
dispatch(fetchFavoritedStatusesFail(error))
})
2020-03-02 22:26:25 +00:00
}
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
const fetchFavoritedStatusesRequest = () => ({
type: FAVORITED_STATUSES_FETCH_REQUEST,
})
const fetchFavoritedStatusesSuccess = (statuses, next) => ({
type: FAVORITED_STATUSES_FETCH_SUCCESS,
statuses,
next,
})
const fetchFavoritedStatusesFail = (error) => ({
type: FAVORITED_STATUSES_FETCH_FAIL,
showToast: true,
2020-11-15 18:48:32 +00:00
error,
})
/**
*
*/
export const expandFavoritedStatuses = () => (dispatch, getState) => {
if (!me) return
const url = getState().getIn(['status_lists', 'favorites', 'next'], null)
if (url === null || getState().getIn(['status_lists', 'favorites', 'isLoading'])) {
return
2020-03-02 22:26:25 +00:00
}
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
dispatch(expandFavoritedStatusesRequest())
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
api(getState).get(url).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next')
dispatch(importFetchedStatuses(response.data))
dispatch(expandFavoritedStatusesSuccess(response.data, next ? next.uri : null))
}).catch(error => {
dispatch(expandFavoritedStatusesFail(error))
})
2020-03-02 22:26:25 +00:00
}
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
const expandFavoritedStatusesRequest = () => ({
type: FAVORITED_STATUSES_EXPAND_REQUEST,
})
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
const expandFavoritedStatusesSuccess = (statuses, next) => ({
type: FAVORITED_STATUSES_EXPAND_SUCCESS,
statuses,
next,
})
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
const expandFavoritedStatusesFail = (error) => ({
type: FAVORITED_STATUSES_EXPAND_FAIL,
showToast: true,
2020-11-15 18:48:32 +00:00
error,
})