tooot/src/utils/slices/settingsSlice.ts

88 lines
2.5 KiB
TypeScript
Raw Normal View History

import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit'
2022-02-06 23:25:16 +01:00
import { LOCALES } from '@root/i18n/locales'
2021-03-09 01:17:07 +01:00
import { RootState } from '@root/store'
2020-12-29 16:19:04 +01:00
import * as Analytics from 'expo-firebase-analytics'
2021-01-17 22:37:05 +01:00
import * as Localization from 'expo-localization'
2021-01-31 03:09:35 +01:00
import { pickBy } from 'lodash'
2021-02-20 19:12:44 +01:00
export const changeAnalytics = createAsyncThunk(
'settings/changeAnalytics',
async (newValue: SettingsState['analytics']) => {
await Analytics.setAnalyticsCollectionEnabled(newValue)
2021-08-29 15:25:38 +02:00
return { newValue }
2021-02-20 19:12:44 +01:00
}
)
export type SettingsState = {
2021-03-10 10:22:53 +01:00
fontsize: -1 | 0 | 1 | 2 | 3
2021-05-30 15:40:06 +02:00
language: string
2020-11-29 18:08:31 +01:00
theme: 'light' | 'dark' | 'auto'
2020-12-20 18:41:28 +01:00
browser: 'internal' | 'external'
2020-12-29 16:19:04 +01:00
analytics: boolean
}
2021-01-07 19:13:09 +01:00
export const settingsInitialState = {
2021-03-10 10:22:53 +01:00
fontsize: 0,
2021-02-20 19:12:44 +01:00
notification: {
enabled: false
},
2021-01-31 03:09:35 +01:00
language: Object.keys(
2022-02-06 23:25:16 +01:00
pickBy(LOCALES, (_, key) => Localization.locale.startsWith(key))
2021-01-31 03:09:35 +01:00
)
? Object.keys(
2022-02-06 23:25:16 +01:00
pickBy(LOCALES, (_, key) => Localization.locale.startsWith(key))
2021-01-31 03:09:35 +01:00
)[0]
: 'en',
2020-12-20 18:41:28 +01:00
theme: 'auto',
2020-12-29 16:19:04 +01:00
browser: 'internal',
2020-12-30 14:33:33 +01:00
analytics: true
}
const settingsSlice = createSlice({
name: 'settings',
2021-01-07 19:13:09 +01:00
initialState: settingsInitialState as SettingsState,
reducers: {
2021-03-10 10:22:53 +01:00
changeFontsize: (
state,
action: PayloadAction<SettingsState['fontsize']>
) => {
state.fontsize = action.payload
},
changeLanguage: (
state,
action: PayloadAction<NonNullable<SettingsState['language']>>
) => {
state.language = action.payload
2020-11-29 18:08:31 +01:00
},
changeTheme: (
state,
action: PayloadAction<NonNullable<SettingsState['theme']>>
) => {
state.theme = action.payload
2020-12-20 18:41:28 +01:00
},
changeBrowser: (
state,
action: PayloadAction<NonNullable<SettingsState['browser']>>
) => {
state.browser = action.payload
}
2020-12-29 16:19:04 +01:00
},
extraReducers: builder => {
builder.addCase(changeAnalytics.fulfilled, (state, action) => {
2021-08-29 15:25:38 +02:00
state.analytics = action.payload.newValue
2020-12-29 16:19:04 +01:00
})
}
})
2021-03-10 10:22:53 +01:00
export const getSettingsFontsize = (state: RootState) =>
state.settings.fontsize || 0
export const getSettingsLanguage = (state: RootState) => state.settings.language
2020-11-29 18:08:31 +01:00
export const getSettingsTheme = (state: RootState) => state.settings.theme
2020-12-20 18:41:28 +01:00
export const getSettingsBrowser = (state: RootState) => state.settings.browser
2020-12-29 16:19:04 +01:00
export const getSettingsAnalytics = (state: RootState) =>
state.settings.analytics
2022-02-06 23:25:16 +01:00
export const { changeFontsize, changeLanguage, changeTheme, changeBrowser } =
settingsSlice.actions
export default settingsSlice.reducer