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.

44 lines
1.4 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 { TIMELINE_REFRESH_FAIL } from '../actions/timelines';
  5. import { NOTIFICATION_DISMISS, NOTIFICATION_CLEAR } from '../actions/notifications';
  6. import Immutable from 'immutable';
  7. const initialState = Immutable.List();
  8. function notificationFromError(state, error) {
  9. let n = Immutable.Map({
  10. key: state.size > 0 ? state.last().get('key') + 1 : 0,
  11. message: ''
  12. });
  13. if (error.response) {
  14. n = n.withMutations(map => {
  15. map.set('message', error.response.statusText);
  16. map.set('title', `${error.response.status}`);
  17. });
  18. } else {
  19. n = n.set('message', `${error}`);
  20. }
  21. return state.push(n);
  22. };
  23. export default function notifications(state = initialState, action) {
  24. switch(action.type) {
  25. case COMPOSE_SUBMIT_FAIL:
  26. case COMPOSE_UPLOAD_FAIL:
  27. case FOLLOW_SUBMIT_FAIL:
  28. case REBLOG_FAIL:
  29. case FAVOURITE_FAIL:
  30. case TIMELINE_REFRESH_FAIL:
  31. return notificationFromError(state, action.error);
  32. case NOTIFICATION_DISMISS:
  33. return state.filterNot(item => item.get('key') === action.notification.key);
  34. case NOTIFICATION_CLEAR:
  35. return state.clear();
  36. default:
  37. return state;
  38. }
  39. };