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.

middleware.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // @flow
  2. import { SET_CONFIG } from '../base/config';
  3. import { ADD_KNOWN_DOMAINS, addKnownDomains } from '../base/known-domains';
  4. import { equals, MiddlewareRegistry } from '../base/redux';
  5. import { APP_STATE_CHANGED } from '../mobile/background/actionTypes';
  6. import { setCalendarAuthorization } from './actions';
  7. import { REFRESH_CALENDAR } from './actionTypes';
  8. import { _fetchCalendarEntries, isCalendarEnabled } from './functions';
  9. isCalendarEnabled()
  10. && MiddlewareRegistry.register(store => next => action => {
  11. switch (action.type) {
  12. case ADD_KNOWN_DOMAINS: {
  13. // XXX Fetch new calendar entries only when an actual domain has
  14. // become known.
  15. const { getState } = store;
  16. const oldValue = getState()['features/base/known-domains'];
  17. const result = next(action);
  18. const newValue = getState()['features/base/known-domains'];
  19. equals(oldValue, newValue)
  20. || _fetchCalendarEntries(store, false, false);
  21. return result;
  22. }
  23. case APP_STATE_CHANGED: {
  24. const result = next(action);
  25. _maybeClearAccessStatus(store, action);
  26. return result;
  27. }
  28. case SET_CONFIG: {
  29. const result = next(action);
  30. // For legacy purposes, we've allowed the deserialization of
  31. // knownDomains and now we're to translate it to base/known-domains.
  32. const state = store.getState()['features/calendar-sync'];
  33. if (state) {
  34. const { knownDomains } = state;
  35. Array.isArray(knownDomains)
  36. && knownDomains.length
  37. && store.dispatch(addKnownDomains(knownDomains));
  38. }
  39. _fetchCalendarEntries(store, false, false);
  40. return result;
  41. }
  42. case REFRESH_CALENDAR: {
  43. const result = next(action);
  44. _fetchCalendarEntries(
  45. store, action.isInteractive, action.forcePermission);
  46. return result;
  47. }
  48. }
  49. return next(action);
  50. });
  51. /**
  52. * Clears the calendar access status when the app comes back from the
  53. * background. This is needed as some users may never quit the app, but puts it
  54. * into the background and we need to try to request for a permission as often
  55. * as possible, but not annoyingly often.
  56. *
  57. * @param {Object} store - The redux store.
  58. * @param {Object} action - The Redux action.
  59. * @private
  60. * @returns {void}
  61. */
  62. function _maybeClearAccessStatus(store, { appState }) {
  63. appState === 'background'
  64. && store.dispatch(setCalendarAuthorization(undefined));
  65. }