您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

reducer.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // @flow
  2. import { ReducerRegistry } from '../base/redux';
  3. import { PersistenceRegistry } from '../base/storage';
  4. import {
  5. ADD_KNOWN_DOMAIN,
  6. SET_CALENDAR_AUTHORIZATION,
  7. SET_CALENDAR_EVENTS
  8. } from './actionTypes';
  9. import { CALENDAR_ENABLED } from './constants';
  10. const DEFAULT_STATE = {
  11. /**
  12. * Note: If features/calendar-sync ever gets persisted, do not persist the
  13. * authorization value as it's needed to remain a runtime value to see if we
  14. * need to re-request the calendar permission from the user.
  15. */
  16. authorization: undefined,
  17. events: [],
  18. knownDomains: []
  19. };
  20. const MAX_DOMAIN_LIST_SIZE = 10;
  21. const STORE_NAME = 'features/calendar-sync';
  22. CALENDAR_ENABLED
  23. && PersistenceRegistry.register(STORE_NAME, {
  24. knownDomains: true
  25. });
  26. CALENDAR_ENABLED
  27. && ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
  28. switch (action.type) {
  29. case ADD_KNOWN_DOMAIN:
  30. return _addKnownDomain(state, action);
  31. case SET_CALENDAR_AUTHORIZATION:
  32. return {
  33. ...state,
  34. authorization: action.status
  35. };
  36. case SET_CALENDAR_EVENTS:
  37. return {
  38. ...state,
  39. events: action.events
  40. };
  41. default:
  42. return state;
  43. }
  44. });
  45. /**
  46. * Adds a new domain to the known domain list if not present yet.
  47. *
  48. * @param {Object} state - The redux state.
  49. * @param {Object} action - The redux action.
  50. * @private
  51. * @returns {Object}
  52. */
  53. function _addKnownDomain(state, action) {
  54. let { knownDomain } = action;
  55. if (knownDomain) {
  56. knownDomain = knownDomain.toLowerCase();
  57. let { knownDomains } = state;
  58. if (knownDomains.indexOf(knownDomain) === -1) {
  59. // Add the specified known domain and at the same time avoid
  60. // modifying the knownDomains Array instance referenced by the
  61. // current redux state.
  62. knownDomains = [
  63. ...state.knownDomains,
  64. knownDomain
  65. ];
  66. // Ensure the list doesn't exceed a/the maximum size.
  67. knownDomains.splice(0, knownDomains.length - MAX_DOMAIN_LIST_SIZE);
  68. return {
  69. ...state,
  70. knownDomains
  71. };
  72. }
  73. }
  74. return state;
  75. }