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.

60 lines
1.9 KiB

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