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. import { SETTING_CHANGE } from '../actions/settings';
  2. import { COLUMN_ADD, COLUMN_REMOVE, COLUMN_MOVE } from '../actions/columns';
  3. import { STORE_HYDRATE } from '../actions/store';
  4. import Immutable from 'immutable';
  5. import uuid from '../uuid';
  6. const initialState = Immutable.Map({
  7. onboarded: false,
  8. home: Immutable.Map({
  9. shows: Immutable.Map({
  10. reblog: true,
  11. reply: true,
  12. }),
  13. regex: Immutable.Map({
  14. body: '',
  15. }),
  16. }),
  17. notifications: Immutable.Map({
  18. alerts: Immutable.Map({
  19. follow: true,
  20. favourite: true,
  21. reblog: true,
  22. mention: true,
  23. }),
  24. shows: Immutable.Map({
  25. follow: true,
  26. favourite: true,
  27. reblog: true,
  28. mention: true,
  29. }),
  30. sounds: Immutable.Map({
  31. follow: true,
  32. favourite: true,
  33. reblog: true,
  34. mention: true,
  35. }),
  36. }),
  37. community: Immutable.Map({
  38. regex: Immutable.Map({
  39. body: '',
  40. }),
  41. }),
  42. public: Immutable.Map({
  43. regex: Immutable.Map({
  44. body: '',
  45. }),
  46. }),
  47. });
  48. const defaultColumns = Immutable.fromJS([
  49. { id: 'COMPOSE', uuid: uuid(), params: {} },
  50. { id: 'HOME', uuid: uuid(), params: {} },
  51. { id: 'NOTIFICATIONS', uuid: uuid(), params: {} },
  52. ]);
  53. const hydrate = (state, settings) => state.mergeDeep(settings).update('columns', (val = defaultColumns) => val);
  54. const moveColumn = (state, uuid, direction) => {
  55. const columns = state.get('columns');
  56. const index = columns.findIndex(item => item.get('uuid') === uuid);
  57. const newIndex = index + direction;
  58. let newColumns;
  59. newColumns = columns.splice(index, 1);
  60. newColumns = newColumns.splice(newIndex, 0, columns.get(index));
  61. return state.set('columns', newColumns);
  62. };
  63. export default function settings(state = initialState, action) {
  64. switch(action.type) {
  65. case STORE_HYDRATE:
  66. return hydrate(state, action.state.get('settings'));
  67. case SETTING_CHANGE:
  68. return state.setIn(action.key, action.value);
  69. case COLUMN_ADD:
  70. return state.update('columns', list => list.push(Immutable.fromJS({ id: action.id, uuid: uuid(), params: action.params })));
  71. case COLUMN_REMOVE:
  72. return state.update('columns', list => list.filterNot(item => item.get('uuid') === action.uuid));
  73. case COLUMN_MOVE:
  74. return moveColumn(state, action.uuid, action.direction);
  75. default:
  76. return state;
  77. }
  78. };