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

78 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-04-11 23:29:19 +01:00
import api from '../api'
import { importFetchedAccounts } from './importer'
import { fetchRelationships } from './accounts'
2020-04-11 23:29:19 +01:00
import { me } from '../initial_state'
import {
SUGGESTION_TYPE_VERIFIED,
SUGGESTION_TYPE_RELATED,
} from '../constants'
2019-07-02 08:10:25 +01:00
2020-04-11 23:29:19 +01:00
export const SUGGESTIONS_FETCH_REQUEST = 'SUGGESTIONS_FETCH_REQUEST'
export const SUGGESTIONS_FETCH_SUCCESS = 'SUGGESTIONS_FETCH_SUCCESS'
export const SUGGESTIONS_FETCH_FAIL = 'SUGGESTIONS_FETCH_FAIL'
2019-07-02 08:10:25 +01:00
export const SUGGESTIONS_DISMISS = 'SUGGESTIONS_DISMISS'
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
/**
*
*/
export const fetchPopularSuggestions = () => (dispatch, getState) => {
if (!me) return false
fetchSuggestions(SUGGESTION_TYPE_VERIFIED, dispatch, getState)
}
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
/**
*
*/
export const fetchRelatedSuggestions = (unlimited = false) => (dispatch, getState) => {
if (!me) return false
fetchSuggestions(SUGGESTION_TYPE_RELATED, dispatch, getState, unlimited)
}
2020-11-15 18:48:32 +00:00
/**
*
*/
const fetchSuggestions = (suggestionType, dispatch, getState, unlimited = false) => {
dispatch(fetchSuggestionsRequest(suggestionType))
2020-11-15 18:48:32 +00:00
api(getState).get(`/api/v1/suggestions?type=${suggestionType}&unlimited=${!!unlimited}`).then((response) => {
dispatch(importFetchedAccounts(response.data))
dispatch(fetchSuggestionsSuccess(response.data, suggestionType))
dispatch(fetchRelationships(response.data.map(item => item.id)))
}).catch(error => dispatch(fetchSuggestionsFail(error, suggestionType)))
}
2020-11-15 18:48:32 +00:00
const fetchSuggestionsRequest = (suggestionType) => ({
type: SUGGESTIONS_FETCH_REQUEST,
skipLoading: true,
suggestionType,
})
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
const fetchSuggestionsSuccess = (accounts, suggestionType) => ({
type: SUGGESTIONS_FETCH_SUCCESS,
skipLoading: true,
accounts,
suggestionType
})
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
const fetchSuggestionsFail = (error, suggestionType) => ({
type: SUGGESTIONS_FETCH_FAIL,
skipLoading: true,
skipAlert: true,
error,
suggestionType,
})
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
/**
*
*/
export const dismissRelatedSuggestion = (accountId) => (dispatch, getState) => {
if (!me) return
2019-07-02 08:10:25 +01:00
dispatch({
type: SUGGESTIONS_DISMISS,
id: accountId,
})
2019-07-02 08:10:25 +01:00
api(getState).delete(`/api/v1/suggestions/related/${accountId}`)
}