Source: actions/settingsActions.js

  1. /**
  2. * @module react-cmf/lib/actions/settingsActions
  3. */
  4. import get from 'lodash/get';
  5. import http from './http';
  6. import CONSTANTS from '../constant';
  7. // keep backward compatibility
  8. export const { REQUEST_OK, REQUEST_KO, REQUEST_SETTINGS } = CONSTANTS;
  9. export function requestSettings() {
  10. return {
  11. type: CONSTANTS.REQUEST_SETTINGS,
  12. };
  13. }
  14. export function receiveSettings(json) {
  15. return {
  16. type: CONSTANTS.REQUEST_OK,
  17. settings: json,
  18. receivedAt: Date.now(),
  19. };
  20. }
  21. export function errorWithSettings(error) {
  22. return {
  23. type: CONSTANTS.REQUEST_KO,
  24. error: {
  25. message: get(error, 'message'),
  26. stack: get(error, 'stack'),
  27. },
  28. };
  29. }
  30. /**
  31. * get the settings on the server and dispatch the corresponding actions
  32. * this should be executed during the bootstrap of the App.
  33. * @param path Path of the settings.json file to fetch. Default 'settings.json'
  34. * @return {function} with the fetch process results
  35. */
  36. export function fetchSettings(path = 'settings.json') {
  37. return http.get(path, {
  38. onResponse: response => receiveSettings(response),
  39. onError: error => errorWithSettings(error),
  40. });
  41. }