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.

99 lines
2.5 KiB

  1. import api, { getLinks } from '../api';
  2. import { fetchRelationships } from './accounts';
  3. import { importFetchedAccounts } from './importer';
  4. import { openModal } from './modal';
  5. export const BLOCKS_FETCH_REQUEST = 'BLOCKS_FETCH_REQUEST';
  6. export const BLOCKS_FETCH_SUCCESS = 'BLOCKS_FETCH_SUCCESS';
  7. export const BLOCKS_FETCH_FAIL = 'BLOCKS_FETCH_FAIL';
  8. export const BLOCKS_EXPAND_REQUEST = 'BLOCKS_EXPAND_REQUEST';
  9. export const BLOCKS_EXPAND_SUCCESS = 'BLOCKS_EXPAND_SUCCESS';
  10. export const BLOCKS_EXPAND_FAIL = 'BLOCKS_EXPAND_FAIL';
  11. export const BLOCKS_INIT_MODAL = 'BLOCKS_INIT_MODAL';
  12. export function fetchBlocks() {
  13. return (dispatch, getState) => {
  14. dispatch(fetchBlocksRequest());
  15. api(getState).get('/api/v1/blocks').then(response => {
  16. const next = getLinks(response).refs.find(link => link.rel === 'next');
  17. dispatch(importFetchedAccounts(response.data));
  18. dispatch(fetchBlocksSuccess(response.data, next ? next.uri : null));
  19. dispatch(fetchRelationships(response.data.map(item => item.id)));
  20. }).catch(error => dispatch(fetchBlocksFail(error)));
  21. };
  22. };
  23. export function fetchBlocksRequest() {
  24. return {
  25. type: BLOCKS_FETCH_REQUEST,
  26. };
  27. };
  28. export function fetchBlocksSuccess(accounts, next) {
  29. return {
  30. type: BLOCKS_FETCH_SUCCESS,
  31. accounts,
  32. next,
  33. };
  34. };
  35. export function fetchBlocksFail(error) {
  36. return {
  37. type: BLOCKS_FETCH_FAIL,
  38. error,
  39. };
  40. };
  41. export function expandBlocks() {
  42. return (dispatch, getState) => {
  43. const url = getState().getIn(['user_lists', 'blocks', 'next']);
  44. if (url === null) {
  45. return;
  46. }
  47. dispatch(expandBlocksRequest());
  48. api(getState).get(url).then(response => {
  49. const next = getLinks(response).refs.find(link => link.rel === 'next');
  50. dispatch(importFetchedAccounts(response.data));
  51. dispatch(expandBlocksSuccess(response.data, next ? next.uri : null));
  52. dispatch(fetchRelationships(response.data.map(item => item.id)));
  53. }).catch(error => dispatch(expandBlocksFail(error)));
  54. };
  55. };
  56. export function expandBlocksRequest() {
  57. return {
  58. type: BLOCKS_EXPAND_REQUEST,
  59. };
  60. };
  61. export function expandBlocksSuccess(accounts, next) {
  62. return {
  63. type: BLOCKS_EXPAND_SUCCESS,
  64. accounts,
  65. next,
  66. };
  67. };
  68. export function expandBlocksFail(error) {
  69. return {
  70. type: BLOCKS_EXPAND_FAIL,
  71. error,
  72. };
  73. };
  74. export function initBlockModal(account) {
  75. return dispatch => {
  76. dispatch({
  77. type: BLOCKS_INIT_MODAL,
  78. account,
  79. });
  80. dispatch(openModal('BLOCK'));
  81. };
  82. }