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.

93 lines
2.3 KiB

  1. /*
  2. `actions/local_settings`
  3. ========================
  4. > For more information on the contents of this file, please contact:
  5. >
  6. > - kibigo! [@kibi@glitch.social]
  7. This file provides our Redux actions related to local settings. It
  8. consists of the following:
  9. - __`changesLocalSetting(key, value)` :__
  10. Changes the local setting with the given `key` to the given
  11. `value`. `key` **MUST** be an array of strings, as required by
  12. `Immutable.Map.prototype.getIn()`.
  13. - __`saveLocalSettings()` :__
  14. Saves the local settings to `localStorage` as a JSON object. We
  15. shouldn't ever need to call this ourselves.
  16. */
  17. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  18. /*
  19. Constants:
  20. ----------
  21. We provide the following constants:
  22. - __`LOCAL_SETTING_CHANGE` :__
  23. This string constant is used to dispatch a setting change to our
  24. reducer in `reducers/local_settings`, where the setting is
  25. actually changed.
  26. */
  27. export const LOCAL_SETTING_CHANGE = 'LOCAL_SETTING_CHANGE';
  28. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  29. /*
  30. `changeLocalSetting(key, value)`:
  31. ---------------------------------
  32. Changes the local setting with the given `key` to the given `value`.
  33. `key` **MUST** be an array of strings, as required by
  34. `Immutable.Map.prototype.getIn()`.
  35. To accomplish this, we just dispatch a `LOCAL_SETTING_CHANGE` to our
  36. reducer in `reducers/local_settings`.
  37. */
  38. export function changeLocalSetting(key, value) {
  39. return dispatch => {
  40. dispatch({
  41. type: LOCAL_SETTING_CHANGE,
  42. key,
  43. value,
  44. });
  45. dispatch(saveLocalSettings());
  46. };
  47. };
  48. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  49. /*
  50. `saveLocalSettings()`:
  51. ----------------------
  52. Saves the local settings to `localStorage` as a JSON object.
  53. `changeLocalSetting()` calls this whenever it changes a setting. We
  54. shouldn't ever need to call this ourselves.
  55. > __TODO :__
  56. > Right now `saveLocalSettings()` doesn't keep track of which user
  57. > is currently signed in, but it might be better to give each user
  58. > their *own* local settings.
  59. */
  60. export function saveLocalSettings() {
  61. return (_, getState) => {
  62. const localSettings = getState().get('local_settings').toJS();
  63. localStorage.setItem('mastodon-settings', JSON.stringify(localSettings));
  64. };
  65. };