您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

reducer.js 3.1KB

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