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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // @flow
  2. import { ReducerRegistry, set } from '../base/redux';
  3. import { PersistenceRegistry } from '../base/storage';
  4. import {
  5. CLEAR_CALENDAR_INTEGRATION,
  6. SET_CALENDAR_AUTH_STATE,
  7. SET_CALENDAR_AUTHORIZATION,
  8. SET_CALENDAR_ERROR,
  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: Never persist the authorization value as it's needed to remain a
  39. * runtime value to see if we need to re-request the calendar permission from
  40. * the user.
  41. */
  42. PersistenceRegistry.register(STORE_NAME, {
  43. integrationType: true,
  44. msAuthState: true
  45. });
  46. ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
  47. if (!isCalendarEnabled(state)) {
  48. return state;
  49. }
  50. switch (action.type) {
  51. case CLEAR_CALENDAR_INTEGRATION:
  52. return DEFAULT_STATE;
  53. case SET_CALENDAR_AUTH_STATE: {
  54. if (!action.msAuthState) {
  55. // received request to delete the state
  56. return set(state, 'msAuthState', undefined);
  57. }
  58. return set(state, 'msAuthState', {
  59. ...state.msAuthState,
  60. ...action.msAuthState
  61. });
  62. }
  63. case SET_CALENDAR_AUTHORIZATION:
  64. return set(state, 'authorization', action.authorization);
  65. case SET_CALENDAR_ERROR:
  66. return set(state, 'error', action.error);
  67. case SET_CALENDAR_EVENTS:
  68. return set(state, 'events', action.events);
  69. case SET_CALENDAR_INTEGRATION:
  70. return {
  71. ...state,
  72. integrationReady: action.integrationReady,
  73. integrationType: action.integrationType
  74. };
  75. case SET_CALENDAR_PROFILE_EMAIL:
  76. return set(state, 'profileEmail', action.email);
  77. case SET_LOADING_CALENDAR_EVENTS:
  78. return set(state, 'isLoadingEvents', action.isLoadingEvents);
  79. }
  80. return state;
  81. });