2020-11-29 13:11:23 +01:00
|
|
|
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit'
|
|
|
|
|
2020-12-13 14:04:25 +01:00
|
|
|
import { RootState } from '@root/store'
|
2020-11-29 13:11:23 +01:00
|
|
|
// 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'
|
2020-11-29 13:11:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const initialState = {
|
2020-11-29 18:08:31 +01:00
|
|
|
language: undefined,
|
|
|
|
theme: 'auto'
|
2020-11-29 13:11:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-11-29 13:11:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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 13:11:23 +01:00
|
|
|
|
2020-11-29 18:08:31 +01:00
|
|
|
export const { changeLanguage, changeTheme } = settingsSlice.actions
|
2020-11-29 13:11:23 +01:00
|
|
|
export default settingsSlice.reducer
|