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

reducer.js 3.2KB

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