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

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