forked from flipt-io/flipt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preferencesSlice.ts
49 lines (41 loc) · 1.4 KB
/
preferencesSlice.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* eslint-disable @typescript-eslint/no-use-before-define */
import { createSlice } from '@reduxjs/toolkit';
import { RootState } from '~/store';
import { Theme, Timezone } from '~/types/Preferences';
import { fetchConfigAsync } from '~/app/meta/metaSlice';
export const preferencesKey = 'preferences';
interface IPreferencesState {
theme: Theme;
timezone: Timezone;
}
const initialState: IPreferencesState = {
theme: Theme.SYSTEM,
timezone: Timezone.LOCAL
};
export const preferencesSlice = createSlice({
name: 'preferences',
initialState,
reducers: {
themeChanged: (state, action) => {
state.theme = action.payload;
},
timezoneChanged: (state, action) => {
state.timezone = action.payload;
}
},
extraReducers(builder) {
builder.addCase(fetchConfigAsync.fulfilled, (state, action) => {
const currentPreference = JSON.parse(
localStorage.getItem(preferencesKey) || '{}'
) as IPreferencesState;
// If there isn't currently a set theme, set to the default theme
if (!currentPreference.theme) {
state.theme = action.payload.ui.defaultTheme;
}
});
}
});
export const { themeChanged, timezoneChanged } = preferencesSlice.actions;
export const selectTheme = (state: RootState) => state.preferences.theme;
export const selectTimezone = (state: RootState) => state.preferences.timezone;
export default preferencesSlice.reducer;