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.

functions.web.ts 3.6KB

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