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.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // @flow
  2. import { toState } from '../base/redux';
  3. import {
  4. clearCalendarIntegration,
  5. setCalendarError,
  6. setLoadingCalendarEvents
  7. } from './actions';
  8. export * from './functions.any';
  9. import {
  10. CALENDAR_TYPE,
  11. ERRORS,
  12. FETCH_END_DAYS,
  13. FETCH_START_DAYS
  14. } from './constants';
  15. import { _updateCalendarEntries } from './functions';
  16. import logger from './logger';
  17. import { googleCalendarApi } from './web/googleCalendar';
  18. import { microsoftCalendarApi } from './web/microsoftCalendar';
  19. /**
  20. * Determines whether the calendar feature is enabled by the web.
  21. *
  22. * @param {Function|Object} stateful - The redux store or {@code getState}
  23. * function.
  24. * @returns {boolean} If the app has enabled the calendar feature, {@code true};
  25. * otherwise, {@code false}.
  26. */
  27. export function isCalendarEnabled(stateful: Function | Object) {
  28. const {
  29. enableCalendarIntegration,
  30. googleApiApplicationClientID,
  31. microsoftApiApplicationClientID
  32. } = toState(stateful)['features/base/config'] || {};
  33. return Boolean(enableCalendarIntegration && (googleApiApplicationClientID || microsoftApiApplicationClientID));
  34. }
  35. /* eslint-disable no-unused-vars */
  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: Object,
  49. maybePromptForPermission: boolean,
  50. forcePermission: ?boolean) {
  51. /* eslint-enable no-unused-vars */
  52. const { dispatch, getState } = store;
  53. const { integrationType } = getState()['features/calendar-sync'];
  54. const integration = _getCalendarIntegration(integrationType);
  55. if (!integration) {
  56. logger.debug('No calendar type available');
  57. return;
  58. }
  59. dispatch(setLoadingCalendarEvents(true));
  60. dispatch(integration.load())
  61. .then(() => dispatch(integration._isSignedIn()))
  62. .then(signedIn => {
  63. if (signedIn) {
  64. return Promise.resolve();
  65. }
  66. return Promise.reject({
  67. error: ERRORS.AUTH_FAILED
  68. });
  69. })
  70. .then(() => dispatch(integration.getCalendarEntries(
  71. FETCH_START_DAYS, FETCH_END_DAYS)))
  72. .then(events => _updateCalendarEntries.call({
  73. dispatch,
  74. getState
  75. }, events))
  76. .then(() => {
  77. dispatch(setCalendarError());
  78. }, error => {
  79. logger.error('Error fetching calendar.', error);
  80. if (error.error === ERRORS.AUTH_FAILED) {
  81. dispatch(clearCalendarIntegration());
  82. }
  83. dispatch(setCalendarError(error));
  84. })
  85. .then(() => dispatch(setLoadingCalendarEvents(false)));
  86. }
  87. /**
  88. * Returns the calendar API implementation by specified type.
  89. *
  90. * @param {string} calendarType - The calendar type API as defined in
  91. * the constant {@link CALENDAR_TYPE}.
  92. * @private
  93. * @returns {Object|undefined}
  94. */
  95. export function _getCalendarIntegration(calendarType: string) {
  96. switch (calendarType) {
  97. case CALENDAR_TYPE.GOOGLE:
  98. return googleCalendarApi;
  99. case CALENDAR_TYPE.MICROSOFT:
  100. return microsoftCalendarApi;
  101. }
  102. }