Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

reducer.tsx 2.8KB

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