bigbuffet-rw/app/soapbox/actions/suggestions.js

67 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import api from '../api';
import { importFetchedAccounts } from './importer';
2021-03-25 10:25:45 -07:00
import { isLoggedIn } from 'soapbox/utils/auth';
2021-09-17 13:38:38 -07:00
import { getFeatures } from 'soapbox/utils/features';
2020-03-27 13:59:38 -07: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';
export const SUGGESTIONS_DISMISS = 'SUGGESTIONS_DISMISS';
2021-09-17 13:38:38 -07:00
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';
2020-03-27 13:59:38 -07:00
2021-09-17 13:38:38 -07:00
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 });
});
2020-03-27 13:59:38 -07:00
};
2021-08-03 12:22:51 -07:00
}
2020-03-27 13:59:38 -07:00
2021-09-17 13:38:38 -07:00
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 });
});
2020-03-27 13:59:38 -07:00
};
2021-08-03 12:22:51 -07:00
}
2020-03-27 13:59:38 -07:00
2021-09-17 13:38:38 -07:00
export function fetchSuggestions() {
return (dispatch, getState) => {
const state = getState();
const instance = state.get('instance');
const features = getFeatures(instance);
2020-03-27 13:59:38 -07:00
2021-09-17 13:38:38 -07:00
if (features.suggestionsV2) {
dispatch(fetchSuggestionsV2());
} else if (features.suggestions) {
dispatch(fetchSuggestionsV1());
} else {
// Do nothing
}
2020-03-27 13:59:38 -07:00
};
2021-08-03 12:22:51 -07:00
}
2020-03-27 13:59:38 -07:00
export const dismissSuggestion = accountId => (dispatch, getState) => {
2021-03-24 09:44:51 -07:00
if (!isLoggedIn(getState)) return;
2020-03-27 13:59:38 -07:00
dispatch({
type: SUGGESTIONS_DISMISS,
id: accountId,
});
api(getState).delete(`/api/v1/suggestions/${accountId}`);
};