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.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // @flow
  2. import { PersistenceRegistry, ReducerRegistry, set } from '../base/redux';
  3. import {
  4. CLEAR_CALENDAR_INTEGRATION,
  5. SET_CALENDAR_AUTH_STATE,
  6. SET_CALENDAR_AUTHORIZATION,
  7. SET_CALENDAR_ERROR,
  8. SET_CALENDAR_EVENTS,
  9. SET_CALENDAR_INTEGRATION,
  10. SET_CALENDAR_PROFILE_EMAIL,
  11. SET_LOADING_CALENDAR_EVENTS
  12. } from './actionTypes';
  13. /**
  14. * The default state of the calendar feature.
  15. *
  16. * @type {Object}
  17. */
  18. const DEFAULT_STATE = {
  19. authorization: undefined,
  20. events: [],
  21. integrationReady: false,
  22. integrationType: undefined,
  23. msAuthState: undefined
  24. };
  25. /**
  26. * Constant for the Redux subtree of the calendar feature.
  27. *
  28. * NOTE: This feature can be disabled and in that case, accessing this subtree
  29. * directly will return undefined and will need a bunch of repetitive type
  30. * checks in other features. Make sure you take care of those checks, or
  31. * consider using the {@code isCalendarEnabled} value to gate features if
  32. * needed.
  33. */
  34. const STORE_NAME = 'features/calendar-sync';
  35. /**
  36. * NOTE: Never persist the authorization value as it's needed to remain a
  37. * runtime value to see if we need to re-request the calendar permission from
  38. * the user.
  39. */
  40. PersistenceRegistry.register(STORE_NAME, {
  41. integrationType: true,
  42. msAuthState: true
  43. });
  44. ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
  45. switch (action.type) {
  46. case CLEAR_CALENDAR_INTEGRATION:
  47. return DEFAULT_STATE;
  48. case SET_CALENDAR_AUTH_STATE: {
  49. if (!action.msAuthState) {
  50. // received request to delete the state
  51. return set(state, 'msAuthState', undefined);
  52. }
  53. return set(state, 'msAuthState', {
  54. ...state.msAuthState,
  55. ...action.msAuthState
  56. });
  57. }
  58. case SET_CALENDAR_AUTHORIZATION:
  59. return set(state, 'authorization', action.authorization);
  60. case SET_CALENDAR_ERROR:
  61. return set(state, 'error', action.error);
  62. case SET_CALENDAR_EVENTS:
  63. return set(state, 'events', action.events);
  64. case SET_CALENDAR_INTEGRATION:
  65. return {
  66. ...state,
  67. integrationReady: action.integrationReady,
  68. integrationType: action.integrationType
  69. };
  70. case SET_CALENDAR_PROFILE_EMAIL:
  71. return set(state, 'profileEmail', action.email);
  72. case SET_LOADING_CALENDAR_EVENTS:
  73. return set(state, 'isLoadingEvents', action.isLoadingEvents);
  74. }
  75. return state;
  76. });