about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/actions/markers.js
blob: 7ffab404d7b8ecb753b68afd7a341082c9b77c36 (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
62
63
64
65
66
67
68
69
70
71
72
import api from 'flavours/glitch/util/api';

export const MARKERS_FETCH_REQUEST = 'MARKERS_FETCH_REQUEST';
export const MARKERS_FETCH_SUCCESS = 'MARKERS_FETCH_SUCCESS';
export const MARKERS_FETCH_FAIL    = 'MARKERS_FETCH_FAIL';

export const submitMarkers = () => (dispatch, getState) => {
  const accessToken = getState().getIn(['meta', 'access_token'], '');
  const params      = {};

  const lastHomeId         = getState().getIn(['timelines', 'home', 'items', 0]);
  const lastNotificationId = getState().getIn(['notifications', 'lastReadId']);

  if (lastHomeId) {
    params.home = {
      last_read_id: lastHomeId,
    };
  }

  if (lastNotificationId && lastNotificationId !== '0') {
    params.notifications = {
      last_read_id: lastNotificationId,
    };
  }

  if (Object.keys(params).length === 0) {
    return;
  }

  const client = new XMLHttpRequest();

  client.open('POST', '/api/v1/markers', false);
  client.setRequestHeader('Content-Type', 'application/json');
  client.setRequestHeader('Authorization', `Bearer ${accessToken}`);
  client.send(JSON.stringify(params));
};

export const fetchMarkers = () => (dispatch, getState) => {
    const params = { timeline: ['notifications'] };

    dispatch(fetchMarkersRequest());

    api(getState).get('/api/v1/markers', { params }).then(response => {
      dispatch(fetchMarkersSuccess(response.data));
    }).catch(error => {
      dispatch(fetchMarkersFail(error));
    });
};

export function fetchMarkersRequest() {
  return {
    type: MARKERS_FETCH_REQUEST,
    skipLoading: true,
  };
};

export function fetchMarkersSuccess(markers) {
  return {
    type: MARKERS_FETCH_SUCCESS,
    markers,
    skipLoading: true,
  };
};

export function fetchMarkersFail(error) {
  return {
    type: MARKERS_FETCH_FAIL,
    error,
    skipLoading: true,
    skipAlert: true,
  };
};