diff --git a/app/soapbox/actions/suggestions.js b/app/soapbox/actions/suggestions.js index be00cb610..71026bad5 100644 --- a/app/soapbox/actions/suggestions.js +++ b/app/soapbox/actions/suggestions.js @@ -1,6 +1,7 @@ import api from '../api'; import { importFetchedAccounts } from './importer'; import { isLoggedIn } from 'soapbox/utils/auth'; +import { getFeatures } from 'soapbox/utils/features'; export const SUGGESTIONS_FETCH_REQUEST = 'SUGGESTIONS_FETCH_REQUEST'; export const SUGGESTIONS_FETCH_SUCCESS = 'SUGGESTIONS_FETCH_SUCCESS'; @@ -8,38 +9,48 @@ export const SUGGESTIONS_FETCH_FAIL = 'SUGGESTIONS_FETCH_FAIL'; export const SUGGESTIONS_DISMISS = 'SUGGESTIONS_DISMISS'; +export const SUGGESTIONS_V2_FETCH_REQUEST = 'SUGGESTIONS_V2_FETCH_REQUEST'; +export const SUGGESTIONS_V2_FETCH_SUCCESS = 'SUGGESTIONS_V2_FETCH_SUCCESS'; +export const SUGGESTIONS_V2_FETCH_FAIL = 'SUGGESTIONS_V2_FETCH_FAIL'; + +export function fetchSuggestionsV1() { + return (dispatch, getState) => { + dispatch({ type: SUGGESTIONS_FETCH_REQUEST, skipLoading: true }); + api(getState).get('/api/v1/suggestions').then(({ data: accounts }) => { + dispatch(importFetchedAccounts(accounts)); + dispatch({ type: SUGGESTIONS_FETCH_SUCCESS, accounts, skipLoading: true }); + }).catch(error => { + dispatch({ type: SUGGESTIONS_FETCH_FAIL, error, skipLoading: true, skipAlert: true }); + }); + }; +} + +export function fetchSuggestionsV2() { + return (dispatch, getState) => { + dispatch({ type: SUGGESTIONS_V2_FETCH_REQUEST, skipLoading: true }); + api(getState).get('/api/v2/suggestions').then(({ data: suggestions }) => { + const accounts = suggestions.map(({ account }) => account); + dispatch(importFetchedAccounts(accounts)); + dispatch({ type: SUGGESTIONS_V2_FETCH_SUCCESS, suggestions, skipLoading: true }); + }).catch(error => { + dispatch({ type: SUGGESTIONS_V2_FETCH_FAIL, error, skipLoading: true, skipAlert: true }); + }); + }; +} + export function fetchSuggestions() { return (dispatch, getState) => { - dispatch(fetchSuggestionsRequest()); + const state = getState(); + const instance = state.get('instance'); + const features = getFeatures(instance); - api(getState).get('/api/v1/suggestions').then(response => { - dispatch(importFetchedAccounts(response.data)); - dispatch(fetchSuggestionsSuccess(response.data)); - }).catch(error => dispatch(fetchSuggestionsFail(error))); - }; -} - -export function fetchSuggestionsRequest() { - return { - type: SUGGESTIONS_FETCH_REQUEST, - skipLoading: true, - }; -} - -export function fetchSuggestionsSuccess(accounts) { - return { - type: SUGGESTIONS_FETCH_SUCCESS, - accounts, - skipLoading: true, - }; -} - -export function fetchSuggestionsFail(error) { - return { - type: SUGGESTIONS_FETCH_FAIL, - error, - skipLoading: true, - skipAlert: true, + if (features.suggestionsV2) { + dispatch(fetchSuggestionsV2()); + } else if (features.suggestions) { + dispatch(fetchSuggestionsV1()); + } else { + // Do nothing + } }; } diff --git a/app/soapbox/actions/suggestions_v2.js b/app/soapbox/actions/suggestions_v2.js deleted file mode 100644 index 0cb33fe39..000000000 --- a/app/soapbox/actions/suggestions_v2.js +++ /dev/null @@ -1,18 +0,0 @@ -import api from '../api'; -import { importFetchedAccount } from './importer'; - -export const SUGGESTIONS_V2_FETCH_REQUEST = 'SUGGESTIONS_V2_FETCH_REQUEST'; -export const SUGGESTIONS_V2_FETCH_SUCCESS = 'SUGGESTIONS_V2_FETCH_SUCCESS'; -export const SUGGESTIONS_V2_FETCH_FAIL = 'SUGGESTIONS_V2_FETCH_FAIL'; - -export function fetchSuggestions() { - return (dispatch, getState) => { - dispatch({ type: SUGGESTIONS_V2_FETCH_REQUEST, skipLoading: true }); - api(getState).get('/api/v2/suggestions').then(({ data: suggestions }) => { - suggestions.forEach(({ account }) => dispatch(importFetchedAccount(account))); - dispatch({ type: SUGGESTIONS_V2_FETCH_SUCCESS, suggestions, skipLoading: true }); - }).catch(error => { - dispatch({ type: SUGGESTIONS_V2_FETCH_FAIL, error, skipLoading: true, skipAlert: true }); - }); - }; -} diff --git a/app/soapbox/features/follow_recommendations/components/account.js b/app/soapbox/features/follow_recommendations/components/account.js index 97b143cc0..e2fbce0bf 100644 --- a/app/soapbox/features/follow_recommendations/components/account.js +++ b/app/soapbox/features/follow_recommendations/components/account.js @@ -66,7 +66,7 @@ class Account extends ImmutablePureComponent { return (