history.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import api from '../api';
  2. import { importFetchedAccounts } from './importer';
  3. export const HISTORY_FETCH_REQUEST = 'HISTORY_FETCH_REQUEST';
  4. export const HISTORY_FETCH_SUCCESS = 'HISTORY_FETCH_SUCCESS';
  5. export const HISTORY_FETCH_FAIL = 'HISTORY_FETCH_FAIL';
  6. export const fetchHistory = statusId => (dispatch, getState) => {
  7. const loading = getState().getIn(['history', statusId, 'loading']);
  8. if (loading) {
  9. return;
  10. }
  11. dispatch(fetchHistoryRequest(statusId));
  12. api(getState).get(`/api/v1/statuses/${statusId}/history`).then(({ data }) => {
  13. dispatch(importFetchedAccounts(data.map(x => x.account)));
  14. dispatch(fetchHistorySuccess(statusId, data));
  15. }).catch(error => dispatch(fetchHistoryFail(error)));
  16. };
  17. export const fetchHistoryRequest = statusId => ({
  18. type: HISTORY_FETCH_REQUEST,
  19. statusId,
  20. });
  21. export const fetchHistorySuccess = (statusId, history) => ({
  22. type: HISTORY_FETCH_SUCCESS,
  23. statusId,
  24. history,
  25. });
  26. export const fetchHistoryFail = error => ({
  27. type: HISTORY_FETCH_FAIL,
  28. error,
  29. });