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.

59 lines
1.7 KiB

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