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

80 lines
2.4 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
export function fetchPopularSuggestions() {
2019-07-02 08:10:25 +01:00
return (dispatch, getState) => {
2020-04-11 23:29:19 +01:00
if (!me) return false
dispatch(fetchSuggestionsRequest(SUGGESTION_TYPE_VERIFIED))
2019-07-02 08:10:25 +01:00
api(getState).get(`/api/v1/suggestions?type=${SUGGESTION_TYPE_VERIFIED}`).then(response => {
dispatch(importFetchedAccounts(response.data))
dispatch(fetchSuggestionsSuccess(response.data, SUGGESTION_TYPE_VERIFIED))
dispatch(fetchRelationships(response.data.map(item => item.id)))
}).catch(error => dispatch(fetchSuggestionsFail(error, SUGGESTION_TYPE_VERIFIED)))
}
}
2019-07-02 08:10:25 +01:00
export function fetchRelatedSuggestions(unlimited = false) {
return (dispatch, getState) => {
if (!me) return false
dispatch(fetchSuggestionsRequest(SUGGESTION_TYPE_RELATED))
api(getState).get(`/api/v1/suggestions?type=${SUGGESTION_TYPE_RELATED}&unlimited=${!!unlimited}`).then(response => {
dispatch(importFetchedAccounts(response.data))
dispatch(fetchSuggestionsSuccess(response.data, SUGGESTION_TYPE_RELATED))
dispatch(fetchRelationships(response.data.map(item => item.id)))
}).catch(error => dispatch(fetchSuggestionsFail(error, SUGGESTION_TYPE_RELATED)))
}
}
export function fetchSuggestionsRequest(suggestionType) {
2019-07-02 08:10:25 +01:00
return {
type: SUGGESTIONS_FETCH_REQUEST,
skipLoading: true,
suggestionType,
}
}
2019-07-02 08:10:25 +01:00
export function fetchSuggestionsSuccess(accounts, suggestionType) {
2019-07-02 08:10:25 +01:00
return {
type: SUGGESTIONS_FETCH_SUCCESS,
skipLoading: true,
accounts,
suggestionType
}
}
2019-07-02 08:10:25 +01:00
export function fetchSuggestionsFail(error, suggestionType) {
2019-07-02 08:10:25 +01:00
return {
type: SUGGESTIONS_FETCH_FAIL,
skipLoading: true,
skipAlert: true,
error,
suggestionType,
}
}
2019-07-02 08:10:25 +01: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}`)
}