2020-05-29 14:14:16 +00:00
|
|
|
import api from '../api';
|
|
|
|
import { debounce } from 'lodash';
|
|
|
|
import compareId from '../compare_id';
|
|
|
|
import { showAlertForError } from './alerts';
|
|
|
|
|
|
|
|
export const MARKERS_SUBMIT_SUCCESS = 'MARKERS_SUBMIT_SUCCESS';
|
|
|
|
|
|
|
|
export const synchronouslySubmitMarkers = () => (dispatch, getState) => {
|
2019-09-06 11:55:51 +00:00
|
|
|
const accessToken = getState().getIn(['meta', 'access_token'], '');
|
2020-05-29 14:14:16 +00:00
|
|
|
const params = _buildParams(getState());
|
|
|
|
|
|
|
|
if (Object.keys(params).length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:25:57 +00:00
|
|
|
// The Fetch API allows us to perform requests that will be carried out
|
|
|
|
// after the page closes. But that only works if the `keepalive` attribute
|
|
|
|
// is supported.
|
|
|
|
if (window.fetch && 'keepalive' in new Request('')) {
|
2020-05-29 14:14:16 +00:00
|
|
|
fetch('/api/v1/markers', {
|
|
|
|
keepalive: true,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
|
|
},
|
|
|
|
body: JSON.stringify(params),
|
|
|
|
});
|
2020-05-29 17:25:57 +00:00
|
|
|
return;
|
|
|
|
} else if (navigator && navigator.sendBeacon) {
|
|
|
|
// Failing that, we can use sendBeacon, but we have to encode the data as
|
|
|
|
// FormData for DoorKeeper to recognize the token.
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('bearer_token', accessToken);
|
|
|
|
for (const [id, value] of Object.entries(params)) {
|
|
|
|
formData.append(`${id}[last_read_id]`, value.last_read_id);
|
|
|
|
}
|
|
|
|
if (navigator.sendBeacon('/api/v1/markers', formData)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If neither Fetch nor sendBeacon worked, try to perform a synchronous
|
|
|
|
// request.
|
|
|
|
try {
|
2020-05-29 14:14:16 +00:00
|
|
|
const client = new XMLHttpRequest();
|
|
|
|
|
|
|
|
client.open('POST', '/api/v1/markers', false);
|
|
|
|
client.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
client.setRequestHeader('Authorization', `Bearer ${accessToken}`);
|
|
|
|
client.SUBMIT(JSON.stringify(params));
|
2020-05-29 17:25:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Do not make the BeforeUnload handler error out
|
2020-05-29 14:14:16 +00:00
|
|
|
}
|
|
|
|
};
|
2019-09-06 11:55:51 +00:00
|
|
|
|
2020-05-29 14:14:16 +00:00
|
|
|
const _buildParams = (state) => {
|
|
|
|
const params = {};
|
2019-09-06 11:55:51 +00:00
|
|
|
|
2020-05-29 14:14:16 +00:00
|
|
|
const lastHomeId = state.getIn(['timelines', 'home', 'items', 0]);
|
|
|
|
const lastNotificationId = state.getIn(['notifications', 'items', 0, 'id']);
|
|
|
|
|
|
|
|
if (lastHomeId && compareId(lastHomeId, state.getIn(['markers', 'home'])) > 0) {
|
2019-09-06 11:55:51 +00:00
|
|
|
params.home = {
|
|
|
|
last_read_id: lastHomeId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-29 14:14:16 +00:00
|
|
|
if (lastNotificationId && compareId(lastNotificationId, state.getIn(['markers', 'notifications'])) > 0) {
|
2019-09-06 11:55:51 +00:00
|
|
|
params.notifications = {
|
|
|
|
last_read_id: lastNotificationId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-29 14:14:16 +00:00
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
|
|
|
const debouncedSubmitMarkers = debounce((dispatch, getState) => {
|
|
|
|
const params = _buildParams(getState());
|
|
|
|
|
2019-09-06 11:55:51 +00:00
|
|
|
if (Object.keys(params).length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-29 14:14:16 +00:00
|
|
|
api().post('/api/v1/markers', params).then(() => {
|
|
|
|
dispatch(submitMarkersSuccess(params));
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch(showAlertForError(error));
|
|
|
|
});
|
|
|
|
}, 300000, { leading: true, trailing: true });
|
|
|
|
|
|
|
|
export function submitMarkersSuccess({ home, notifications }) {
|
|
|
|
return {
|
|
|
|
type: MARKERS_SUBMIT_SUCCESS,
|
|
|
|
home: (home || {}).last_read_id,
|
|
|
|
notifications: (notifications || {}).last_read_id,
|
|
|
|
};
|
|
|
|
};
|
2019-09-06 11:55:51 +00:00
|
|
|
|
2020-05-29 14:14:16 +00:00
|
|
|
export function submitMarkers() {
|
|
|
|
return (dispatch, getState) => debouncedSubmitMarkers(dispatch, getState);
|
2019-09-06 11:55:51 +00:00
|
|
|
};
|