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 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. isCalendarEnabled()
  43. && PersistenceRegistry.register(STORE_NAME, {
  44. integrationType: true,
  45. msAuthState: true
  46. });
  47. isCalendarEnabled()
  48. && ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
  49. switch (action.type) {
  50. case CLEAR_CALENDAR_INTEGRATION:
  51. return DEFAULT_STATE;
  52. case SET_CALENDAR_AUTH_STATE: {
  53. if (!action.msAuthState) {
  54. // received request to delete the state
  55. return set(state, 'msAuthState', undefined);
  56. }
  57. return set(state, 'msAuthState', {
  58. ...state.msAuthState,
  59. ...action.msAuthState
  60. });
  61. }
  62. case SET_CALENDAR_AUTHORIZATION:
  63. return set(state, 'authorization', action.authorization);
  64. case SET_CALENDAR_ERROR:
  65. return set(state, 'error', action.error);
  66. case SET_CALENDAR_EVENTS:
  67. return set(state, 'events', action.events);
  68. case SET_CALENDAR_INTEGRATION:
  69. return {
  70. ...state,
  71. integrationReady: action.integrationReady,
  72. integrationType: action.integrationType
  73. };
  74. case SET_CALENDAR_PROFILE_EMAIL:
  75. return set(state, 'profileEmail', action.email);
  76. case SET_LOADING_CALENDAR_EVENTS:
  77. return set(state, 'isLoadingEvents', action.isLoadingEvents);
  78. }
  79. return state;
  80. });