about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/actions/directory.js
blob: 9fbfb7f5b7588446fb54def832199b33c6d4ff28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import api from 'flavours/glitch/util/api';
import { importFetchedAccounts } from './importer';
import { fetchRelationships } from './accounts';

export const DIRECTORY_FETCH_REQUEST = 'DIRECTORY_FETCH_REQUEST';
export const DIRECTORY_FETCH_SUCCESS = 'DIRECTORY_FETCH_SUCCESS';
export const DIRECTORY_FETCH_FAIL    = 'DIRECTORY_FETCH_FAIL';

export const DIRECTORY_EXPAND_REQUEST = 'DIRECTORY_EXPAND_REQUEST';
export const DIRECTORY_EXPAND_SUCCESS = 'DIRECTORY_EXPAND_SUCCESS';
export const DIRECTORY_EXPAND_FAIL    = 'DIRECTORY_EXPAND_FAIL';

export const fetchDirectory = params => (dispatch, getState) => {
  dispatch(fetchDirectoryRequest());

  api(getState).get('/api/v1/directory', { params: { ...params, limit: 20 } }).then(({ data }) => {
    dispatch(importFetchedAccounts(data));
    dispatch(fetchDirectorySuccess(data));
    dispatch(fetchRelationships(data.map(x => x.id)));
  }).catch(error => dispatch(fetchDirectoryFail(error)));
};

export const fetchDirectoryRequest = () => ({
  type: DIRECTORY_FETCH_REQUEST,
});

export const fetchDirectorySuccess = accounts => ({
  type: DIRECTORY_FETCH_SUCCESS,
  accounts,
});

export const fetchDirectoryFail = error => ({
  type: DIRECTORY_FETCH_FAIL,
  error,
});

export const expandDirectory = params => (dispatch, getState) => {
  dispatch(expandDirectoryRequest());

  const loadedItems = getState().getIn(['user_lists', 'directory', 'items']).size;

  api(getState).get('/api/v1/directory', { params: { ...params, offset: loadedItems, limit: 20 } }).then(({ data }) => {
    dispatch(importFetchedAccounts(data));
    dispatch(expandDirectorySuccess(data));
    dispatch(fetchRelationships(data.map(x => x.id)));
  }).catch(error => dispatch(expandDirectoryFail(error)));
};

export const expandDirectoryRequest = () => ({
  type: DIRECTORY_EXPAND_REQUEST,
});

export const expandDirectorySuccess = accounts => ({
  type: DIRECTORY_EXPAND_SUCCESS,
  accounts,
});

export const expandDirectoryFail = error => ({
  type: DIRECTORY_EXPAND_FAIL,
  error,
});