Add preferences reducer
This commit is contained in:
parent
95d3b72f9b
commit
5781d0de07
3 changed files with 72 additions and 0 deletions
29
app/gabsocial/reducers/__tests__/preferences-test.js
Normal file
29
app/gabsocial/reducers/__tests__/preferences-test.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
import { mastoPrefsToMap } from '../preferences';
|
||||
import { Map as ImmutableMap } from 'immutable';
|
||||
|
||||
describe('mastoPrefToMap', () => {
|
||||
const prefs = {
|
||||
'posting:default:visibility': 'public',
|
||||
'posting:default:sensitive': false,
|
||||
'posting:default:language': null,
|
||||
'reading:expand:media': 'default',
|
||||
'reading:expand:spoilers': false,
|
||||
};
|
||||
it('returns a map', () => {
|
||||
expect(mastoPrefsToMap(prefs)).toEqual(ImmutableMap({
|
||||
posting: ImmutableMap({
|
||||
default: ImmutableMap({
|
||||
visibility: 'public',
|
||||
sensitive: false,
|
||||
language: null,
|
||||
}),
|
||||
}),
|
||||
reading: ImmutableMap({
|
||||
expand: ImmutableMap({
|
||||
media: 'default',
|
||||
spoilers: false,
|
||||
}),
|
||||
}),
|
||||
}));
|
||||
});
|
||||
});
|
|
@ -42,6 +42,7 @@ import soapbox from './soapbox';
|
|||
import instance from './instance';
|
||||
import me from './me';
|
||||
import auth from './auth';
|
||||
import preferences from './preferences';
|
||||
|
||||
const reducers = {
|
||||
dropdown_menu,
|
||||
|
@ -87,6 +88,7 @@ const reducers = {
|
|||
instance,
|
||||
me,
|
||||
auth,
|
||||
preferences,
|
||||
};
|
||||
|
||||
export default combineReducers(reducers);
|
||||
|
|
41
app/gabsocial/reducers/preferences.js
Normal file
41
app/gabsocial/reducers/preferences.js
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { MASTO_PREFS_FETCH_SUCCESS, FE_NAME } from 'gabsocial/actions/preferences';
|
||||
import { ME_FETCH_SUCCESS } from 'gabsocial/actions/me';
|
||||
|
||||
import { Map as ImmutableMap, fromJS } from 'immutable';
|
||||
|
||||
const initialState = ImmutableMap({
|
||||
posting: ImmutableMap({
|
||||
default: ImmutableMap({
|
||||
visibility: 'public',
|
||||
sensitive: false,
|
||||
language: null,
|
||||
}),
|
||||
}),
|
||||
reading: ImmutableMap({
|
||||
expand: ImmutableMap({
|
||||
media: 'default',
|
||||
spoilers: false,
|
||||
}),
|
||||
}),
|
||||
});
|
||||
|
||||
export function mastoPrefsToMap(prefs) {
|
||||
let map = ImmutableMap();
|
||||
for (const [key, value] of Object.entries(prefs)) {
|
||||
map = map.setIn(key.split(':'), value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
export default function preferences(state = initialState, action) {
|
||||
switch(action.type) {
|
||||
case MASTO_PREFS_FETCH_SUCCESS:
|
||||
return state.merge(mastoPrefsToMap(action.prefs));
|
||||
case ME_FETCH_SUCCESS:
|
||||
const me = fromJS(action.me);
|
||||
const fePrefs = me.getIn(['pleroma', 'settings_store', FE_NAME]);
|
||||
return state.merge(fePrefs);
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue