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

functions.web.ts 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { IStore } from '../app/types';
  2. import { IStateful } from '../base/app/types';
  3. import { toState } from '../base/redux/functions';
  4. import {
  5. clearCalendarIntegration,
  6. setCalendarError,
  7. setLoadingCalendarEvents
  8. } from './actions.web';
  9. export * from './functions.any';
  10. import {
  11. CALENDAR_TYPE,
  12. ERRORS,
  13. FETCH_END_DAYS,
  14. FETCH_START_DAYS
  15. } from './constants';
  16. import { _updateCalendarEntries } from './functions.web';
  17. import logger from './logger';
  18. import { googleCalendarApi } from './web/googleCalendar';
  19. import { microsoftCalendarApi } from './web/microsoftCalendar';
  20. /**
  21. * Determines whether the calendar feature is enabled by the web.
  22. *
  23. * @param {Function|Object} stateful - The redux store or {@code getState}
  24. * function.
  25. * @returns {boolean} If the app has enabled the calendar feature, {@code true};
  26. * otherwise, {@code false}.
  27. */
  28. export function isCalendarEnabled(stateful: IStateful) {
  29. const {
  30. enableCalendarIntegration,
  31. googleApiApplicationClientID,
  32. microsoftApiApplicationClientID
  33. } = toState(stateful)['features/base/config'] || {};
  34. return Boolean(enableCalendarIntegration && (googleApiApplicationClientID || microsoftApiApplicationClientID));
  35. }
  36. /**
  37. * Reads the user's calendar and updates the stored entries if need be.
  38. *
  39. * @param {Object} store - The redux store.
  40. * @param {boolean} _maybePromptForPermission - Flag to tell the app if it should
  41. * prompt for a calendar permission if it wasn't granted yet.
  42. * @param {boolean|undefined} _forcePermission - Whether to force to re-ask for
  43. * the permission or not.
  44. * @private
  45. * @returns {void}
  46. */
  47. export function _fetchCalendarEntries(
  48. store: IStore,
  49. _maybePromptForPermission: boolean,
  50. _forcePermission?: boolean) {
  51. const { dispatch, getState } = store;
  52. const { integrationType = '' } = getState()['features/calendar-sync'];
  53. const integration = _getCalendarIntegration(integrationType);
  54. if (!integration) {
  55. logger.debug('No calendar type available');
  56. return;
  57. }
  58. dispatch(setLoadingCalendarEvents(true));
  59. dispatch(integration.load())
  60. .then(() => dispatch(integration._isSignedIn()))
  61. .then((signedIn: boolean) => {
  62. if (signedIn) {
  63. return Promise.resolve();
  64. }
  65. return Promise.reject({
  66. error: ERRORS.AUTH_FAILED
  67. });
  68. })
  69. .then(() => dispatch(integration.getCalendarEntries(
  70. FETCH_START_DAYS, FETCH_END_DAYS)))
  71. .then((events: Object[]) => _updateCalendarEntries.call({
  72. dispatch,
  73. getState
  74. }, events))
  75. .then(() => {
  76. dispatch(setCalendarError());
  77. }, (error: any) => {
  78. logger.error('Error fetching calendar.', error);
  79. if (error.error === ERRORS.AUTH_FAILED) {
  80. dispatch(clearCalendarIntegration());
  81. }
  82. dispatch(setCalendarError(error));
  83. })
  84. .then(() => dispatch(setLoadingCalendarEvents(false)));
  85. }
  86. /**
  87. * Returns the calendar API implementation by specified type.
  88. *
  89. * @param {string} calendarType - The calendar type API as defined in
  90. * the constant {@link CALENDAR_TYPE}.
  91. * @private
  92. * @returns {Object|undefined}
  93. */
  94. export function _getCalendarIntegration(calendarType: string) {
  95. switch (calendarType) {
  96. case CALENDAR_TYPE.GOOGLE:
  97. return googleCalendarApi;
  98. case CALENDAR_TYPE.MICROSOFT:
  99. return microsoftCalendarApi;
  100. }
  101. }