tooot/src/utils/slices/settingsSlice.ts

87 lines
2.1 KiB
TypeScript
Raw Normal View History

import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit'
2020-12-13 14:04:25 +01:00
import { RootState } from '@root/store'
// import client from 'src/api/client'
export type SettingsState = {
language: 'zh' | 'en' | undefined
2020-11-29 18:08:31 +01:00
theme: 'light' | 'dark' | 'auto'
}
const initialState = {
2020-11-29 18:08:31 +01:00
language: undefined,
theme: 'auto'
}
// export const updateLocal = createAsyncThunk(
// 'instances/updateLocal',
// async ({
// url,
// token
// }: {
// url?: InstancesState['local']['url']
// token?: InstancesState['local']['token']
// }) => {
// if (!url || !token) {
// return initialStateLocal
// }
// const {
// body: { id }
// } = await client({
// method: 'get',
// instance: 'remote',
// instanceUrl: url,
// endpoint: `accounts/verify_credentials`,
// headers: { Authorization: `Bearer ${token}` }
// })
// const { body: preferences } = await client({
// method: 'get',
// instance: 'remote',
// instanceUrl: url,
// endpoint: `preferences`,
// headers: { Authorization: `Bearer ${token}` }
// })
// return {
// url,
// token,
// account: {
// id,
// preferences
// }
// }
// }
// )
const settingsSlice = createSlice({
name: 'settings',
initialState: initialState as SettingsState,
reducers: {
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
}
}
// extraReducers: builder => {
// builder.addCase(updateLocal.fulfilled, (state, action) => {
// state.local = action.payload
// })
// }
})
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-11-29 18:08:31 +01:00
export const { changeLanguage, changeTheme } = settingsSlice.actions
export default settingsSlice.reducer