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.

reducer.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // @flow
  2. import { APP_WILL_MOUNT } from '../base/app';
  3. import { ReducerRegistry, set } from '../base/redux';
  4. import { PersistenceRegistry } from '../base/storage';
  5. import {
  6. CLEAR_CALENDAR_INTEGRATION,
  7. SET_CALENDAR_AUTH_STATE,
  8. SET_CALENDAR_AUTHORIZATION,
  9. SET_CALENDAR_ERROR,
  10. SET_CALENDAR_EVENTS,
  11. SET_CALENDAR_INTEGRATION,
  12. SET_CALENDAR_PROFILE_EMAIL,
  13. SET_LOADING_CALENDAR_EVENTS
  14. } from './actionTypes';
  15. import { isCalendarEnabled } from './functions';
  16. /**
  17. * The default state of the calendar feature.
  18. *
  19. * @type {Object}
  20. */
  21. const DEFAULT_STATE = {
  22. authorization: undefined,
  23. events: [],
  24. integrationReady: false,
  25. integrationType: undefined,
  26. msAuthState: undefined
  27. };
  28. /**
  29. * Constant for the Redux subtree of the calendar feature.
  30. *
  31. * NOTE: This feature can be disabled and in that case, accessing this subtree
  32. * directly will return undefined and will need a bunch of repetitive type
  33. * checks in other features. Make sure you take care of those checks, or
  34. * consider using the {@code isCalendarEnabled} value to gate features if
  35. * needed.
  36. */
  37. const STORE_NAME = 'features/calendar-sync';
  38. /**
  39. * NOTE 1: For legacy purposes, read any {@code knownDomains} persisted by the
  40. * feature calendar-sync.
  41. *
  42. * NOTE 2: Never persist the authorization value as it's needed to remain a
  43. * runtime value to see if we need to re-request the calendar permission from
  44. * the user.
  45. */
  46. isCalendarEnabled()
  47. && PersistenceRegistry.register(STORE_NAME, {
  48. integrationType: true,
  49. knownDomains: true,
  50. msAuthState: true
  51. });
  52. isCalendarEnabled()
  53. && ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
  54. switch (action.type) {
  55. case APP_WILL_MOUNT:
  56. // For legacy purposes, we've allowed the deserialization of
  57. // knownDomains. At this point, it should have already been
  58. // translated into the new state format (namely, base/known-domains)
  59. // and the app no longer needs it.
  60. // $FlowFixMe
  61. if (typeof state.knownDomains !== 'undefined') {
  62. return set(state, 'knownDomains', undefined);
  63. }
  64. break;
  65. case CLEAR_CALENDAR_INTEGRATION:
  66. return DEFAULT_STATE;
  67. case SET_CALENDAR_AUTH_STATE: {
  68. if (!action.msAuthState) {
  69. // received request to delete the state
  70. return set(state, 'msAuthState', undefined);
  71. }
  72. return set(state, 'msAuthState', {
  73. ...state.msAuthState,
  74. ...action.msAuthState
  75. });
  76. }
  77. case SET_CALENDAR_AUTHORIZATION:
  78. return set(state, 'authorization', action.authorization);
  79. case SET_CALENDAR_ERROR:
  80. return set(state, 'error', action.error);
  81. case SET_CALENDAR_EVENTS:
  82. return set(state, 'events', action.events);
  83. case SET_CALENDAR_INTEGRATION:
  84. return {
  85. ...state,
  86. integrationReady: action.integrationReady,
  87. integrationType: action.integrationType
  88. };
  89. case SET_CALENDAR_PROFILE_EMAIL:
  90. return set(state, 'profileEmail', action.email);
  91. case SET_LOADING_CALENDAR_EVENTS:
  92. return set(state, 'isLoadingEvents', action.isLoadingEvents);
  93. }
  94. return state;
  95. });