snackbar.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { showSnackbar, hideSnackbar } from 'misago/reducers/snackbar';
  2. const HIDE_ANIMATION_LENGTH = 300;
  3. const MESSAGE_SHOW_LENGTH = 5000;
  4. export class Snackbar {
  5. init(store) {
  6. this._store = store;
  7. this._timeout = null;
  8. }
  9. alert(message, type) {
  10. if (this._timeout) {
  11. window.clearTimeout(this._timeout);
  12. this._store.dispatch(hideSnackbar());
  13. this._timeout = window.setTimeout(() => {
  14. this._timeout = null;
  15. this.alert(message, type);
  16. }, HIDE_ANIMATION_LENGTH);
  17. } else {
  18. this._store.dispatch(showSnackbar(message, type));
  19. this._timeout = window.setTimeout(() => {
  20. this._store.dispatch(hideSnackbar());
  21. this._timeout = null;
  22. }, MESSAGE_SHOW_LENGTH);
  23. }
  24. }
  25. // shorthands for message types
  26. info(message) {
  27. this.alert(message, 'info');
  28. }
  29. success(message) {
  30. this.alert(message, 'success');
  31. }
  32. warning(message) {
  33. this.alert(message, 'warning');
  34. }
  35. error(message) {
  36. this.alert(message, 'error');
  37. }
  38. // shorthand for api errors
  39. apiError(rejection) {
  40. let message = rejection.detail;
  41. if (!message) {
  42. if (rejection.status === 404) {
  43. message = gettext("Action link is invalid.");
  44. } else {
  45. message = gettext("Unknown error has occured.");
  46. }
  47. }
  48. if (rejection.status === 403 && message === "Permission denied") {
  49. message = gettext("You don't have permission to perform this action.");
  50. }
  51. this.error(message);
  52. }
  53. }
  54. export default new Snackbar();