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.tsx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import PersistenceRegistry from '../base/redux/PersistenceRegistry';
  2. import ReducerRegistry from '../base/redux/ReducerRegistry';
  3. import { set } from '../base/redux/functions';
  4. import {
  5. CLEAR_CALENDAR_INTEGRATION,
  6. SET_CALENDAR_AUTHORIZATION,
  7. SET_CALENDAR_AUTH_STATE,
  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. /**
  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. export interface ICalendarSyncState {
  27. authorization?: string;
  28. error?: Object;
  29. events: Array<{
  30. calendarId: string;
  31. id: string;
  32. url: string;
  33. }>;
  34. integrationReady: boolean;
  35. integrationType?: string;
  36. isLoadingEvents?: boolean;
  37. msAuthState?: Object;
  38. profileEmail?: string;
  39. }
  40. /**
  41. * Constant for the Redux subtree of the calendar feature.
  42. *
  43. * NOTE: This feature can be disabled and in that case, accessing this subtree
  44. * directly will return undefined and will need a bunch of repetitive type
  45. * checks in other features. Make sure you take care of those checks, or
  46. * consider using the {@code isCalendarEnabled} value to gate features if
  47. * needed.
  48. */
  49. const STORE_NAME = 'features/calendar-sync';
  50. /**
  51. * NOTE: Never persist the authorization value as it's needed to remain a
  52. * runtime value to see if we need to re-request the calendar permission from
  53. * the user.
  54. */
  55. PersistenceRegistry.register(STORE_NAME, {
  56. integrationType: true,
  57. msAuthState: true
  58. });
  59. ReducerRegistry.register<ICalendarSyncState>(STORE_NAME, (state = DEFAULT_STATE, action): ICalendarSyncState => {
  60. switch (action.type) {
  61. case CLEAR_CALENDAR_INTEGRATION:
  62. return DEFAULT_STATE;
  63. case SET_CALENDAR_AUTH_STATE: {
  64. if (!action.msAuthState) {
  65. // received request to delete the state
  66. return set(state, 'msAuthState', undefined);
  67. }
  68. return set(state, 'msAuthState', {
  69. ...state.msAuthState,
  70. ...action.msAuthState
  71. });
  72. }
  73. case SET_CALENDAR_AUTHORIZATION:
  74. return set(state, 'authorization', action.authorization);
  75. case SET_CALENDAR_ERROR:
  76. return set(state, 'error', action.error);
  77. case SET_CALENDAR_EVENTS:
  78. return set(state, 'events', action.events);
  79. case SET_CALENDAR_INTEGRATION:
  80. return {
  81. ...state,
  82. integrationReady: action.integrationReady,
  83. integrationType: action.integrationType
  84. };
  85. case SET_CALENDAR_PROFILE_EMAIL:
  86. return set(state, 'profileEmail', action.email);
  87. case SET_LOADING_CALENDAR_EVENTS:
  88. return set(state, 'isLoadingEvents', action.isLoadingEvents);
  89. }
  90. return state;
  91. });