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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. if (typeof state.knownDomains !== 'undefined') {
  61. return set(state, 'knownDomains', undefined);
  62. }
  63. break;
  64. case CLEAR_CALENDAR_INTEGRATION:
  65. return DEFAULT_STATE;
  66. case SET_CALENDAR_AUTH_STATE: {
  67. if (!action.msAuthState) {
  68. // received request to delete the state
  69. return set(state, 'msAuthState', undefined);
  70. }
  71. return set(state, 'msAuthState', {
  72. ...state.msAuthState,
  73. ...action.msAuthState
  74. });
  75. }
  76. case SET_CALENDAR_AUTHORIZATION:
  77. return set(state, 'authorization', action.authorization);
  78. case SET_CALENDAR_ERROR:
  79. return set(state, 'error', action.error);
  80. case SET_CALENDAR_EVENTS:
  81. return set(state, 'events', action.events);
  82. case SET_CALENDAR_INTEGRATION:
  83. return {
  84. ...state,
  85. integrationReady: action.integrationReady,
  86. integrationType: action.integrationType
  87. };
  88. case SET_CALENDAR_PROFILE_EMAIL:
  89. return set(state, 'profileEmail', action.email);
  90. case SET_LOADING_CALENDAR_EVENTS:
  91. return set(state, 'isLoadingEvents', action.isLoadingEvents);
  92. }
  93. return state;
  94. });