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-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const fetchPopularSuggestions = () => (dispatch, getState) => {
|
|
|
|
if (!me) return false
|
|
|
|
fetchSuggestions(SUGGESTION_TYPE_VERIFIED, dispatch, getState)
|
2020-07-02 02:36:53 +01:00
|
|
|
}
|
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-07-02 02:36:53 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
const fetchSuggestions = (suggestionType, dispatch, getState, unlimited = false) => {
|
|
|
|
dispatch(fetchSuggestionsRequest(suggestionType))
|
2020-07-02 02:36:53 +01:00
|
|
|
|
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-07-02 02:36:53 +01:00
|
|
|
}
|
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchSuggestionsRequest = (suggestionType) => ({
|
|
|
|
type: SUGGESTIONS_FETCH_REQUEST,
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
skipAlert: true,
|
|
|
|
error,
|
|
|
|
suggestionType,
|
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00: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}`)
|
|
|
|
}
|