gab-social/app/javascript/gabsocial/reducers/settings.js

68 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-05-03 06:22:49 +01:00
import { SETTING_CHANGE, SETTING_SAVE } from '../actions/settings'
import { STORE_HYDRATE } from '../actions/store'
import { EMOJI_USE } from '../actions/emojis'
import { LIST_DELETE_SUCCESS, LIST_FETCH_FAIL } from '../actions/lists'
import { COMMENT_SORTING_TYPE_OLDEST } from '../constants'
2020-05-03 06:22:49 +01:00
import { Map as ImmutableMap, fromJS } from 'immutable'
import uuid from '../utils/uuid'
2019-07-02 08:10:25 +01:00
const initialState = ImmutableMap({
saved: true,
shownOnboarding: false,
2019-07-02 08:10:25 +01:00
skinTone: 1,
commentSorting: COMMENT_SORTING_TYPE_OLDEST,
2019-07-02 08:10:25 +01:00
2020-05-03 06:22:49 +01:00
displayOptions: ImmutableMap({
fontSize: 'normal',
radiusSmallDisabled: false,
radiusCircleDisabled: false,
theme: 'light',
2020-03-25 03:08:43 +00:00
}),
2019-07-02 08:10:25 +01:00
2020-05-03 06:22:49 +01:00
home: ImmutableMap({
shows: ImmutableMap({
reply: true,
2020-03-25 03:08:43 +00:00
repost: true,
}),
}),
2019-07-02 08:10:25 +01:00
community: ImmutableMap({
2020-03-25 03:08:43 +00:00
shows: ImmutableMap({
2019-07-31 16:04:11 +01:00
onlyMedia: false,
}),
2019-07-02 08:10:25 +01:00
}),
});
const defaultColumns = fromJS([
{ id: 'COMPOSE', uuid: uuid(), params: {} },
{ id: 'HOME', uuid: uuid(), params: {} },
{ id: 'NOTIFICATIONS', uuid: uuid(), params: {} },
]);
const hydrate = (state, settings) => state.mergeDeep(settings).update('columns', (val = defaultColumns) => val);
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));
export default function settings(state = initialState, action) {
switch(action.type) {
case STORE_HYDRATE:
return hydrate(state, action.state.get('settings'));
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;
}
};