2020-04-11 23:29:19 +01:00
|
|
|
import api from '../api'
|
|
|
|
import { importFetchedAccounts } from './importer'
|
2020-08-12 23:58:01 +01:00
|
|
|
import { fetchRelationships } from './accounts'
|
2020-04-11 23:29:19 +01:00
|
|
|
import { me } from '../initial_state'
|
2020-07-02 02:36:53 +01:00
|
|
|
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
|
|
|
|
2020-07-02 02:36:53 +01:00
|
|
|
export const SUGGESTIONS_DISMISS = 'SUGGESTIONS_DISMISS'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-07-02 02:36:53 +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
|
|
|
|
|
2020-07-02 02:36:53 +01:00
|
|
|
dispatch(fetchSuggestionsRequest(SUGGESTION_TYPE_VERIFIED))
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-07-02 02:36:53 +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))
|
2020-08-12 23:58:01 +01:00
|
|
|
dispatch(fetchRelationships(response.data.map(item => item.id)))
|
2020-07-02 02:36:53 +01:00
|
|
|
}).catch(error => dispatch(fetchSuggestionsFail(error, SUGGESTION_TYPE_VERIFIED)))
|
|
|
|
}
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-09-10 22:21:20 +01:00
|
|
|
export function fetchRelatedSuggestions(unlimited = false) {
|
2020-07-02 02:36:53 +01:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
if (!me) return false
|
|
|
|
|
|
|
|
dispatch(fetchSuggestionsRequest(SUGGESTION_TYPE_RELATED))
|
|
|
|
|
2020-09-10 22:21:20 +01:00
|
|
|
api(getState).get(`/api/v1/suggestions?type=${SUGGESTION_TYPE_RELATED}&unlimited=${!!unlimited}`).then(response => {
|
2020-07-02 02:36:53 +01:00
|
|
|
dispatch(importFetchedAccounts(response.data))
|
|
|
|
dispatch(fetchSuggestionsSuccess(response.data, SUGGESTION_TYPE_RELATED))
|
2020-08-12 23:58:01 +01:00
|
|
|
dispatch(fetchRelationships(response.data.map(item => item.id)))
|
2020-07-02 02:36:53 +01:00
|
|
|
}).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,
|
2020-07-02 02:36:53 +01:00
|
|
|
suggestionType,
|
|
|
|
}
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-07-02 02:36:53 +01:00
|
|
|
export function fetchSuggestionsSuccess(accounts, suggestionType) {
|
2019-07-02 08:10:25 +01:00
|
|
|
return {
|
|
|
|
type: SUGGESTIONS_FETCH_SUCCESS,
|
|
|
|
skipLoading: true,
|
2020-07-02 02:36:53 +01:00
|
|
|
accounts,
|
|
|
|
suggestionType
|
|
|
|
}
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-07-02 02:36:53 +01:00
|
|
|
export function fetchSuggestionsFail(error, suggestionType) {
|
2019-07-02 08:10:25 +01:00
|
|
|
return {
|
|
|
|
type: SUGGESTIONS_FETCH_FAIL,
|
|
|
|
skipLoading: true,
|
|
|
|
skipAlert: true,
|
2020-07-02 02:36:53 +01:00
|
|
|
error,
|
|
|
|
suggestionType,
|
|
|
|
}
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-07-02 02:36:53 +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,
|
2020-07-02 02:36:53 +01:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-07-02 02:36:53 +01:00
|
|
|
api(getState).delete(`/api/v1/suggestions/related/${accountId}`)
|
|
|
|
}
|