Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

reducer.js 2.1KB

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