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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // @flow
  2. import { parseURLParams } from '../config';
  3. import { toState } from '../redux';
  4. /**
  5. * Returns the effective value of a configuration/preference/setting by applying
  6. * a precedence among the values specified by JWT, URL, settings,
  7. * and config.
  8. *
  9. * @param {Object|Function} stateful - The redux state object or
  10. * {@code getState} function.
  11. * @param {string} propertyName - The name of the
  12. * configuration/preference/setting (property) to retrieve.
  13. * @param {{
  14. * config: boolean,
  15. * jwt: boolean,
  16. * settings: boolean,
  17. * urlParams: boolean
  18. * }} [sources] - A set/structure of {@code boolean} flags indicating the
  19. * configuration/preference/setting sources to consider/retrieve values from.
  20. * @returns {any}
  21. */
  22. export function getPropertyValue(
  23. stateful: Object | Function,
  24. propertyName: string,
  25. sources?: Object
  26. ) {
  27. // Default values don't play nicely with partial objects and we want to make
  28. // the function easy to use without exhaustively defining all flags:
  29. sources = { // eslint-disable-line no-param-reassign
  30. // Defaults:
  31. config: true,
  32. jwt: true,
  33. settings: true,
  34. urlParams: true,
  35. ...sources
  36. };
  37. // Precedence: jwt -> urlParams -> settings -> config.
  38. const state = toState(stateful);
  39. // jwt
  40. if (sources.jwt) {
  41. const value = state['features/base/jwt'][propertyName];
  42. if (typeof value !== 'undefined') {
  43. return value[propertyName];
  44. }
  45. }
  46. // urlParams
  47. if (sources.urlParams) {
  48. const urlParams
  49. = parseURLParams(state['features/base/connection'].locationURL);
  50. const value = urlParams[`config.${propertyName}`];
  51. if (typeof value !== 'undefined') {
  52. return value;
  53. }
  54. }
  55. // settings
  56. if (sources.settings) {
  57. const value = state['features/base/settings'][propertyName];
  58. if (typeof value !== 'undefined') {
  59. return value;
  60. }
  61. }
  62. // config
  63. if (sources.config) {
  64. const value = state['features/base/config'][propertyName];
  65. if (typeof value !== 'undefined') {
  66. return value;
  67. }
  68. }
  69. return undefined;
  70. }