[Glitch] Fix alert when failing to save timeline markers in web UI

Port 905032d903 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
lolsob-rspec
Eugen Rochko 2020-12-07 03:23:43 +01:00 committed by Claire
parent 303aa05c63
commit cc95ab392f
1 changed files with 8 additions and 5 deletions

View File

@ -1,7 +1,6 @@
import api from 'flavours/glitch/util/api'; import api from 'flavours/glitch/util/api';
import { debounce } from 'lodash'; import { debounce } from 'lodash';
import compareId from 'flavours/glitch/util/compare_id'; import compareId from 'flavours/glitch/util/compare_id';
import { showAlertForError } from './alerts';
export const MARKERS_FETCH_REQUEST = 'MARKERS_FETCH_REQUEST'; export const MARKERS_FETCH_REQUEST = 'MARKERS_FETCH_REQUEST';
export const MARKERS_FETCH_SUCCESS = 'MARKERS_FETCH_SUCCESS'; export const MARKERS_FETCH_SUCCESS = 'MARKERS_FETCH_SUCCESS';
@ -29,15 +28,19 @@ export const synchronouslySubmitMarkers = () => (dispatch, getState) => {
}, },
body: JSON.stringify(params), body: JSON.stringify(params),
}); });
return; return;
} else if (navigator && navigator.sendBeacon) { } else if (navigator && navigator.sendBeacon) {
// Failing that, we can use sendBeacon, but we have to encode the data as // Failing that, we can use sendBeacon, but we have to encode the data as
// FormData for DoorKeeper to recognize the token. // FormData for DoorKeeper to recognize the token.
const formData = new FormData(); const formData = new FormData();
formData.append('bearer_token', accessToken); formData.append('bearer_token', accessToken);
for (const [id, value] of Object.entries(params)) { for (const [id, value] of Object.entries(params)) {
formData.append(`${id}[last_read_id]`, value.last_read_id); formData.append(`${id}[last_read_id]`, value.last_read_id);
} }
if (navigator.sendBeacon('/api/v1/markers', formData)) { if (navigator.sendBeacon('/api/v1/markers', formData)) {
return; return;
} }
@ -85,11 +88,9 @@ const debouncedSubmitMarkers = debounce((dispatch, getState) => {
return; return;
} }
api().post('/api/v1/markers', params).then(() => { api(getState).post('/api/v1/markers', params).then(() => {
dispatch(submitMarkersSuccess(params)); dispatch(submitMarkersSuccess(params));
}).catch(error => { }).catch(() => {});
dispatch(showAlertForError(error));
});
}, 300000, { leading: true, trailing: true }); }, 300000, { leading: true, trailing: true });
export function submitMarkersSuccess({ home, notifications }) { export function submitMarkersSuccess({ home, notifications }) {
@ -102,9 +103,11 @@ export function submitMarkersSuccess({ home, notifications }) {
export function submitMarkers(params = {}) { export function submitMarkers(params = {}) {
const result = (dispatch, getState) => debouncedSubmitMarkers(dispatch, getState); const result = (dispatch, getState) => debouncedSubmitMarkers(dispatch, getState);
if (params.immediate === true) { if (params.immediate === true) {
debouncedSubmitMarkers.flush(); debouncedSubmitMarkers.flush();
} }
return result; return result;
}; };