You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.9 KiB

  1. import api from '../api';
  2. import { importFetchedAccounts } from './importer';
  3. import { fetchRelationships } from './accounts';
  4. export const SUGGESTIONS_FETCH_REQUEST = 'SUGGESTIONS_FETCH_REQUEST';
  5. export const SUGGESTIONS_FETCH_SUCCESS = 'SUGGESTIONS_FETCH_SUCCESS';
  6. export const SUGGESTIONS_FETCH_FAIL = 'SUGGESTIONS_FETCH_FAIL';
  7. export const SUGGESTIONS_DISMISS = 'SUGGESTIONS_DISMISS';
  8. export function fetchSuggestions(withRelationships = false) {
  9. return (dispatch, getState) => {
  10. dispatch(fetchSuggestionsRequest());
  11. api(getState).get('/api/v2/suggestions', { params: { limit: 20 } }).then(response => {
  12. dispatch(importFetchedAccounts(response.data.map(x => x.account)));
  13. dispatch(fetchSuggestionsSuccess(response.data));
  14. if (withRelationships) {
  15. dispatch(fetchRelationships(response.data.map(item => item.account.id)));
  16. }
  17. }).catch(error => dispatch(fetchSuggestionsFail(error)));
  18. };
  19. };
  20. export function fetchSuggestionsRequest() {
  21. return {
  22. type: SUGGESTIONS_FETCH_REQUEST,
  23. skipLoading: true,
  24. };
  25. };
  26. export function fetchSuggestionsSuccess(suggestions) {
  27. return {
  28. type: SUGGESTIONS_FETCH_SUCCESS,
  29. suggestions,
  30. skipLoading: true,
  31. };
  32. };
  33. export function fetchSuggestionsFail(error) {
  34. return {
  35. type: SUGGESTIONS_FETCH_FAIL,
  36. error,
  37. skipLoading: true,
  38. skipAlert: true,
  39. };
  40. };
  41. export const dismissSuggestion = accountId => (dispatch, getState) => {
  42. dispatch({
  43. type: SUGGESTIONS_DISMISS,
  44. id: accountId,
  45. });
  46. api(getState).delete(`/api/v1/suggestions/${accountId}`).then(() => {
  47. dispatch(fetchSuggestionsRequest());
  48. api(getState).get('/api/v2/suggestions').then(response => {
  49. dispatch(importFetchedAccounts(response.data.map(x => x.account)));
  50. dispatch(fetchSuggestionsSuccess(response.data));
  51. }).catch(error => dispatch(fetchSuggestionsFail(error)));
  52. }).catch(() => {});
  53. };