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.

148 lines
4.0 KiB

  1. import api from '../api';
  2. import { debounce } from 'lodash';
  3. import compareId from '../compare_id';
  4. export const MARKERS_FETCH_REQUEST = 'MARKERS_FETCH_REQUEST';
  5. export const MARKERS_FETCH_SUCCESS = 'MARKERS_FETCH_SUCCESS';
  6. export const MARKERS_FETCH_FAIL = 'MARKERS_FETCH_FAIL';
  7. export const MARKERS_SUBMIT_SUCCESS = 'MARKERS_SUBMIT_SUCCESS';
  8. export const synchronouslySubmitMarkers = () => (dispatch, getState) => {
  9. const accessToken = getState().getIn(['meta', 'access_token'], '');
  10. const params = _buildParams(getState());
  11. if (Object.keys(params).length === 0) {
  12. return;
  13. }
  14. // The Fetch API allows us to perform requests that will be carried out
  15. // after the page closes. But that only works if the `keepalive` attribute
  16. // is supported.
  17. if (window.fetch && 'keepalive' in new Request('')) {
  18. fetch('/api/v1/markers', {
  19. keepalive: true,
  20. method: 'POST',
  21. headers: {
  22. 'Content-Type': 'application/json',
  23. 'Authorization': `Bearer ${accessToken}`,
  24. },
  25. body: JSON.stringify(params),
  26. });
  27. return;
  28. } else if (navigator && navigator.sendBeacon) {
  29. // Failing that, we can use sendBeacon, but we have to encode the data as
  30. // FormData for DoorKeeper to recognize the token.
  31. const formData = new FormData();
  32. formData.append('bearer_token', accessToken);
  33. for (const [id, value] of Object.entries(params)) {
  34. formData.append(`${id}[last_read_id]`, value.last_read_id);
  35. }
  36. if (navigator.sendBeacon('/api/v1/markers', formData)) {
  37. return;
  38. }
  39. }
  40. // If neither Fetch nor sendBeacon worked, try to perform a synchronous
  41. // request.
  42. try {
  43. const client = new XMLHttpRequest();
  44. client.open('POST', '/api/v1/markers', false);
  45. client.setRequestHeader('Content-Type', 'application/json');
  46. client.setRequestHeader('Authorization', `Bearer ${accessToken}`);
  47. client.SUBMIT(JSON.stringify(params));
  48. } catch (e) {
  49. // Do not make the BeforeUnload handler error out
  50. }
  51. };
  52. const _buildParams = (state) => {
  53. const params = {};
  54. const lastHomeId = state.getIn(['timelines', 'home', 'items']).find(item => item !== null);
  55. const lastNotificationId = state.getIn(['notifications', 'lastReadId']);
  56. if (lastHomeId && compareId(lastHomeId, state.getIn(['markers', 'home'])) > 0) {
  57. params.home = {
  58. last_read_id: lastHomeId,
  59. };
  60. }
  61. if (lastNotificationId && compareId(lastNotificationId, state.getIn(['markers', 'notifications'])) > 0) {
  62. params.notifications = {
  63. last_read_id: lastNotificationId,
  64. };
  65. }
  66. return params;
  67. };
  68. const debouncedSubmitMarkers = debounce((dispatch, getState) => {
  69. const params = _buildParams(getState());
  70. if (Object.keys(params).length === 0) {
  71. return;
  72. }
  73. api(getState).post('/api/v1/markers', params).then(() => {
  74. dispatch(submitMarkersSuccess(params));
  75. }).catch(() => {});
  76. }, 300000, { leading: true, trailing: true });
  77. export function submitMarkersSuccess({ home, notifications }) {
  78. return {
  79. type: MARKERS_SUBMIT_SUCCESS,
  80. home: (home || {}).last_read_id,
  81. notifications: (notifications || {}).last_read_id,
  82. };
  83. };
  84. export function submitMarkers(params = {}) {
  85. const result = (dispatch, getState) => debouncedSubmitMarkers(dispatch, getState);
  86. if (params.immediate === true) {
  87. debouncedSubmitMarkers.flush();
  88. }
  89. return result;
  90. };
  91. export const fetchMarkers = () => (dispatch, getState) => {
  92. const params = { timeline: ['notifications'] };
  93. dispatch(fetchMarkersRequest());
  94. api(getState).get('/api/v1/markers', { params }).then(response => {
  95. dispatch(fetchMarkersSuccess(response.data));
  96. }).catch(error => {
  97. dispatch(fetchMarkersFail(error));
  98. });
  99. };
  100. export function fetchMarkersRequest() {
  101. return {
  102. type: MARKERS_FETCH_REQUEST,
  103. skipLoading: true,
  104. };
  105. };
  106. export function fetchMarkersSuccess(markers) {
  107. return {
  108. type: MARKERS_FETCH_SUCCESS,
  109. markers,
  110. skipLoading: true,
  111. };
  112. };
  113. export function fetchMarkersFail(error) {
  114. return {
  115. type: MARKERS_FETCH_FAIL,
  116. error,
  117. skipLoading: true,
  118. skipAlert: true,
  119. };
  120. };