bigbuffet-rw/app/soapbox/actions/alerts.js

60 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import { defineMessages } from 'react-intl';
const messages = defineMessages({
unexpectedTitle: { id: 'alert.unexpected.title', defaultMessage: 'Oops!' },
unexpectedMessage: { id: 'alert.unexpected.message', defaultMessage: 'An unexpected error occurred.' },
});
export const ALERT_SHOW = 'ALERT_SHOW';
export const ALERT_DISMISS = 'ALERT_DISMISS';
export const ALERT_CLEAR = 'ALERT_CLEAR';
export function dismissAlert(alert) {
return {
type: ALERT_DISMISS,
alert,
};
};
export function clearAlert() {
return {
type: ALERT_CLEAR,
};
};
export function showAlert(title = messages.unexpectedTitle, message = messages.unexpectedMessage, severity = 'info') {
2020-03-27 13:59:38 -07:00
return {
type: ALERT_SHOW,
title,
message,
severity,
2020-03-27 13:59:38 -07:00
};
};
export function showAlertForError(error) {
if (error.response) {
const { data, status, statusText } = error.response;
2021-05-21 18:16:09 -07:00
if (status === 502) {
return showAlert('', 'The server is down', 'error');
}
2020-03-27 13:59:38 -07:00
if (status === 404 || status === 410) {
// Skip these errors as they are reflected in the UI
return {};
}
let message = statusText;
const title = `${status}`;
2020-03-27 13:59:38 -07:00
if (data.error) {
message = data.error;
}
return showAlert(title, message, 'error');
2020-03-27 13:59:38 -07:00
} else {
console.error(error);
return showAlert(undefined, undefined, 'error');
2020-03-27 13:59:38 -07:00
}
}