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.

73 lines
2.2 KiB

  1. import { COMPOSE_SUBMIT_FAIL, COMPOSE_UPLOAD_FAIL } from '../actions/compose';
  2. import { FOLLOW_SUBMIT_FAIL } from '../actions/follow';
  3. import {
  4. REBLOG_FAIL,
  5. UNREBLOG_FAIL,
  6. FAVOURITE_FAIL,
  7. UNFAVOURITE_FAIL
  8. } from '../actions/interactions';
  9. import {
  10. TIMELINE_REFRESH_FAIL,
  11. TIMELINE_EXPAND_FAIL
  12. } from '../actions/timelines';
  13. import { NOTIFICATION_DISMISS, NOTIFICATION_CLEAR } from '../actions/notifications';
  14. import {
  15. ACCOUNT_FETCH_FAIL,
  16. ACCOUNT_FOLLOW_FAIL,
  17. ACCOUNT_UNFOLLOW_FAIL,
  18. ACCOUNT_TIMELINE_FETCH_FAIL,
  19. ACCOUNT_TIMELINE_EXPAND_FAIL
  20. } from '../actions/accounts';
  21. import {
  22. STATUS_FETCH_FAIL,
  23. STATUS_DELETE_FAIL
  24. } from '../actions/statuses';
  25. import Immutable from 'immutable';
  26. const initialState = Immutable.List();
  27. function notificationFromError(state, error) {
  28. let n = Immutable.Map({
  29. key: state.size > 0 ? state.last().get('key') + 1 : 0,
  30. message: ''
  31. });
  32. if (error.response) {
  33. n = n.withMutations(map => {
  34. map.set('message', error.response.statusText);
  35. map.set('title', `${error.response.status}`);
  36. });
  37. } else {
  38. n = n.set('message', `${error}`);
  39. }
  40. return state.push(n);
  41. };
  42. export default function notifications(state = initialState, action) {
  43. switch(action.type) {
  44. case COMPOSE_SUBMIT_FAIL:
  45. case COMPOSE_UPLOAD_FAIL:
  46. case FOLLOW_SUBMIT_FAIL:
  47. case REBLOG_FAIL:
  48. case FAVOURITE_FAIL:
  49. case TIMELINE_REFRESH_FAIL:
  50. case TIMELINE_EXPAND_FAIL:
  51. case ACCOUNT_FETCH_FAIL:
  52. case ACCOUNT_FOLLOW_FAIL:
  53. case ACCOUNT_UNFOLLOW_FAIL:
  54. case ACCOUNT_TIMELINE_FETCH_FAIL:
  55. case ACCOUNT_TIMELINE_EXPAND_FAIL:
  56. case STATUS_FETCH_FAIL:
  57. case STATUS_DELETE_FAIL:
  58. case UNREBLOG_FAIL:
  59. case UNFAVOURITE_FAIL:
  60. return notificationFromError(state, action.error);
  61. case NOTIFICATION_DISMISS:
  62. return state.filterNot(item => item.get('key') === action.notification.key);
  63. case NOTIFICATION_CLEAR:
  64. return state.clear();
  65. default:
  66. return state;
  67. }
  68. };