bigbuffet-rw/app/soapbox/reducers/settings.js

47 lines
1.8 KiB
JavaScript
Raw Normal View History

import { SETTING_CHANGE, SETTING_SAVE, FE_NAME } from '../actions/settings';
2020-03-27 13:59:38 -07:00
import { NOTIFICATIONS_FILTER_SET } from '../actions/notifications';
import { EMOJI_USE } from '../actions/emojis';
import { LIST_DELETE_SUCCESS, LIST_FETCH_FAIL } from '../actions/lists';
import { ME_FETCH_SUCCESS } from 'soapbox/actions/me';
2020-03-27 13:59:38 -07:00
import { Map as ImmutableMap, fromJS } from 'immutable';
2020-04-28 10:59:15 -07:00
// Default settings are in action/settings.js
//
// Settings should be accessed with `getSettings(getState()).getIn(...)`
2020-04-28 10:59:15 -07:00
// instead of directly from the state.
2020-03-27 13:59:38 -07:00
const initialState = ImmutableMap({
saved: true,
});
const updateFrequentEmojis = (state, emoji) => state.update('frequentlyUsedEmojis', ImmutableMap(), map => map.update(emoji.id, 0, count => count + 1)).set('saved', false);
const filterDeadListColumns = (state, listId) => state.update('columns', columns => columns.filterNot(column => column.get('id') === 'LIST' && column.get('params').get('id') === listId));
2021-03-23 22:05:06 -07:00
const importSettings = (state, account) => {
account = fromJS(account);
const prefs = account.getIn(['pleroma', 'settings_store', FE_NAME], ImmutableMap());
return state.merge(prefs);
};
2020-03-27 13:59:38 -07:00
export default function settings(state = initialState, action) {
switch(action.type) {
case ME_FETCH_SUCCESS:
2021-03-23 22:05:06 -07:00
return importSettings(state, action.me);
2020-03-27 13:59:38 -07:00
case NOTIFICATIONS_FILTER_SET:
case SETTING_CHANGE:
return state
.setIn(action.path, action.value)
.set('saved', false);
case EMOJI_USE:
return updateFrequentEmojis(state, action.emoji);
case SETTING_SAVE:
return state.set('saved', true);
case LIST_FETCH_FAIL:
return action.error.response.status === 404 ? filterDeadListColumns(state, action.id) : state;
case LIST_DELETE_SUCCESS:
return filterDeadListColumns(state, action.id);
default:
return state;
}
};